* fix: secret file reading * do ToUpper for keys not only for cli args * add tests
This commit is contained in:
18
cmd/root.go
18
cmd/root.go
@@ -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
28
cmd/root_test.go
Normal 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
4
cmd/testdata/secrets.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
mysecret: |
|
||||||
|
line1
|
||||||
|
line2
|
||||||
|
line3
|
||||||
Reference in New Issue
Block a user