Add autoinstall command

This commit is contained in:
Tobias Reisinger 2024-01-09 16:48:11 +01:00
parent d6985ded7e
commit b5eaaead9b
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
14 changed files with 344 additions and 4 deletions

View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
target=${args[target]:?}
url=${args[url]:?}
if [ ! -f "$target" ]
then
echo "Installing archive $url to $target"
target_dir=$(dirname "$target")
mkdir -p "$target_dir"
cd "$target_dir" || exit 1
_http_client "$url" > "$target"
tar xaf "$target"
_run_hook
fi

107
autoinstall/bashly.yml Normal file
View file

@ -0,0 +1,107 @@
name: autoinstall
help: Install files, repository and else
version: 0.1.0
flags:
- long: --clean
short: -c
help: Clean exisiting targets
- long: --hook
short: -h
help: Hook to run if something changed
arg: hook
environment_variables:
- name: AUTOINSTALL_CLEAN
help: Clean exisiting targets
dependencies:
http_client:
command: [curl, wget]
help: Please install either curl or wget
commands:
- name: completions
help: Generate bash completions
- name: run
help: Autoinstall from file
args:
- name: group
required: true
help: The group to install
- name: git
help: Install a git repository
args:
- name: repo
required: true
help: The url of the git repository
- name: target
required: true
help: The path to clone the repository into
- name: file
help: Install a file
args:
- name: url
required: true
help: The url of the file
- name: target
required: true
help: The file-path to download the file into
flags:
- long: --pipe
short: -p
help: Pipe file through command (e.g. "tar xzO")
arg: pipe
default: cat
- name: exe
help: Install an executable
args:
- name: url
required: true
help: The url of the file
- name: target
required: true
help: The file-path to download the file into
filename: file_command.sh
flags:
- long: --pipe
short: -f
help: Pipe file through command (e.g. "tar xzO")
arg: pipe
default: cat
- name: archive
help: Install an archive
args:
- name: url
required: true
help: The url of the arhive
- name: target
required: true
help: The path to clone the repository into
- name: env
help: Create a file with envsubst
args:
- name: template
required: true
help: The path to the template
- name: target
required: true
help: The path to write the result to
dependencies:
envsubst: This tool is usually part of the gettext package
- name: text
help: Create a file from text
args:
- name: text
required: true
help: The text to write to the file
- name: target
required: true
help: The path to write the result to

20
autoinstall/before.sh Normal file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env bash
if [ "${action:?}" = "run" ]
then
return
fi
clean=${args[--clean]:-}
target=${args[target]:?}
if [ "${action:?}" = "exe" ]
then
target="$HOME/.local/bin/$target"
fi
if [ -n "$clean" ] || [ -n "${AUTOINSTALL_CLEAN:-}" ]
then
echo "Cleaning $target"
rm -rf "$target"
fi

View file

@ -0,0 +1,3 @@
#!/usr/bin/env bash
send_completions

View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
target=${args[target]:?}
template=${args[template]:?}
if [ ! -f "$target" ]
then
echo "Installing env-template $template to $target"
target_dir=$(dirname "$target")
mkdir -p "$target_dir"
cd "$target_dir" || exit 1
envsubst < "$template" > "$target"
_run_hook
fi

View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
pipe=${args[--pipe]:?}
target=${args[target]:?}
url=${args[url]:?}
if [ "${action:-file}" = "exe" ]
then
if [ -x "$(command -v "$target")" ]
then
return
fi
target="$HOME/.local/bin/$target"
fi
if [ ! -f "$target" ]
then
echo "Installing file $url to $target"
target_dir=$(dirname "$target")
mkdir -p "$target_dir"
cd "$target_dir" || exit 1
_http_client "$url" | eval "$pipe" > "$target"
if [ "${action:-file}" = "exe" ]
then
chmod +x "$target"
fi
_run_hook
fi

View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
target=${args[target]:?}
repo=${args[repo]:?}
if [ ! -d "$target" ]
then
echo "Installing repo $repo to $target"
mkdir -p "$target"
git clone --depth=1 "$repo" "$target" >/dev/null 2>&1
_run_hook
fi

