Skip to content

Commit

Permalink
feat(package): add a command to list packages
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
yunielrc committed Sep 28, 2023
1 parent 8c8eb7b commit 02ace92
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/usr/lib/ydf/components/package/ydf-package-command.bash
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ HELPMSG
# Install one or more ydotfile packages
#
# Flags:
# -h | --help Show help
# -h, --help Show help
#
# Options:
# --os Operating system
Expand Down Expand Up @@ -137,6 +137,71 @@ ydf::package_command::__install() {
ydf::package_service::install "$packages" "$os" "$packages_dir"
}

#
# Show help for __list command
#
# Output:
# Writes the help to the stdout
#
ydf::package_command::__list_help() {
cat <<-HELPMSG
Usage:
${__YDF_SCRIPT_NAME} package list [OPTIONS]
List packages in the packages directory
Flags:
-h, --help Show this help
Options:
--packages-dir Packages directory
HELPMSG
}

#
# List packages in the packages directory
#
# Flags:
# -h, --help Show help
#
# Options:
# --packages-dir Packages directory
#
# Output:
# write packages_names (list) to stdout
#
# Returns:
# 0 on success, non-zero on error.
#
ydf::package_command::__list() {
# shellcheck disable=SC2155
local packages_dir="$(ydf::package_service::get_packages_dir)"

while [[ $# -gt 0 ]]; do
case "$1" in
# flags
-h | --help)
ydf::package_command::__list_help
return 0
;;
# options
--packages-dir)
readonly packages_dir="${2:-}"
# validate argument
if [[ -z "$packages_dir" ]]; then
err "No packages dir specified\n"
ydf::package_command::__list_help
return "$ERR_MISSING_ARG"
fi
shift 2
;;
esac
done

ydf::package_service::list "$packages_dir"
}

ydf::package_command::__help() {
cat <<-HELPMSG
Usage:
Expand All @@ -149,6 +214,7 @@ Flags:
Commands:
install install packages
list list packages
Run '${__YDF_SCRIPT_NAME} package --help' for more information on a command.
HELPMSG
Expand All @@ -168,6 +234,11 @@ ydf::package_service::run_cmd() {
ydf::package_command::__install "$@"
return $?
;;
list)
shift
ydf::package_command::__list "$@"
return $?
;;
*)
err "Invalid command: ${1}\n"
ydf::package_command::__help
Expand Down
34 changes: 34 additions & 0 deletions src/usr/lib/ydf/components/package/ydf-package-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,37 @@ ydf::package_service::install() {

msg "> DONE. INSTALLED ${#_elements_arr[*]} packages"
}

#
# List packages in the packages directory
#
# Arguments:
# [packages_dir] string packages dir
#
# Output:
# writes packages_names (list) to the stdout
#
# Returns:
# 0 on success, non-zero on error.
#
ydf::package_service::list() {
local -r packages_dir="${1:-"$(ydf::package_service::get_packages_dir)"}"

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

# show only non hidden directories
(
cd "$packages_dir" 2>/dev/null || {
err "Changing current directory to '${packages_dir}'"
return "$ERR_CHANGING_WORKDIR"
}

find . -maxdepth 1 -type d -printf '%f\n' |
grep -Ev '^\.' |
sort
)
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
50 changes: 50 additions & 0 deletions tests/usr/lib/ydf/components/package/ydf-package-command.f.bats
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# shellcheck disable=SC2153
load test_helper

setup() {
Expand Down Expand Up @@ -950,3 +951,52 @@ preinstall: preinstall succeed"
assert_success
assert_output --partial "exa/"
}

# Tests for ydf package list
@test "ydf package list, Should show help" {

for p in -h --help; do
run ydf package list $p

assert_success
assert_output --partial 'ydf package list [OPTIONS]'
done
}

@test "ydf package list --packages-dir, Should fail with missing argument packages-dir" {

run ydf package list --packages-dir

assert_failure
assert_output --partial 'ERROR> No packages dir specified'
}

@test "ydf package list --packages-dir, Should fail If packages-dir doesn't exist" {

run ydf package list --packages-dir 'asdfadf324325623afddwg11'

assert_failure
assert_output "ERROR> Packages directory 'asdfadf324325623afddwg11' doesn't exist"
}

@test "ydf package list, Should list packages" {

export E_YDF_PACKAGE_SERVICE_PACKAGES_DIR="${TEST_FIXTURES_DIR}/packages3"

run ydf package list

assert_success
assert_output "pkg1
pkg2
pkg3"
}

@test "ydf package list --packages-dir ..., Should list packages with" {

run ydf package list --packages-dir "${TEST_FIXTURES_DIR}/packages3"

assert_success
assert_output "pkg1
pkg2
pkg3"
}
38 changes: 38 additions & 0 deletions tests/usr/lib/ydf/components/package/ydf-package-service.i.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1418,3 +1418,41 @@ install: HOME: ${TEST_HOME_DIR}"
assert_success
assert_output --partial "exa/"
}

# Tests for ydf::package_service::list()
@test "ydf::package_service::list() Should fail If packages_dir doesn't exist" {
local -r _packages_dir="adfadsf23423098587209"

run ydf::package_service::list "$_packages_dir"

assert_failure
assert_output "ERROR> Packages directory '${_packages_dir}' doesn't exist"
}

@test "ydf::package_service::list() Should fail If change dir fails" {
local -r _packages_dir="$(mktemp -d)"

cd() {
if [[ "$*" == "$_packages_dir" ]]; then
return 1
fi
command cd "$@"
}

run ydf::package_service::list "$_packages_dir"

assert_failure
assert_output "ERROR> Changing current directory to '${_packages_dir}'"
}

@test "ydf::package_service::list() Should succeed" {

local -r _packages_dir="${TEST_FIXTURES_DIR}/packages3"

run ydf::package_service::list "$_packages_dir"

assert_success
assert_output "pkg1
pkg2
pkg3"
}

0 comments on commit 02ace92

Please sign in to comment.