Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added quiet flag #8

Merged
merged 1 commit into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions completion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ flags:
commands:
- name: dec
flags:
- help
- help
- name: enc
flags:
- help
- help
- name: view
flags:
- help
- help
- name: edit
flags:
- help
- help
- name: clean
flags:
- help
- help
- name: lint
flags:
- quiet
- name: template
flags:
- quiet
- name: install
flags:
- quiet
- name: upgrade
flags:
- quiet
- name: diff
flags:
- quiet
- name: kubeval
flags:
- quiet
45 changes: 39 additions & 6 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ EOF

helm_command_usage() {
cat <<EOF
helm secrets $1
helm secrets $1 [ --quiet | -q ]

This is a wrapper for "helm [command]". It will detect -f and
--values options, and decrypt any secrets*.yaml files before running "helm
Expand Down Expand Up @@ -295,14 +295,28 @@ clean() {
find "$basedir" -type f -name "secrets*${DEC_SUFFIX}" -exec rm -v {} \;
}

helm_wrapper_cleanup() {
if [ "${QUIET}" = "false" ]; then
echo >/dev/stderr
# shellcheck disable=SC2016
xargs -0 -n1 sh -c 'rm -f "$1" && echo "[helm-secrets] Removed: $1"' sh >/dev/stderr <"${decrypted_files}"
else
xargs -0 rm -f >/dev/stderr <"${decrypted_files}"
fi

rm -f "${decrypted_files}"
}

helm_wrapper() {
decrypted_files=$(mktemp)
QUIET=false
HELM_CMD_SET=false

argc=$#
j=0

#cleanup on-the-fly decrypted files
trap 'xargs -0 rm -fv > /dev/stderr < "${decrypted_files}"; rm -f "${decrypted_files}"' EXIT
trap helm_wrapper_cleanup EXIT

while [ $j -lt $argc ]; do
case "$1" in
Expand All @@ -319,16 +333,34 @@ helm_wrapper() {
if decrypt_helper "${file}"; then
file_dec="$(file_dec_name "${file}")"
set -- "$@" "$file_dec"
echo "[helm-secrets] Decrypt: ${file}" >/dev/stderr
printf '%s\0' "${file_dec}" >>"${decrypted_files}"

if [ "${QUIET}" = "false" ]; then
echo "[helm-secrets] Decrypt: ${file}" >/dev/stderr
fi
else
set -- "$@" "$file"
fi

shift
j=$((j + 1))
;;
-*)
if [ "${HELM_CMD_SET}" = "false" ]; then
case "$1" in
-q | --quiet)
QUIET=true
;;
*)
set -- "$@" "$1"
;;
esac
else
set -- "$@" "$1"
fi
;;
*)
HELM_CMD_SET=true
set -- "$@" "$1"
;;
esac
Expand All @@ -337,10 +369,11 @@ helm_wrapper() {
j=$((j + 1))
done

echo
if [ "${QUIET}" = "false" ]; then
echo >/dev/stderr
fi

"${HELM_BIN}" ${TILLER_HOST:+--host "$TILLER_HOST"} "$@"
echo
echo "[helm-secrets] Remove decrypted files:" >/dev/stderr
}

helm_command() {
Expand Down
51 changes: 43 additions & 8 deletions tests/7-lint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ load 'bats/extensions/bats-assert/load'

run helm secrets lint "tests/tmp/${CHART}" 2>&1
assert_success
refute_output --partial "[helm-secrets] Decrypt: tests/tmp/${CHART}/secrets.yaml"
assert_output --partial '1 chart(s) linted, 0 chart(s) failed'
assert_output --partial '[helm-secrets] Remove decrypted files:'
refute_output --partial "[helm-secrets] Removed: tests/tmp/${CHART}/secrets.yaml.dec"
assert [ ! -f "tests/tmp/${CHART}/secrets.yaml.dec" ]
}

@test "lint: helm lint w/ chart and secret file" {
Expand All @@ -36,9 +38,42 @@ load 'bats/extensions/bats-assert/load'

run helm secrets lint "tests/tmp/${CHART}" -f "tests/tmp/${CHART}/secrets.yaml" 2>&1
assert_success
assert_output --partial '[helm-secrets] Decrypt: tests/tmp/lint/secrets.yaml'
assert_output --partial '1 chart(s) linted, 0 chart(s) failed'
assert_output --partial '[helm-secrets] Remove decrypted files:'
assert_output --partial "[helm-secrets] Decrypt: tests/tmp/${CHART}/secrets.yaml"
assert_output --partial "1 chart(s) linted, 0 chart(s) failed"
assert_output --partial "[helm-secrets] Removed: tests/tmp/${CHART}/secrets.yaml.dec"
assert [ ! -f "tests/tmp/${CHART}/secrets.yaml.dec" ]
}

