From 12c7ef15506c06e21cdbfb5a9d4a7398921b2970 Mon Sep 17 00:00:00 2001 From: Manuel Mendez Date: Mon, 21 Mar 2022 19:51:00 -0400 Subject: [PATCH 1/3] Sort shells in DetectShell --- internal/cmd/shell.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index d6d81b8c4..60d0251be 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -44,20 +44,20 @@ func DetectShell(target string) Shell { switch target { case "bash": return Bash - case "zsh": - return Zsh + case "elvish": + return Elvish case "fish": return Fish case "gzenv": return GzEnv - case "vim": - return Vim - case "tcsh": - return Tcsh case "json": return JSON - case "elvish": - return Elvish + case "tcsh": + return Tcsh + case "vim": + return Vim + case "zsh": + return Zsh } return nil From f5cfc1ab461ae863710ee11712ea797254a10bc2 Mon Sep 17 00:00:00 2001 From: Manuel Mendez Date: Mon, 21 Mar 2022 19:51:54 -0400 Subject: [PATCH 2/3] Add gha shell for GitHub Actions GHA has both simple and multi-line env var modes, I decided to always use the multiline mode for ease of implementation. --- internal/cmd/shell.go | 2 ++ internal/cmd/shell_gha.go | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 internal/cmd/shell_gha.go diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index 60d0251be..62db86daf 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -48,6 +48,8 @@ func DetectShell(target string) Shell { return Elvish case "fish": return Fish + case "gha": + return GitHubActions case "gzenv": return GzEnv case "json": diff --git a/internal/cmd/shell_gha.go b/internal/cmd/shell_gha.go new file mode 100644 index 000000000..04c4c2e4d --- /dev/null +++ b/internal/cmd/shell_gha.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "fmt" + "strings" +) + +type gha struct{} + +// GitHubActions shell instance +var GitHubActions Shell = gha{} + +func (sh gha) Hook() (string, error) { + return "", fmt.Errorf("Hook not implemented for GitHub Actions shell") +} + +func (sh gha) Export(e ShellExport) string { + var b strings.Builder + for key, value := range e { + if value == nil { + sh.unset(&b, key) + } else { + sh.export(&b, key, *value) + } + } + return b.String() +} + +const ghaDelim = "DIRENV_GITHUB_ACTIONS_EOV\n" + +func (sh gha) Dump(env Env) string { + var b strings.Builder + + for key, value := range env { + sh.export(&b, key, value) + } + return b.String() +} + +func (sh gha) export(b *strings.Builder, key, value string) { + b.WriteString(key) + b.WriteString("<<") + b.WriteString(ghaDelim) + b.WriteString(value) + if value != "" && !strings.HasSuffix(value, "\n") { + b.WriteByte('\n') + } + b.WriteString(ghaDelim) +} + +func (sh gha) unset(b *strings.Builder, key string) { + sh.export(b, key, "") +} From 364e07a7a05cf5ead1915e9df85d881dc6f3d615 Mon Sep 17 00:00:00 2001 From: Manuel Mendez Date: Tue, 29 Mar 2022 11:37:24 -0400 Subject: [PATCH 3/3] Add test for export gha Only doing this in CI because it doesn't seem worth it to mock GHA to test this, especially conserding `act` already does a good job of mocking GHA. --- .github/workflows/go.yml | 16 ++++++++++++++++ test/scenarios/github-actions/.envrc | 1 + 2 files changed, 17 insertions(+) create mode 100644 test/scenarios/github-actions/.envrc diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d1a7a0e5e..441a47370 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -42,3 +42,19 @@ jobs: GO111MODULE: on run: make test-stdlib test-bash + - name: GitHub Actions Env Test Setup + # FIXME: make this work on Windows as well + if: runner.os != 'Windows' + run: | + cd test/scenarios/github-actions/ + ../../../direnv allow + ../../../direnv export gha >> "$GITHUB_ENV" + + - name: GitHub Actions Env Test Verification + # FIXME: make this work on Windows as well + if: runner.os != 'Windows' + run: | + [[ -z ${TEST_EXPORT_DIRENV_GITHUB_ACTIONS:-} ]] && echo "TEST_EXPORT_DIRENV_GITHUB_ACTIONS is unset or empty" >&2 && exit 1 + tee TEST_EXPORT_DIRENV_GITHUB_ACTIONS.got <<<"$TEST_EXPORT_DIRENV_GITHUB_ACTIONS" + echo "${GITHUB_SHA}"$'\n'"${GITHUB_RUN_ID}"$'\n'"${GITHUB_RUN_NUMBER}" | tee TEST_EXPORT_DIRENV_GITHUB_ACTIONS.want + diff -u TEST_EXPORT_DIRENV_GITHUB_ACTIONS.want TEST_EXPORT_DIRENV_GITHUB_ACTIONS.got diff --git a/test/scenarios/github-actions/.envrc b/test/scenarios/github-actions/.envrc new file mode 100644 index 000000000..205409b06 --- /dev/null +++ b/test/scenarios/github-actions/.envrc @@ -0,0 +1 @@ +export TEST_EXPORT_DIRENV_GITHUB_ACTIONS="${GITHUB_SHA:-MISSING_GITHUB_SHA}"$'\n'"${GITHUB_RUN_ID:MISSING_GITHUB_RUN_ID}"$'\n'"${GITHUB_RUN_NUMBER:-MISSING_GITHUB_RUN_NUMBER}"