-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.rc
177 lines (164 loc) · 7.86 KB
/
init.rc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
#
# init.rc — all tools in one place
# https://github.com/bkahlert/init
#
# MIT License
#
# Copyright (c) 2021 Dr. Björn Kahlert
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
(return 2>/dev/null) || [ $# -gt 0 ] || set -- "-!-"
export INIT_VERSION=0.1.0
declare -A cmds=(
['bash']=libguestfs # GNU Bourne-Again SHell
['base32']=libguestfs # Transform data into printable data
['base64']=libguestfs # Transform data into printable data
['bats']=bats # Run Bats (Bash Automated Testing System) tests
['batsw']=bats # Run Bats (Bash Automated Testing System) tests
['guestfish']=libguestfs # The guest filesystem shell
['guestmount']=libguestfs # Mount a guest filesystem on the host using FUSE and libguestfs
['guestunmount']=libguestfs # Unmount a guest'""'mounted filesystem
['jq']=jq # Command-line JSON processor
['recordr']=recordr # Record terminal sessions and convert them to SVG
['recordrw']=recordr # Record terminal sessions and convert them to SVG
['virt-alignment-scan']=libguestfs # Check alignment of virtual machine partitions
['virt-builder']=libguestfs # Build virtual machine images quickly
['virt-builder-repository']=libguestfs # Build virt-builder source repository easily
['virt-cat']=libguestfs # Display files in a virtual machine
['virt-copy-in']=libguestfs # Copy files and directories into a virtual machine disk image
['virt-copy-out']=libguestfs # Copy files and directories out of a virtual machine disk image
['virt-customize']=libguestfs # Customize a virtual machine
['virt-df']=libguestfs # Display free space on virtual filesystems
['virt-dib']=libguestfs # Run diskimage-builder elements
['virt-diff']=libguestfs # Differences between files in two virtual machines
['virt-edit']=libguestfs # Edit a file in a virtual machine
['virt-filesystems']=libguestfs # List filesystems, partitions, block devices, LVM in a virtual machine or disk image
['virt-format']=libguestfs # Erase and make a blank disk
['virt-get-kernel']=libguestfs # Extract kernel and ramdisk from guests
['virt-index-validate']=libguestfs # Validate virt-builder index file
['virt-inspector']=libguestfs # Display operating system version and other information about a virtual machine
['virt-list-filesystems']=libguestfs # List filesystems in a virtual machine or disk image
['virt-list-partitions']=libguestfs # List partitions in a virtual machine or disk image
['virt-log']=libguestfs # Display log files from a virtual machine
['virt-ls']=libguestfs # List files in a virtual machine
['virt-make-fs']=libguestfs # Make a filesystem from a tar archive or files
['virt-p2v-make-disk']=libguestfs # Build the virt-p2v disk using virt-builder
['virt-p2v-make-kickstart']=libguestfs # Build the virt-p2v kickstart
['virt-p2v-make-kiwi']=libguestfs # Build the virt-p2v kiwi configuration
['virt-rescue']=libguestfs # Run a rescue shell on a virtual machine
['virt-resize']=libguestfs # Resize a virtual machine disk
['virt-sparsify']=libguestfs # Make a virtual machine disk sparse
['virt-sysprep']=libguestfs # Reset, un'""'configure or customize a virtual machine so clones can be made
['virt-tail']=libguestfs # Follow (tail) files in a virtual machine
['virt-tar']=libguestfs # Extract or upload files to a virtual machine
['virt-tar-in']=libguestfs # Unpack a tarball into a virtual machine disk image
['virt-tar-out']=libguestfs # Pack a virtual machine disk image directory into a tarball
['virt-v2v']=libguestfs # Convert a guest to use KVM
['virt-v2v-copy-to-local']=libguestfs # Copy a remote guest to the local machine
['virt-win-reg']=libguestfs # Export and merge Windows Registry entries from a Windows guest
)
# Runs Bats (Bash Automated Testing System) tests
bats() {
shift
curl -LfsS https://git.io/batsw | "$SHELL" -s -- "$@"
}
# Runs a command-line JSON processor
jq() {
shift
docker run --rm -i \
-v "$PWD":"$PWD" \
-w "$PWD" jetbrainsinfra/jq jq \
"$@"
}
# Records terminal sessions and convert them to SVG
recordr() {
shift
curl -LfsS https://git.io/recordrw | "$SHELL" -s -- "$@"
}
# Accesses and modifies virtual machine disk images
libguestfs() {
curl -LfsS https://git.io/libguestfsw | "$SHELL" -s -- "$@"
}
# Creates an alias for the specified command and wrapper.
# Arguments:
# 1 - name of the new command
# 2 - name of the implementing command
create_alias() {
local cmd=${1?command missing} wrapper=${2?wrapper missing} wrapper_declaration patched LF=$'\n'
command -v "$cmd" &>/dev/null || {
wrapper_declaration=$(declare -f "$wrapper")
patched="$cmd${wrapper_declaration#"$wrapper"}"
eval "${patched/{/{"${LF}set -- '$cmd' \"\$@\""}"
}
}
# Usage:
# - create aliases: source <(curl -LfsS https://git.io/init.rc)
# - run single command: curl -LfsS https://git.io/init.rc | "$SHELL" -s -- COMMAND [ARGS]
main() {
local usage="COMMAND [ARGS...]"
if [ "${1-}" = '-!-' ]; then
set -- '--help'
elif [ $# -eq 0 ]; then
set -- '--setup'
fi
case $1 in
-h|--help)
source <(curl -LfsS https://git.io/logr.sh)
# TODO move to logr
# Parses this script for a line documenting the parameter and returns the comment.
describe() { sed -En "/[[:space:]]+\['$1'].*#/p" <(cat "${BASH_SOURCE[0]}" 2>/dev/null || curl -LfsS https://git.io/init.rc) | sed -E 's/[^#]*#[[:space:]]*(.*)$/\1/g'; }
declare margin="$MARGIN " commands=() col_width=26
for cmd in "${!cmds[@]}"; do
commands+=("$(printf -- '%s%*s' "$cmd" $((col_width - ${#cmd})) '')" "$(describe "$cmd") (default: ${cmds[$cmd]})")
done
printf '\n %s\n\n Usage: curl -LfsS https://git.io/init.rc | "$SHELL" -s -- %s%s' "$(banr --static "init.rc" "$INIT_VERSION")" "$usage" "
${MARGIN}Commands:
$(printf "$margin"'%s %s\n' "${commands[@]}")
"
exit "$EX_OK"
;;
--setup)
local cmd
if [ "${ZSH_VERSION-}" ]; then
for cmd in ${(@k)cmds}; do
create_alias "$cmd" "$cmds[$cmd]"
done
else
for cmd in "${!cmds[@]}"; do
create_alias "$cmd" "${cmds[$cmd]}"
done
fi
;;
*)
local cmd
if command "$cmd" &>/dev/null; then
"$@"
else
if [ "${ZSH_VERSION-}" ]; then
cmd="$cmds[$1]"
else
cmd="${cmds[$1]}"
fi
"$cmd" "$@"
fi
;;
esac
}
main "$@"