-
-
Notifications
You must be signed in to change notification settings - Fork 1
Split main() into smaller functions. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,6 @@ | |
| # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| Verbose= | ||
| DryRun= | ||
|
|
||
| run() { | ||
| if [ -n "$DryRun" ]; then | ||
| echo "$*" | ||
|
|
@@ -52,7 +49,12 @@ sq() { | |
| } | ||
|
|
||
| main() { | ||
| local opt file destdir filename dest tmpfile ppa | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variables within this declaration became unused after extraction. Replaced with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: AFAIK Most of the time |
||
| # Script is being sourced, skip CLI logic | ||
| if [ "${0##*/}" != apt-bundle ]; then | ||
| initialize | ||
| main_prerequisites; | ||
| return | ||
|
Comment on lines
+54
to
+56
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a user is sourcing Prerequisites are also obviously necessary. Prerequisites leverage the same system for installing packages so they need the same temp directory & files. |
||
| fi | ||
|
|
||
| set -e | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exit on first error can be enabled from the shebang |
||
|
|
||
|
|
@@ -61,6 +63,19 @@ main() { | |
| exec sudo "$0" "$@" | ||
| fi | ||
|
|
||
| main_options "$@"; | ||
|
|
||
| initialize | ||
| main_prerequisites; | ||
| main_env; | ||
|
|
||
| cat -- "$@" > "$TmpDir/Debfile" | ||
| . "$TmpDir/Debfile" | ||
|
Comment on lines
+72
to
+73
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a user is sourcing We have to let the user invoke source/package and eventually a "flush" function (not yet defined). |
||
|
|
||
| apply_changes; | ||
| } | ||
|
|
||
| main_options() { | ||
| while getopts "nv" opt; do | ||
| case $opt in | ||
| n) | ||
|
|
@@ -80,10 +95,15 @@ main() { | |
| echo "usage: $0 [-n] [-v] <Debfile>..." >&2 | ||
| exit 64 | ||
| fi | ||
| } | ||
|
|
||
| type apt >/dev/null 2>&1 || die "apt is not installed" | ||
| main_env() { | ||
| export ARCH="$(dpkg --print-architecture)" | ||
| eval "$(sed 's/^/export /' /etc/os-release)" | ||
| } | ||
|
|
||
| initialize | ||
| main_prerequisites() { | ||
| type apt >/dev/null 2>&1 || die "apt is not installed" | ||
|
|
||
| for file in apt-transport-https software-properties-common curl gpg; do | ||
| is_installed "$file" && continue | ||
|
|
@@ -96,22 +116,29 @@ main() { | |
| xrun apt install -y < "$TmpDir/packages" | ||
| : > "$TmpDir/packages" | ||
| fi | ||
| } | ||
|
|
||
| export ARCH="$(dpkg --print-architecture)" | ||
| eval "$(sed 's/^/export /' /etc/os-release)" | ||
| apply_changes() { | ||
| apply_keyrings; | ||
| apply_sources; | ||
| apply_ppas; | ||
|
|
||
| cat -- "$@" > "$TmpDir/Debfile" | ||
| . "$TmpDir/Debfile" | ||
| run apt update | ||
|
|
||
| destdir=/usr/share/keyrings | ||
| apply_packages; | ||
| apply_debs; | ||
| } | ||
|
|
||
| apply_keyrings() { | ||
| local destdir=/usr/share/keyrings | ||
|
|
||
| find "$TmpDir/keyring_scripts" -type f | while read -r file; do | ||
| filename=$(basename "$file" .sh).gpg | ||
| dest=$destdir/$filename | ||
| tmpfile=$TmpDir/keyrings/$filename | ||
| local filename=$(basename "$file" .sh).gpg | ||
| local dest=$destdir/$filename | ||
| local tmpfile=$TmpDir/keyrings/$filename | ||
| ( | ||
| set -e | ||
| dir=$TmpDir/tmp | ||
| local dir=$TmpDir/tmp | ||
| rm -rf "$dir" | ||
| mkdir "$dir" | ||
| cd "$dir" | ||
|
|
@@ -124,35 +151,41 @@ main() { | |
| cmp -s "$tmpfile" "$dest" 2>/dev/null || | ||
| run install -o 0 -g 0 -m 644 "$tmpfile" "$dest" | ||
| done | ||
| } | ||
|
|
||
| destdir=/etc/apt/sources.list.d | ||
| apply_sources() { | ||
| local destdir=/etc/apt/sources.list.d | ||
|
|
||
| find "$TmpDir/sources" -type f -name '*.list' | while read -r file; do | ||
| cmp -s "$file" "$destdir/$(basename "$file")" 2>/dev/null || | ||
| run install -o 0 -g 0 -m 644 "$file" "$destdir/" | ||
| done | ||
| } | ||
|
|
||
| apply_ppas() { | ||
| while read ppa; do | ||
| if [ -z "$(find /etc/apt/sources.list.d -name '*.list' -type f -print0 | xargs -r0 awk -v repo="http://ppa.launchpad.net/$ppa/ubuntu" '$1 == "deb" && $2 == repo')" ]; then | ||
| run add-apt-repository --no-update -y "ppa:$ppa" | ||
| fi | ||
| done < "$TmpDir/ppas" | ||
| } | ||
|
|
||
| run apt update | ||
|
|
||
| apply_packages() { | ||
| if [ -s "$TmpDir/packages" ]; then | ||
| xrun apt install -y < "$TmpDir/packages" | ||
| fi | ||
|
|
||
| if [ -s "$TmpDir/satisfy" ]; then | ||
| xrun apt satisfy -y < "$TmpDir/satisfy" | ||
| fi | ||
| } | ||
|
|
||
| destdir=$HOME/.cache/$(basename "$0") | ||
| apply_debs() { | ||
| local destdir=$HOME/.cache/$(basename "$0") | ||
| mkdir -p "$destdir" | ||
|
|
||
| while read url; do | ||
| file=$(basename "$url") | ||
| local file=$(basename "$url") | ||
| if [ -f "$destdir/$file" ]; then | ||
| curl -fsSLR -z "$destdir/$file" -o "$destdir/$file" "$url" | ||
| else | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe these may have been initialized here, empty, as documentation / for developer awareness of their existence.
LMK if you want them restored.