Skip to content

Commit

Permalink
parse env files with weird format
Browse files Browse the repository at this point in the history
  • Loading branch information
frantjc committed Apr 2, 2023
1 parent 06ef4c8 commit 4381f91
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 110 deletions.
2 changes: 1 addition & 1 deletion command/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewUse() *cobra.Command {
if outputs {
defer func() {
if outputs := globalContext.StepsContext[id].Outputs; len(outputs) > 0 {
_ = json.NewEncoder(cmd.OutOrStdout()).Encode(globalContext.StepsContext[id].Outputs)
_ = json.NewEncoder(cmd.OutOrStdout()).Encode(outputs)
}
}()
}
Expand Down
77 changes: 0 additions & 77 deletions envconv/dot_env.go

This file was deleted.

7 changes: 3 additions & 4 deletions forgeactions/env_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/frantjc/forge"
"github.com/frantjc/forge/envconv"
"github.com/frantjc/forge/githubactions"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -44,21 +43,21 @@ func (m *Mapping) SetGlobalContextFromEnvFiles(ctx context.Context, globalContex
case tar.TypeReg:
switch {
case strings.HasSuffix(m.GitHubOutputPath, header.Name):
outputs, err := envconv.MapFromReader(io.LimitReader(r, header.Size))
outputs, err := githubactions.ParseEnvFile(r)
if err != nil {
errs = append(errs, err)
continue
}

if _, ok := globalContext.StepsContext[step]; !ok {
if stepContext, ok := globalContext.StepsContext[step]; !ok || stepContext.Outputs == nil {
globalContext.StepsContext[step] = &githubactions.StepContext{
Outputs: outputs,
}
} else {
maps.Copy(globalContext.StepsContext[step].Outputs, outputs)
}
case strings.HasSuffix(m.GitHubStatePath, header.Name):
outputs, err := envconv.MapFromReader(io.LimitReader(r, header.Size))
outputs, err := githubactions.ParseEnvFile(r)
if err != nil {
errs = append(errs, err)
continue
Expand Down
2 changes: 2 additions & 0 deletions forgeactions/workflow_command_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func (w *WorkflowCommandWriter) Callback(wc *githubactions.WorkflowCommand) []by
w.GlobalContext.StepsContext[w.ID] = &githubactions.StepContext{
Outputs: map[string]string{},
}
} else if w.GlobalContext.StepsContext[w.ID].Outputs == nil {
w.GlobalContext.StepsContext[w.ID].Outputs = make(map[string]string)
}

w.GlobalContext.StepsContext[w.ID].Outputs[wc.GetName()] = wc.Value
Expand Down
60 changes: 60 additions & 0 deletions githubactions/env_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package githubactions

import (
"bufio"
"fmt"
"io"
"regexp"
"strings"
)

var (
valueDelimiter = `ghadelimiter_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`
valueDelimiterR = regexp.MustCompile(valueDelimiter)

keyDelimiter = `(\w+)<<` + valueDelimiter
keyDelimiterR = regexp.MustCompile(keyDelimiter)
)

func ParseEnvFile(r io.Reader) (map[string]string, error) {
var (
values = make(map[string]string)
scanner = bufio.NewScanner(r)
)
for scanner.Scan() {
line0 := scanner.Text()
if strings.HasPrefix(line0, "# ") || strings.TrimSpace(line0) == "" {
continue
} else if matches := keyDelimiterR.FindStringSubmatch(line0); len(matches) == 2 {
fmt.Println(line0, matches)
if scanner.Scan() {
line1 := scanner.Text()
if scanner.Scan() {
line2 := scanner.Text()
if valueDelimiterR.MatchString(line2) {
values[matches[1]] = strings.SplitN(line1, " #", 2)[0]
continue
}
}
}

return nil, fmt.Errorf("invalid multiline environment file entry")
} else if matches := strings.SplitN(line0, "=", 2); len(matches) == 2 {
values[matches[0]] = trimValue(matches[1])
} else {
return nil, fmt.Errorf("parse environment file line: %s", line0)
}
}

return values, scanner.Err()
}

func trimValue(value string) string {
if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
return strings.Trim(value, `"`)
} else if strings.HasPrefix(value, `'`) && strings.HasSuffix(value, `'`) {
return strings.Trim(value, `'`)
}

return value
}
45 changes: 45 additions & 0 deletions githubactions/env_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package githubactions_test

import (
"bytes"
"testing"

"github.com/frantjc/forge/githubactions"
"github.com/google/uuid"
)

func TestParseEnvFile(t *testing.T) {
var (
env = `# comment
HELLO=there
GENERAL<<ghadelimiter_` + uuid.NewString() + `
kenobi # comment
ghadelimiter_` + uuid.NewString() + `
YOU="are a"
BOLD<<ghadelimiter_` + uuid.NewString() + `
one
ghadelimiter_` + uuid.NewString() + `
`
expected = map[string]string{
"HELLO": "there",
"GENERAL": "kenobi",
"YOU": "are a",
"BOLD": "one",
}
actual, err = githubactions.ParseEnvFile(bytes.NewBufferString(env))
)
if err != nil {
t.Error(err)
t.FailNow()
}

for k, v := range actual {
if expected[k] != v {
t.Error("actual", v, "for key", k, "does not match expected", expected[k])
t.FailNow()
}
}
}
24 changes: 3 additions & 21 deletions envconv/path.go → githubactions/path_file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package envconv
package githubactions

import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
)
Expand All @@ -15,8 +14,8 @@ import (
//
// and returns a corresponding PATH environment variable
//
// /usr/local/bin:/usr/bin.
func PathFromReader(r io.Reader) (string, error) {
// /usr/local/bin:/usr/bin
func ParsePathFile(r io.Reader) (string, error) {
var (
lines []string
path = ""
Expand Down Expand Up @@ -44,23 +43,6 @@ func PathFromReader(r io.Reader) (string, error) {
return path, nil
}

// PathFromReader takes a path to a file with newline-delimited directory paths e.g.
//
// /usr/local/bin
// /usr/bin
//
// and returns a corresponding PATH environment variable
//
// /usr/local/bin:/usr/bin.
func PathFromFile(name string) (string, error) {
f, err := os.Open(name)
if err != nil {
return "", err
}

return PathFromReader(f)
}

func shouldIgnore(line string) bool {
trimmedLine := strings.TrimSpace(line)
return trimmedLine == "" || strings.HasPrefix(trimmedLine, "#")
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/google/go-containerregistry v0.12.1
github.com/google/go-github/v50 v50.2.0
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.4.0
github.com/moby/term v0.0.0-20221205130635-1aeaba878587
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
Binary file modified internal/bin/shim
Binary file not shown.
12 changes: 8 additions & 4 deletions internal/cmd/shim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ func mainE(ctx context.Context) error {
}

if githubEnvPath != "" {
if githubEnv, err := envconv.ArrFromFile(githubEnvPath); err == nil {
command.Env = append(command.Env, githubEnv...)
if file, err := os.Open(githubEnvPath); err == nil {
if githubEnv, err := githubactions.ParseEnvFile(file); err == nil {
command.Env = append(command.Env, envconv.MapToArr(githubEnv)...)
}
}
}

Expand All @@ -101,8 +103,10 @@ func mainE(ctx context.Context) error {
}

if githubPathPath != "" {
if githubPath, err := envconv.PathFromFile(githubPathPath); err == nil && githubPath != "" {
path += ":" + githubPath
if file, err := os.Open(githubPathPath); err == nil {
if githubPath, err := githubactions.ParsePathFile(file); err == nil {
path += ":" + githubPath
}
}
}

Expand Down

0 comments on commit 4381f91

Please sign in to comment.