Skip to content

Commit

Permalink
feat: implement 'add' 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 910f6cb commit 3e787cc
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/tests/.tmp/
/tests/coverage/
/node_modules/
/repository/
34 changes: 34 additions & 0 deletions scripts/commands/add.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env sh

set -eu

add_usage() {
cat <<EOF
helm config-scheme add NAME FILE-URI...
Create a new configuration scheme NAME with all specified FILE-URI
$(file_uri_substitution_help)
EOF
}

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

if repository_scheme_exists "${scheme_name}"; then
log_error "[add] Scheme '${scheme_name}' already exists"
exit 2
fi

repository_create_scheme "${scheme_name}"

for file_uri in "$@"; do
repository_scheme_append_file_uri "${scheme_name}" "${file_uri}"
done
}
38 changes: 38 additions & 0 deletions scripts/lib/file_uri_substitution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env sh

set -eu

file_uri_substitution_help() {
cat <<EOF
FILE-URI specify values in a YAML file or a URL
File uri support some substitutions:
| variable | substituted by | default value |
| ------------- | ---------------------------------------------------- | ------------- |
| {{namespace}} | Helm command namespace | unknown |
| {{release}} | Helm release name | RELEASE-NAME |
| {{chart}} | Helm chart name | CHART_NAME |
| {{env}} | Replaced by the environment variable value for "env" | unknown |
Example:
- local/file.yaml
- https://my.repo.com/config/{{namespace}}/file.yaml
- git+https://github.com/user/repo/{{chart}}.yaml?ref=master
- secrets://local/{{release}}.yaml
- local/{{my_env}}.yaml
EOF
}

file_uri_subst() {
file_uri="${1}"

if [ -z "${file_uri}" ]; then
log_error "No file uri specified"
exit 1
fi

echo "${file_uri}" | sed 's/{{/${/g' | sed 's/}}/}/g'
}
35 changes: 35 additions & 0 deletions scripts/lib/repository.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env sh

set -eu

# Configuration directly for all scheme
CONFIG_REPOSITORY="${HELM_CONFIG_SCHEME_REPOSITORY:-${HELM_PLUGIN_DIR}/repository}"

# Make sure that directory exists
mkdir -p "${CONFIG_REPOSITORY}"

_repository_scheme_file() {
echo "${CONFIG_REPOSITORY}/${1}"
}

repository_scheme_exists() {
test -f "$(_repository_scheme_file "${1}")"
}

repository_create_scheme() {
scheme_name="${1}"

log_info "[registry] Creating '${scheme_name}'"
printf '' >"$(_repository_scheme_file "${scheme_name}")"
}

repository_scheme_append_file_uri() {
scheme_name="${1}"
file_uri="${2}"

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}")"

echo "${file_uri_scheme}" >>"${scheme_file}"
}
6 changes: 6 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ HELM_BIN="${HELM_BIN:-helm}"
# shellcheck source=scripts/lib/is_help.sh
. "${SCRIPT_DIR}/lib/is_help.sh"

# shellcheck source=scripts/lib/file_uri_substitution.sh
. "${SCRIPT_DIR}/lib/file_uri_substitution.sh"

# shellcheck source=scripts/lib/repository.sh
. "${SCRIPT_DIR}/lib/repository.sh"

_trap_hook() {
true
}
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/asserts-config-repository.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

assert_config_repository_scheme() {
file="${HELM_CONFIG_SCHEME_REPOSITORY}/${1}"

assert_file_exist "${file}"
assert_equal "$(cat "${file}")" "${2}"
}
4 changes: 0 additions & 4 deletions tests/lib/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ setup() {

# Use a temporary config-repository reseted after every suite
export HELM_CONFIG_SCHEME_REPOSITORY="${TEST_TEMP_DIR}/config-repository"
export HELM_CONFIG_SCHEME_TMP_DIR="${TEST_TEMP_DIR}/config-tmp"

mkdir "${TEST_TEMP_DIR}/chart"
mkdir "${HELM_CONFIG_SCHEME_TMP_DIR}"

# shellcheck disable=SC2034
XDG_DATA_HOME="${HOME}"
Expand Down
94 changes: 92 additions & 2 deletions tests/unit/add.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,99 @@ 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 "add:" {
@test "add: show help" {
run helm config-scheme add

assert_failure 1
assert_output --partial 'helm config-scheme add NAME FILE-URI...'
}

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

assert_success
assert_output --partial 'helm config-scheme add NAME FILE-URI...'
}

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

assert_success
assert_output --partial 'helm config-scheme add NAME FILE-URI...'
}

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

assert_success
assert_output --partial 'helm config-scheme add NAME FILE-URI...'
}

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

assert_failure 1
assert_output --partial 'helm config-scheme add NAME FILE-URI...'
}

@test "add scheme uri: create scheme" {
run helm config-scheme add scheme1 test.yaml

assert_success
assert_output --partial "[config-scheme][registry] Creating 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme1" "test.yaml"
}

@test "add scheme uri: fail if scheme already exists" {
helm config-scheme add scheme1 ori.yaml

run helm config-scheme add scheme1 test.yaml

assert_failure 2
assert_output --partial 'Error: Not yet implemented.'
assert_output --partial "[config-scheme][add] Scheme 'scheme1' already exists"
assert_config_repository_scheme "scheme1" "ori.yaml"
}

@test "add scheme uris: create scheme" {
run helm config-scheme add scheme1 test.yaml config/test.yaml config/sub/test.yaml

assert_success
assert_output --partial "[config-scheme][registry] Creating 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'test.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'config/test.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'config/sub/test.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme1" $'test.yaml\nconfig/test.yaml\nconfig/sub/test.yaml'
}

@test "add scheme uri substitution: create scheme" {
if is_windows; then
skip
fi

run helm config-scheme add scheme1 "{{namespace}}.yaml"

assert_success
assert_output --partial "[config-scheme][registry] Creating 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri '{{namespace}}.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme1" "\${namespace}.yaml"
}

@test "add scheme uris substitution: create scheme" {
if is_windows; then
skip
fi

run helm config-scheme add scheme1 local/file.yaml "https://my.repo.com/config/{{namespace}}/file.yaml" "git+https://github.com/user/repo/{{chart}}.yaml?ref=master" "secrets://local/{{release}}.yaml" "local/{{my_env}}.yaml"

assert_success
assert_output --partial "[config-scheme][registry] Creating 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'local/file.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'https://my.repo.com/config/{{namespace}}/file.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'git+https://github.com/user/repo/{{chart}}.yaml?ref=master' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'secrets://local/{{release}}.yaml' to scheme 'scheme1'"
assert_output --partial "[config-scheme][registry] Add file uri 'local/{{my_env}}.yaml' to scheme 'scheme1'"
assert_config_repository_scheme "scheme1" $'local/file.yaml\nhttps://my.repo.com/config/${namespace}/file.yaml\ngit+https://github.com/user/repo/${chart}.yaml?ref=master\nsecrets://local/${release}.yaml\nlocal/${my_env}.yaml'
}

0 comments on commit 3e787cc

Please sign in to comment.