42
autoinstall/lib/colors.sh Normal file
View file

@ -0,0 +1,42 @@
## Color functions [@bashly-upgrade colors]
## This file is a part of Bashly standard library
##
## Usage:
## Use any of the functions below to color or format a portion of a string.
##
## echo "before $(red this is red) after"
## echo "before $(green_bold this is green_bold) after"
##
## Color output will be disabled if `NO_COLOR` environment variable is set
## in compliance with https://no-color.org/
##
print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
yellow() { print_in_color "\e[33m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }
magenta() { print_in_color "\e[35m" "$*"; }
cyan() { print_in_color "\e[36m" "$*"; }
bold() { print_in_color "\e[1m" "$*"; }
underlined() { print_in_color "\e[4m" "$*"; }
red_bold() { print_in_color "\e[1;31m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
blue_bold() { print_in_color "\e[1;34m" "$*"; }
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
red_underlined() { print_in_color "\e[4;31m" "$*"; }
green_underlined() { print_in_color "\e[4;32m" "$*"; }
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

20
autoinstall/lib/common.sh Normal file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env bash
_http_client() {
if [ -x "$(command -v curl)" ]
then
curl -fsSL "$1"
elif [ -x "$(command -v wget)" ]
then
wget -qO - "$1"
fi
}
_run_hook() {
hook=${args[--hook]:-}
if [ -n "$hook" ]
then
yellow "Running hook: $hook"
bash -c "$hook"
fi
}

View file

@ -0,0 +1,40 @@
#!/usr/bin/env bash
group=${args[group]:?}
_config_query() {
tomlq -c --arg group "$group" \
'.autoinstall | map(select(.group == $group)) | '"$1" \
"$XDG_CONFIG_HOME/autoinstall.toml"
}
#length=$(_config_query "length")
_config_query ".[]" | while read -r entry; do
install_args=()
type=$(echo "$entry" | jq -r '.type')
source=$(echo "$entry" | jq -r '.source')
target=$(echo "$entry" | jq -r '.target')
hook=$(echo "$entry" | jq -r '.hook // ""')
pipe=$(echo "$entry" | jq -r '.pipe // ""')
if [[ -n "$hook" ]]; then
install_args+=("--hook=$hook")
fi
install_args+=("$type")
if [[ -n "$pipe" ]]; then
install_args+=("--pipe=$pipe")
fi
if [[ $source = \$* ]]
then
source=$(eval "echo $source")
fi
install_args+=("$source")
install_args+=("$(eval "echo $target")")
autoinstall "${install_args[@]}"
done

View file

@ -0,0 +1,16 @@
#!/usr/bin/env bash
target=${args[target]:?}
text=${args[text]:?}
if [ ! -f "$target" ]
then
echo "Installing text '$text' to $target"
target_dir=$(dirname "$target")
mkdir -p "$target_dir"
echo "$text" > "$target"
_run_hook
fi

View file

@ -3,9 +3,7 @@ help: Manage autostart
version: 0.1.0
dependencies:
yq:
command: [tomlq]
help: please install yq (https://github.com/kislyuk/yq)
tomlq: please install yq (https://github.com/kislyuk/yq)
environment_variables:
- name: HOSTNAME
@ -76,6 +74,7 @@ commands:
- $(autostart-manage list)
filename: systemctl.sh
- name: log
alias: logs
help: Show the log for a single program from autostart
args:
- name: program

View file

@ -18,6 +18,7 @@ _list () {
}
_autostart_run_graphical () {
set +e
pass x # Try to unlock yubikey asap
start-audio pipewire
@ -33,7 +34,7 @@ _autostart_run_graphical () {
fi
fi
autoinstall graphical
autoinstall run graphical
autostart-manage run
}

9
run.sh Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env sh
target=$(cat .target)
[ -n "$target" ] || exit 1
make generate
"./output/$target" "$@"