Skip to content

Commit

Permalink
feat: implement 'edit' command
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Bouyoud <jBouyoud@users.noreply.github.com>
  • Loading branch information
jBouyoud committed Nov 11, 2020
1 parent 772c0c3 commit 52b36ae
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 2 deletions.
94 changes: 94 additions & 0 deletions scripts/commands/edit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env sh

set -eu

edit_usage() {
cat <<EOF
helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...
Edit an existing NAME configuration scheme
Available Commands:
- append FILE-URI...
Add new FILE-URIs to the end of NAME configuration scheme
- insert-at INDEX FILE-URI...
Add new FILE-URIs at INDEX of NAME configuration scheme
- replace INDEX FILE-URI
Replace an existing file_uri at INDEX by FILE-URI of NAME configuration scheme
$(file_uri_substitution_help)
EOF
}

_edit_check_index() {
count="$(repository_scheme_file_uri_count "${1}")"
if [ "${2}" -lt 0 ]; then
log_error "[edit] INDEX must be in [O;${count}["
exit 2
elif [ "${2}" -ge "${count}" ]; then
log_error "[edit] INDEX must be in [O;${count}["
exit 2
fi
}

edit() {
if [ $# -lt 2 ]; then
edit_usage
exit 1
fi
scheme_name="${1}"
subcommand="${2}"
shift 2

if ! repository_scheme_exists "${scheme_name}"; then
log_error "[edit] Scheme '${scheme_name}' doesn't exists"
exit 2
fi

case "${subcommand}" in
append)
if [ $# -lt 1 ]; then
edit_usage
exit 1
fi
for file_uri in "$@"; do
repository_scheme_append_file_uri "${scheme_name}" "${file_uri}"
done
;;
insert-at)
if [ $# -lt 2 ]; then
edit_usage
exit 1
fi
idx="${1}"
_edit_check_index "${scheme_name}" "${idx}"

shift
for file_uri in "$@"; do
repository_scheme_insert_file_uri "${scheme_name}" "${idx}" "${file_uri}"
idx=$((idx + 1))
done
;;
replace)
if [ $# -ne 2 ]; then
edit_usage
exit 1
fi
idx="${1}"
file_uri="${2}"

_edit_check_index "${scheme_name}" "${idx}"
repository_scheme_replace_file_uri "${scheme_name}" "${idx}" "${file_uri}"
;;
*)
edit_usage
exit 1
;;
esac
}
38 changes: 38 additions & 0 deletions scripts/lib/repository.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ repository_delete_scheme() {
rm -f "$(_repository_scheme_file "${1}")"
}

repository_scheme_file_uri_count() {
repository_view_scheme "${1}" | wc -l | awk '{print $1}'
}

