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

Add GitHub Actions export only shell #910

Merged
merged 3 commits into from Apr 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/go.yml
Expand Up @@ -42,3 +42,19 @@ jobs:
GO111MODULE: on
run: make test-stdlib test-bash

- name: GitHub Actions Env Test Setup
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to create a separate workflow file for this test but decided to just append the tests here instead. I can split it to different files if that makes more sense.

Should I "port" this test to do something when running locally on a dev machine? It feels kind of odd to only test some functionality in CI vs locally first.

# 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
18 changes: 10 additions & 8 deletions internal/cmd/shell.go
Expand Up @@ -44,20 +44,22 @@ func DetectShell(target string) Shell {
switch target {
case "bash":
return Bash
case "zsh":
return Zsh
case "elvish":
return Elvish
case "fish":
return Fish
case "gha":
return GitHubActions
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
Expand Down
53 changes: 53 additions & 0 deletions 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, "")
}
1 change: 1 addition & 0 deletions 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}"