diff --git a/main.go b/main.go index 0ccef0f..c561ca3 100644 --- a/main.go +++ b/main.go @@ -215,6 +215,11 @@ func main() { EnvVars: []string{"PLUGIN_ENVS_FORMAT", "INPUT_ENVS_FORMAT"}, Value: envsFormat, }, + &cli.BoolFlag{ + Name: "allenvs", + Usage: "pass all environment variable to shell script", + EnvVars: []string{"PLUGIN_ALLENVS", "INPUT_ALLENVS"}, + }, } // Override a template @@ -282,6 +287,7 @@ func run(c *cli.Context) error { Sync: c.Bool("sync"), Ciphers: c.StringSlice("ciphers"), UseInsecureCipher: c.Bool("useInsecureCipher"), + AllEnvs: c.Bool("allenvs"), Proxy: easyssh.DefaultConfig{ Key: c.String("proxy.ssh-key"), KeyPath: c.String("proxy.key-path"), diff --git a/plugin.go b/plugin.go index d17792b..e52e959 100644 --- a/plugin.go +++ b/plugin.go @@ -43,6 +43,7 @@ type ( Ciphers []string UseInsecureCipher bool EnvsFormat string + AllEnvs bool } // Plugin structure @@ -105,6 +106,10 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) { p.log(host, "======END======") 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 { key = strings.ToUpper(key) if val, found := os.LookupEnv(key); found { @@ -267,3 +272,18 @@ func trimValues(keys []string) []string { 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 +} diff --git a/plugin_test.go b/plugin_test.go index ac8038e..ac301ac 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -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())) +}