repository_scheme_append_file_uri() {
scheme_name="${1}"
file_uri="${2}"
Expand All @@ -45,3 +49,37 @@ repository_scheme_append_file_uri() {

echo "${file_uri_scheme}" >>"${scheme_file}"
}

_sed_i() {
# MacOS syntax is different for in-place
if [ "$(uname)" = "Darwin" ]; then
sed -i "" "$@"
else
sed -i "$@"
fi
}

repository_scheme_insert_file_uri() {
scheme_name="${1}"
index="${2}"
file_uri="${3}"

log_info "[registry] Add file uri '${file_uri}' to scheme '${scheme_name}'"
file_uri_scheme="$(file_uri_subst "${file_uri}")"
scheme_file="$(_repository_scheme_file "${scheme_name}")"

sed "${index}s/\(.*\)/\1#${file_uri}/" "${scheme_file}" | tr '#' '\n' >"${scheme_file}.tmp"
mv "${scheme_file}.tmp" "${scheme_file}"
}

repository_scheme_replace_file_uri() {
scheme_name="${1}"
index="${2}"
file_uri="${3}"

log_info "[registry] Add file uri '${file_uri}' to scheme '${scheme_name}'"
file_uri_scheme="$(file_uri_subst "${file_uri}")"
scheme_file="$(_repository_scheme_file "${scheme_name}")"

_sed_i "$((index + 1))s/.*/${file_uri}/" "${scheme_file}"
}
221 changes: 219 additions & 2 deletions tests/unit/edit.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,226 @@ load '../lib/helper'
load '../bats/extensions/bats-support/load'
load '../bats/extensions/bats-assert/load'
load '../bats/extensions/bats-file/load'
load '../lib/asserts-config-repository'

@test "edit:" {
@test "edit: show help" {
run helm config-scheme edit

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS'
}

@test "edit -h: show help" {
run helm config-scheme edit -h

assert_success
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS'
}

@test "edit --help: show help" {
run helm config-scheme edit --help

assert_success
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS'
}

@test "edit help: show help" {
run helm config-scheme edit help

assert_success
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit whatever: show help" {
run helm config-scheme edit whatever

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit whatever append: error" {
helm config-scheme add scheme0 z.yaml

run helm config-scheme edit whatever append

assert_failure 2
assert_output --partial 'Error: Not yet implemented.'
assert_output --partial "[config-scheme][edit] Scheme 'whatever' doesn't exists"
}

@test "edit NAME append: error" {
helm config-scheme add scheme1 a.yaml

run helm config-scheme edit scheme1 append

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit NAME insert-at: error" {
helm config-scheme add scheme1 a.yaml

run helm config-scheme edit scheme1 insert-at

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit NAME replace: error" {
helm config-scheme add scheme1 a.yaml

run helm config-scheme edit scheme1 replace

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit NAME append uri: append one uri to scheme" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 append test.yaml

assert_success
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme0" "z.yaml"
assert_config_repository_scheme "scheme2" "y.yaml"
assert_config_repository_scheme "scheme1" $'a.yaml\nb.yaml\nc.yaml\ntest.yaml'
}

@test "edit NAME append uris: append uris to scheme" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 append test.yaml test2.yaml test3.yaml

assert_success
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'test2.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'test3.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme0" "z.yaml"
assert_config_repository_scheme "scheme2" "y.yaml"
assert_config_repository_scheme "scheme1" $'a.yaml\nb.yaml\nc.yaml\ntest.yaml\ntest2.yaml\ntest3.yaml'
}

@test "edit NAME insert-at idx: error" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 insert-at 0

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit NAME insert-at -1 uri: error" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 insert-at -1 test.yaml

assert_failure 2
assert_output --partial '[config-scheme][edit] INDEX must be in [O;3['
}

@test "edit NAME insert-at n+1 uri: error" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 insert-at 4 test.yaml

assert_failure 2
assert_output --partial '[config-scheme][edit] INDEX must be in [O;3['
}

@test "edit NAME insert-at idx uri: success" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 insert-at 1 test.yaml

assert_success
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme0" "z.yaml"
assert_config_repository_scheme "scheme2" "y.yaml"
assert_config_repository_scheme "scheme1" $'a.yaml\ntest.yaml\nb.yaml\nc.yaml'
}

@test "edit NAME insert-at idx uris: success" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 insert-at 1 test.yaml test2.yaml test3.yaml

assert_success
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'test2.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'test3.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme0" "z.yaml"
assert_config_repository_scheme "scheme2" "y.yaml"
assert_config_repository_scheme "scheme1" $'a.yaml\ntest.yaml\ntest2.yaml\ntest3.yaml\nb.yaml\nc.yaml'
}

@test "edit NAME replace idx: error" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 replace 0

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

@test "edit NAME replace -1 uri: error" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 replace -1 test.yaml

assert_failure 2
assert_output --partial '[config-scheme][edit] INDEX must be in [O;3['
}

@test "edit NAME replace n+1 uri: error" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 replace 4 test.yaml

assert_failure 2
assert_output --partial '[config-scheme][edit] INDEX must be in [O;3['
}

@test "edit NAME replace idx uri: success" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 replace 1 test.yaml

assert_success
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme0" "z.yaml"
assert_config_repository_scheme "scheme2" "y.yaml"
assert_config_repository_scheme "scheme1" $'a.yaml\ntest.yaml\nc.yaml'
}

@test "edit NAME replace idx uris: success" {
helm config-scheme add scheme0 z.yaml
helm config-scheme add scheme1 a.yaml b.yaml c.yaml
helm config-scheme add scheme2 y.yaml

run helm config-scheme edit scheme1 replace 1 test.yaml test2.yaml test3.yaml

assert_failure 1
assert_output --partial 'helm config-scheme edit NAME SUB-COMMAND SUB-COMMAND-ARGS...'
}

0 comments on commit 52b36ae

Please sign in to comment.