feat: implement and test all environment variables functionality (#264)
- Add a new flag `allenvs` to pass all environment variables to the shell script - Implement the `AllEnvs` functionality in the `exec` function - Add a new function `findEnvs` to find all environment variables with specified prefixes - Add tests for the `findEnvs` function and the `AllEnvs` functionality
This commit is contained in:
parent
80cecf1ed3
commit
32510c86bf
6
main.go
6
main.go
|
@ -215,6 +215,11 @@ func main() {
|
||||||
EnvVars: []string{"PLUGIN_ENVS_FORMAT", "INPUT_ENVS_FORMAT"},
|
EnvVars: []string{"PLUGIN_ENVS_FORMAT", "INPUT_ENVS_FORMAT"},
|
||||||
Value: envsFormat,
|
Value: envsFormat,
|
||||||
},
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "allenvs",
|
||||||
|
Usage: "pass all environment variable to shell script",
|
||||||
|
EnvVars: []string{"PLUGIN_ALLENVS", "INPUT_ALLENVS"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override a template
|
// Override a template
|
||||||
|
@ -282,6 +287,7 @@ func run(c *cli.Context) error {
|
||||||
Sync: c.Bool("sync"),
|
Sync: c.Bool("sync"),
|
||||||
Ciphers: c.StringSlice("ciphers"),
|
Ciphers: c.StringSlice("ciphers"),
|
||||||
UseInsecureCipher: c.Bool("useInsecureCipher"),
|
UseInsecureCipher: c.Bool("useInsecureCipher"),
|
||||||
|
AllEnvs: c.Bool("allenvs"),
|
||||||
Proxy: easyssh.DefaultConfig{
|
Proxy: easyssh.DefaultConfig{
|
||||||
Key: c.String("proxy.ssh-key"),
|
Key: c.String("proxy.ssh-key"),
|
||||||
KeyPath: c.String("proxy.key-path"),
|
KeyPath: c.String("proxy.key-path"),
|
||||||
|
|
20
plugin.go
20
plugin.go
|
@ -43,6 +43,7 @@ type (
|
||||||
Ciphers []string
|
Ciphers []string
|
||||||
UseInsecureCipher bool
|
UseInsecureCipher bool
|
||||||
EnvsFormat string
|
EnvsFormat string
|
||||||
|
AllEnvs bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin structure
|
// Plugin structure
|
||||||
|
@ -105,6 +106,10 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
|
||||||
p.log(host, "======END======")
|
p.log(host, "======END======")
|
||||||
|
|
||||||
env := []string{}
|
env := []string{}
|
||||||
|
if p.Config.AllEnvs {
|
||||||
|
allenvs := findEnvs("DRONE_", "PLUGIN_", "INPUT_", "GITHUB_")
|
||||||
|
p.Config.Envs = append(p.Config.Envs, allenvs...)
|
||||||
|
}
|
||||||
for _, key := range p.Config.Envs {
|
for _, key := range p.Config.Envs {
|
||||||
key = strings.ToUpper(key)
|
key = strings.ToUpper(key)
|
||||||
if val, found := os.LookupEnv(key); found {
|
if val, found := os.LookupEnv(key); found {
|
||||||
|
@ -267,3 +272,18 @@ func trimValues(keys []string) []string {
|
||||||
|
|
||||||
return newKeys
|
return newKeys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find all envs from specified prefix
|
||||||
|
func findEnvs(prefix ...string) []string {
|
||||||
|
envs := []string{}
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
for _, p := range prefix {
|
||||||
|
if strings.HasPrefix(e, p) {
|
||||||
|
e = strings.Split(e, "=")[0]
|
||||||
|
envs = append(envs, e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return envs
|
||||||
|
}
|
||||||
|
|
|
@ -823,3 +823,99 @@ func TestPlugin_hostPort(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindEnvs(t *testing.T) {
|
||||||
|
testEnvs := []string{
|
||||||
|
"INPUT_FOO",
|
||||||
|
"INPUT_BAR",
|
||||||
|
"NO_PREFIX",
|
||||||
|
"INPUT_FOOBAR",
|
||||||
|
}
|
||||||
|
|
||||||
|
origEnviron := os.Environ()
|
||||||
|
os.Clearenv()
|
||||||
|
for _, env := range testEnvs {
|
||||||
|
os.Setenv(env, "dummyValue")
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
os.Clearenv()
|
||||||
|
for _, env := range origEnviron {
|
||||||
|
pair := strings.SplitN(env, "=", 2)
|
||||||
|
os.Setenv(pair[0], pair[1])
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
t.Run("Find single prefix", func(t *testing.T) {
|
||||||
|
expected := []string{"INPUT_FOO", "INPUT_BAR", "INPUT_FOOBAR"}
|
||||||
|
result := findEnvs("INPUT_")
|
||||||
|
if !reflect.DeepEqual(result, expected) {
|
||||||
|
t.Errorf("Expected %v, but got %v", expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Find multiple prefixes", func(t *testing.T) {
|
||||||
|
expected := []string{"INPUT_FOO", "INPUT_BAR", "NO_PREFIX", "INPUT_FOOBAR"}
|
||||||
|
result := findEnvs("INPUT_", "NO_PREFIX")
|
||||||
|
if !reflect.DeepEqual(result, expected) {
|
||||||
|
t.Errorf("Expected %v, but got %v", expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Find non-existing prefix", func(t *testing.T) {
|
||||||
|
expected := []string{}
|
||||||
|
result := findEnvs("NON_EXISTING_")
|
||||||
|
if !reflect.DeepEqual(result, expected) {
|
||||||
|
t.Errorf("Expected %v, but got %v", expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAllEnvs(t *testing.T) {
|
||||||
|
var (
|
||||||
|
buffer bytes.Buffer
|
||||||
|
expected = `
|
||||||
|
======CMD======
|
||||||
|
echo "[${INPUT_1}]"
|
||||||
|
echo "[${GITHUB_2}]"
|
||||||
|
echo "[${PLUGIN_3}]"
|
||||||
|
======END======
|
||||||
|
out: [foobar]
|
||||||
|
out: [foobar]
|
||||||
|
out: [foobar]
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
os.Setenv("INPUT_1", `foobar`)
|
||||||
|
os.Setenv("GITHUB_2", `foobar`)
|
||||||
|
os.Setenv("PLUGIN_3", `foobar`)
|
||||||
|
|
||||||
|
plugin := Plugin{
|
||||||
|
Config: Config{
|
||||||
|
Host: []string{"localhost"},
|
||||||
|
Username: "drone-scp",
|
||||||
|
Port: 22,
|
||||||
|
KeyPath: "./tests/.ssh/test",
|
||||||
|
Passphrase: "1234",
|
||||||
|
AllEnvs: true,
|
||||||
|
Script: []string{
|
||||||
|
`echo "[${INPUT_1}]"`,
|
||||||
|
`echo "[${GITHUB_2}]"`,
|
||||||
|
`echo "[${PLUGIN_3}]"`,
|
||||||
|
},
|
||||||
|
CommandTimeout: 10 * time.Second,
|
||||||
|
Proxy: easyssh.DefaultConfig{
|
||||||
|
Server: "localhost",
|
||||||
|
User: "drone-scp",
|
||||||
|
Port: "22",
|
||||||
|
KeyPath: "./tests/.ssh/id_rsa",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Writer: &buffer,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := plugin.Exec()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, unindent(expected), unindent(buffer.String()))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue