Skip to content

Commit

Permalink
feat(package): add a command to install a package
Browse files Browse the repository at this point in the history
  • Loading branch information
yunielrc committed Sep 13, 2023
1 parent 0618b80 commit da8fa28
Show file tree
Hide file tree
Showing 27 changed files with 402 additions and 78 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ test-integration:
test-functional:
./tools/bats-functional

test-all: test-unit test-integration test-functional
test-all:
./tools/bats --recursive tests

# ci server does not support VT-x so we can't run integration or functional tests
test-all-ci: test-unit
Expand Down
3 changes: 3 additions & 0 deletions src/etc/skel/.ydf.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# shellcheck disable=SC2034,SC2148

# YDF_PACKAGE_SERVICE_DEFAULT_OS=manjaro
1 change: 1 addition & 0 deletions src/etc/ydf/ydf.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# shellcheck disable=SC2034,SC2148
22 changes: 20 additions & 2 deletions src/usr/lib/ydf/components/package/ydf-package-command.bash
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ ydf::package_command::constructor() {
ydf::package_command::__install_help() {
cat <<-HELPMSG
Usage:
${__YDF_SCRIPT_NAME} package install PACKAGE [PACKAGE...]
${__YDF_SCRIPT_NAME} package install [OPTIONS] PACKAGE [PACKAGE...]
Install packages
Flags:
-h, --help Show this help
Options:
--os Operating system
HELPMSG
}

Expand All @@ -58,6 +61,9 @@ HELPMSG
# Flags:
# -h | --help Show help
#
# Options:
# --os Operating system
#
# Arguments:
# PACKAGE [PACKAGE...] one or more packages
#
Expand All @@ -69,6 +75,7 @@ HELPMSG
#
ydf::package_command::__install() {
local packages=''
local os=''

if [[ $# == 0 ]]; then set -- '-h'; fi

Expand All @@ -79,6 +86,17 @@ ydf::package_command::__install() {
ydf::package_command::__install_help
return 0
;;
# options
--os)
readonly os="${2:-}"
# validate argument
if [[ -z "$os" ]]; then
err "No os name specified\n"
ydf::package_command::__install_help
return "$ERR_MISSING_ARG"
fi
shift 2
;;
# arguments
*)
readonly packages="$*"
Expand All @@ -93,7 +111,7 @@ ydf::package_command::__install() {
return "$ERR_MISSING_ARG"
fi

ydf::package_service::install "$packages"
ydf::package_service::install "$packages" "$os"
}

ydf::package_command::__help() {
Expand Down
102 changes: 80 additions & 22 deletions src/usr/lib/ydf/components/package/ydf-package-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ fi
# [ubuntu]="apt"
# )

readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='home homeln homecp homecps homecat root rootcp rootcps rootln rootcat flatpack dconf.ini plugin_zsh docker_compose'
readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="pre_install pacman yay install post_install ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}"
readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="pre_install apt install post_install ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}"
# readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='home homeln homecp homecps homecat root rootcp rootcps rootln rootcat flatpack dconf.ini plugin_zsh docker_compose'
# readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall pacman yay install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}"
# readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="preinstall apt install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}"

readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON=''
readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}"
readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="preinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}"

#
# FUNCTIONS
Expand All @@ -44,16 +48,53 @@ ydf::package_service::constructor() {
readonly __YDF_PACKAGE_SERVICE_DEFAULT_OS="$1"
}

ydf::package_service::__instruction_iname() {
:
#
# Get instructions names
#
# Arguments:
# [os_name] string os name
#
# Output:
# writes instructions_names (string) to the stdout
#
# Returns:
# 0 on success, non-zero on error.
#
ydf::package_service::get_instructions_names() {
local -r os_name="${1:-"$__YDF_PACKAGE_SERVICE_DEFAULT_OS"}"

local instr
instr="__YDF_PACKAGE_SERVICE_INSTRUCTIONS_${os_name^^}"

if [[ -z "${!instr:-}" ]]; then
err "There is no instructions for os: ${os_name}"
return "$ERR_INVAL_ARG"
fi

echo "${!instr}"
}

#
# Execute preinstall script
#
#
# Returns:
# 0 on success, non-zero on error.
#
ydf::package_service::__instruction_preinstall() {
if [[ ! -f ./preinstall ]]; then
return 0
fi

bash ./preinstall
}

#
# Install a ydotfile package from a directory
#
# Arguments:
# package_dir string package directory
# os_name string os name
# [os_name] string os name
#
# Output:
# writes installed package name to the stdout
Expand All @@ -63,38 +104,54 @@ ydf::package_service::__instruction_iname() {
#
ydf::package_service::install_one_from_dir() {
local -r package_dir="$1"
local -r os_name="${2:-"$__YDF_PACKAGE_SERVICE_DEFAULT_OS"}"
local -r os_name="${2:-}"

# validate arguments
if [[ ! -d "$package_dir" ]]; then
err "Directory '${package_dir}' doesn't exist"
return "$ERR_NO_DIR"
fi

local inst_specific
inst_specific="__YDF_PACKAGE_SERVICE_INSTRUCTIONS_${os_name^^}"
readonly inst_specific
local instr
instr="$(ydf::package_service::get_instructions_names "$os_name")" || {
err "Getting instructions names for os: ${os_name}"
return "$ERR_YPS_GENERAL"
}
readonly instr

local -a instructions_arr
# shellcheck disable=SC2206,SC2317
instructions_arr=(${!inst_specific})
readonly instructions_arr
if [[ -z "$instr" ]]; then
err "There is no instructions"
return "$ERR_INVAL_VALUE"
fi

for iname in "${instructions_arr[@]}"; do
local ifunction="ydf::package_service::__instruction_${iname}"
local -a instr_arr
# shellcheck disable=SC2206,SC2317
instr_arr=($instr)
readonly instr_arr

"$ifunction" "$package_dir" || {
err "Executing instruction '${iname}' on '${package_dir}'"
return "$ERR_YPS_INSTRUCTION_FAIL"
(
cd "$package_dir" 2>/dev/null || {
err "Changing the current directory to ${package_dir}"
return "$ERR_CHANGING_WORKDIR"
}
done

for iname in "${instr_arr[@]}"; do
local ifunction="ydf::package_service::__instruction_${iname}"

"$ifunction" || {
err "Executing instruction '${iname}' on '${package_dir}'"
return "$ERR_YPS_INSTRUCTION_FAIL"
}
done
)
}

#
# Install a ydotfile package
#
# Arguments:
# package_name string package name
# [os_name] string operating system
#
# Output:
# writes installed package name to the stdout
Expand All @@ -103,14 +160,15 @@ ydf::package_service::install_one_from_dir() {
# 0 on success, non-zero on error.
#
ydf::package_service::install_one() {
:
ydf::package_service::install_one_from_dir "$@"
}

#
# Install one or many ydotfile packages
#
# Arguments:
# packages_names string[] packages names
# [os_name] string operating system
#
# Output:
# writes installed packages names to the stdout
Expand All @@ -119,5 +177,5 @@ ydf::package_service::install_one() {
# 0 on success, non-zero on error.
#
ydf::package_service::install() {
ydf::package_service::install_one_from_dir "$@"
ydf::package_service::install_one "$@"
}
4 changes: 3 additions & 1 deletion src/usr/lib/ydf/errors.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ readonly ERR_INVAL_VALUE=66
readonly ERR_INVAL_ARG=67
readonly ERR_MISSING_ARG=68
readonly ERR_NO_DIR=69
readonly ERR_CHANGING_WORKDIR=69

## package-service
readonly ERR_YPS_INSTRUCTION_FAIL=80
readonly ERR_YPS_GENERAL=80
readonly ERR_YPS_INSTRUCTION_FAIL=81
4 changes: 2 additions & 2 deletions src/usr/lib/ydf/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ err() {
echo -e "ERROR> $*" >&2
}

inf() {
echo -e "INFO> $*"
ech() {
echo -e "$*"
}
3 changes: 3 additions & 0 deletions tests/fixtures/packages/0freedom-fail/postinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo postinstall
4 changes: 4 additions & 0 deletions tests/fixtures/packages/0freedom-fail/preinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

echo 'preinstall fails'
exit 1
3 changes: 3 additions & 0 deletions tests/fixtures/packages/1freedom/postinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo postinstall
3 changes: 3 additions & 0 deletions tests/fixtures/packages/1freedom/preinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo 'preinstall succeed'
1 change: 1 addition & 0 deletions tests/fixtures/packages/2preinstall/preinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo 'preinstall: preinstall succeed'
1 change: 1 addition & 0 deletions tests/fixtures/packages/3install/install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo '3install: install succeed'
1 change: 1 addition & 0 deletions tests/fixtures/packages/3install/preinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo '3install: preinstall succeed'
Empty file.
13 changes: 0 additions & 13 deletions tests/fixtures/packages/aws-cli-v2/aws-cli-v2.plugin.zsh

This file was deleted.

8 changes: 0 additions & 8 deletions tests/fixtures/packages/aws-cli-v2/post-install

This file was deleted.

2 changes: 2 additions & 0 deletions tests/test_helper_base.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

# VARIABLES

readonly TEST_FIXTURES_DIR="${TESTS_DIR}/fixtures"

# HELPER FUNCTIONS

ydf() {
Expand Down
2 changes: 1 addition & 1 deletion tests/usr/lib/ydf/components/package/test_helper.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# shellcheck source=../../test_helper.bash
. "${TESTS_DIR}/test_helper_base.bash"
. "${TESTS_DIR}/usr/lib/ydf/test_helper.bash"
# shellcheck source=../../../../../../src/usr/lib/ydf/components/package/ydf-package-entity.bash
. "${SRC_DIR}/usr/lib/ydf/components/package/ydf-package-entity.bash"
# shellcheck source=../../../../../../src/usr/lib/ydf/components/package/ydf-package-service.bash
Expand Down
9 changes: 0 additions & 9 deletions tests/usr/lib/ydf/components/package/ydf-package-command.bats

This file was deleted.

Loading

0 comments on commit da8fa28

Please sign in to comment.