Skip to content

Commit

Permalink
Add gha shell for GitHub Actions
Browse files Browse the repository at this point in the history
GHA has both simple and multi-line env var modes, I decided to always use the
multiline mode for ease of implementation.
  • Loading branch information
mmlb committed Mar 22, 2022
1 parent 743e2c4 commit d488237
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/go.yml
Expand Up @@ -42,3 +42,18 @@ jobs:
GO111MODULE: on
run: make test-stdlib test-bash

- name: GitHub Actions Env Test Setup
id: test-gha-setup
run: |
topdir=$PWD
cd test/scenarios/github-actions/
"$topdir/direnv" allow
"$topdir/direnv" export gha >> "$GITHUB_ENV"
base64 -w0 <<<"$DIRENV_GITHUB_ACTIONS_TEST_EXPORT" | tee >"$topdir/DIRENV_GITHUB_ACTIONS_TEST_EXPORT.want"
- name: GitHub Actions Env Test Verification
run: |
[[ -z ${DIRENV_GITHUB_ACTIONS_TEST_EXPORT:-} ]] && echo "DIRENV_GITHUB_ACTIONS_TEST_EXPORT is unset or empty" >&2 && exit 1
base64 -w0 <<<"$DIRENV_GITHUB_ACTIONS_TEST_EXPORT" | tee >DIRENV_GITHUBACTIONS_TEST_EXPORT_BASE64.got
diff -u DIRENV_GITHUBACTIONS_TEST_EXPORT_BASE64.want DIRENV_GITHUBACTIONS_TEST_EXPORT_BASE64.got
rm -f DIRENV_GITHUBACTIONS_TEST_EXPORT_BASE64.*
2 changes: 2 additions & 0 deletions internal/cmd/shell.go
Expand Up @@ -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":
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 DIRENV_GITHUB_ACTIONS_TEST_EXPORT=$SRANDOM$SRANDOM$'\n'$SRANDOM$SRANDOM

0 comments on commit d488237

Please sign in to comment.