fix: secret file reading (#45) (#2664)

* fix: secret file reading

* do ToUpper for keys not only for cli args

* add tests
This commit is contained in:
ChristopherHX
2025-02-12 22:36:50 +01:00
committed by GitHub
parent 3f741df6bc
commit 7fec28d69e
3 changed files with 42 additions and 8 deletions

View File

@@ -331,6 +331,10 @@ func readYamlFile(file string) (map[string]string, error) {
} }
func readEnvs(path string, envs map[string]string) bool { func readEnvs(path string, envs map[string]string) bool {
return readEnvsEx(path, envs, false)
}
func readEnvsEx(path string, envs map[string]string, caseInsensitive bool) bool {
if _, err := os.Stat(path); err == nil { if _, err := os.Stat(path); err == nil {
var env map[string]string var env map[string]string
if ext := filepath.Ext(path); ext == ".yml" || ext == ".yaml" { if ext := filepath.Ext(path); ext == ".yml" || ext == ".yaml" {
@@ -342,6 +346,9 @@ func readEnvs(path string, envs map[string]string) bool {
log.Fatalf("Error loading from %s: %v", path, err) log.Fatalf("Error loading from %s: %v", path, err)
} }
for k, v := range env { for k, v := range env {
if caseInsensitive {
k = strings.ToUpper(k)
}
if _, ok := envs[k]; !ok { if _, ok := envs[k]; !ok {
envs[k] = v envs[k] = v
} }
@@ -412,14 +419,9 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
log.Debugf("Loading secrets from %s", input.Secretfile()) log.Debugf("Loading secrets from %s", input.Secretfile())
secrets := newSecrets(input.secrets) secrets := newSecrets(input.secrets)
_ = readEnvs(input.Secretfile(), secrets) _ = readEnvsEx(input.Secretfile(), secrets, true)
hasGitHubToken := false
for k := range secrets { if _, hasGitHubToken := secrets["GITHUB_TOKEN"]; !hasGitHubToken {
if strings.EqualFold(k, "GITHUB_TOKEN") {
hasGitHubToken = true
}
}
if !hasGitHubToken {
secrets["GITHUB_TOKEN"], _ = gh.GetToken(ctx, "") secrets["GITHUB_TOKEN"], _ = gh.GetToken(ctx, "")
} }

28
cmd/root_test.go Normal file
View File

@@ -0,0 +1,28 @@
package cmd
import (
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReadSecrets(t *testing.T) {
secrets := map[string]string{}
ret := readEnvsEx(path.Join("testdata", "secrets.yml"), secrets, true)
assert.True(t, ret)
assert.Equal(t, `line1
line2
line3
`, secrets["MYSECRET"])
}
func TestReadEnv(t *testing.T) {
secrets := map[string]string{}
ret := readEnvs(path.Join("testdata", "secrets.yml"), secrets)
assert.True(t, ret)
assert.Equal(t, `line1
line2
line3
`, secrets["mysecret"])
}

4
cmd/testdata/secrets.yml vendored Normal file
View File

@@ -0,0 +1,4 @@
mysecret: |
line1
line2
line3