@test "lint: helm lint w/ chart and secret file and q flag" {
CHART=lint

mkdir -p "tests/tmp/${CHART}" >&2
printf 'podAnnotations:\n secret: value' > "tests/tmp/${CHART}/secrets.yaml"

create_chart "${CHART}"

run helm secrets -q lint "tests/tmp/${CHART}" -f "tests/tmp/${CHART}/secrets.yaml" 2>&1
assert_success
refute_output --partial "[helm-secrets] Decrypt: tests/tmp/${CHART}/secrets.yaml"
assert_output --partial "1 chart(s) linted, 0 chart(s) failed"
refute_output --partial "[helm-secrets] Removed: tests/tmp/${CHART}/secrets.yaml.dec"
assert [ ! -f "tests/tmp/${CHART}/secrets.yaml.dec" ]
}

@test "lint: helm lint w/ chart and secret file and quiet flag" {
CHART=lint

mkdir -p "tests/tmp/${CHART}" >&2
printf 'podAnnotations:\n secret: value' > "tests/tmp/${CHART}/secrets.yaml"

create_chart "${CHART}"

run helm secrets --quiet lint "tests/tmp/${CHART}" -f "tests/tmp/${CHART}/secrets.yaml" 2>&1
assert_success
refute_output --partial "[helm-secrets] Decrypt: tests/tmp/${CHART}/secrets.yaml"
assert_output --partial "1 chart(s) linted, 0 chart(s) failed"
refute_output --partial "[helm-secrets] Removed: tests/tmp/${CHART}/secrets.yaml.dec"
assert [ ! -f "tests/tmp/${CHART}/secrets.yaml.dec" ]
}

@test "lint: helm lint w/ chart and invalid yaml" {
Expand All @@ -51,8 +86,8 @@ load 'bats/extensions/bats-assert/load'

run helm secrets lint "tests/tmp/${CHART}" -f "tests/tmp/${CHART}/secrets.yaml" 2>&1
assert_failure
assert_output --partial '[helm-secrets] Decrypt: tests/tmp/lint/secrets.yaml'
assert_output --partial 'Error: 1 chart(s) linted, 1 chart(s) failed'
# @TOOD: Run cleanup if errors appears
# assert_output --partial '[helm-secrets] Remove decrypted files:'
assert_output --partial "[helm-secrets] Decrypt: tests/tmp/${CHART}/secrets.yaml"
assert_output --partial "Error: 1 chart(s) linted, 1 chart(s) failed"
assert_output --partial "[helm-secrets] Removed: tests/tmp/${CHART}/secrets.yaml.dec"
assert [ ! -f "tests/tmp/${CHART}/secrets.yaml.dec" ]
}
11 changes: 11 additions & 0 deletions tests/99-cleanup.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bats

load helper
load 'bats/extensions/bats-support/load'
load 'bats/extensions/bats-assert/load'

@test "Cleanup test environment" {
# Reset test environment
run git checkout HEAD -- tests/assets/helm_vars/
assert_success
}
2 changes: 1 addition & 1 deletion tests/assets/mock-editor/editor.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env sh

echo "hello: world" > "$1"
echo "hello: world" >"$1"
22 changes: 11 additions & 11 deletions tests/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ export GIT_ROOT
TEST_HOME="${GIT_ROOT}/tests/.home"
export TEST_HOME

helm () {
helm() {
env HOME="${TEST_HOME}" helm "$@"
}

gpg () {
gpg() {
env HOME="${TEST_HOME}" gpg "$@"
}

create_chart () {
run helm create "tests/tmp/$1"
assert_success
assert_output --partial "Creating tests/tmp/$1"
create_chart() {
run helm create "tests/tmp/$1"
assert_success
assert_output --partial "Creating tests/tmp/$1"

if [ -f tests/tmp/lint/secrets.yaml ]; then
cp "tests/assets/helm_vars/.sops.yaml" "tests/tmp/$1/" >&2
if [ -f tests/tmp/lint/secrets.yaml ]; then
cp "tests/assets/helm_vars/.sops.yaml" "tests/tmp/$1/" >&2

run helm secrets enc tests/tmp/lint/secrets.yaml
assert_success
fi
run helm secrets enc tests/tmp/lint/secrets.yaml
assert_success
fi
}