Skip to content

Commit 32510c8

Browse files
authored
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
1 parent 80cecf1 commit 32510c8

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ func main() {
215215
EnvVars: []string{"PLUGIN_ENVS_FORMAT", "INPUT_ENVS_FORMAT"},
216216
Value: envsFormat,
217217
},
218+
&cli.BoolFlag{
219+
Name: "allenvs",
220+
Usage: "pass all environment variable to shell script",
221+
EnvVars: []string{"PLUGIN_ALLENVS", "INPUT_ALLENVS"},
222+
},
218223
}
219224

220225
// Override a template
@@ -282,6 +287,7 @@ func run(c *cli.Context) error {
282287
Sync: c.Bool("sync"),
283288
Ciphers: c.StringSlice("ciphers"),
284289
UseInsecureCipher: c.Bool("useInsecureCipher"),
290+
AllEnvs: c.Bool("allenvs"),
285291
Proxy: easyssh.DefaultConfig{
286292
Key: c.String("proxy.ssh-key"),
287293
KeyPath: c.String("proxy.key-path"),

plugin.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type (
4343
Ciphers []string
4444
UseInsecureCipher bool
4545
EnvsFormat string
46+
AllEnvs bool
4647
}
4748

4849
// Plugin structure
@@ -105,6 +106,10 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
105106
p.log(host, "======END======")
106107

107108
env := []string{}
109+
if p.Config.AllEnvs {
110+
allenvs := findEnvs("DRONE_", "PLUGIN_", "INPUT_", "GITHUB_")
111+
p.Config.Envs = append(p.Config.Envs, allenvs...)
112+
}
108113
for _, key := range p.Config.Envs {
109114
key = strings.ToUpper(key)
110115
if val, found := os.LookupEnv(key); found {
@@ -267,3 +272,18 @@ func trimValues(keys []string) []string {
267272

268273
return newKeys
269274
}
275+
276+
// Find all envs from specified prefix
277+
func findEnvs(prefix ...string) []string {
278+
envs := []string{}
279+
for _, e := range os.Environ() {
280+
for _, p := range prefix {
281+
if strings.HasPrefix(e, p) {
282+
e = strings.Split(e, "=")[0]
283+
envs = append(envs, e)
284+
break
285+
}
286+
}
287+
}
288+
return envs
289+
}

plugin_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,99 @@ func TestPlugin_hostPort(t *testing.T) {
823823
})
824824
}
825825
}
826+
827+
func TestFindEnvs(t *testing.T) {
828+
testEnvs := []string{
829+
"INPUT_FOO",
830+
"INPUT_BAR",
831+
"NO_PREFIX",
832+
"INPUT_FOOBAR",
833+
}
834+
835+
origEnviron := os.Environ()
836+
os.Clearenv()
837+
for _, env := range testEnvs {
838+
os.Setenv(env, "dummyValue")
839+
}
840+
841+
defer func() {
842+
os.Clearenv()
843+
for _, env := range origEnviron {
844+
pair := strings.SplitN(env, "=", 2)
845+
os.Setenv(pair[0], pair[1])
846+
}
847+
}()
848+
849+
t.Run("Find single prefix", func(t *testing.T) {
850+
expected := []string{"INPUT_FOO", "INPUT_BAR", "INPUT_FOOBAR"}
851+
result := findEnvs("INPUT_")
852+
if !reflect.DeepEqual(result, expected) {
853+
t.Errorf("Expected %v, but got %v", expected, result)
854+
}
855+
})
856+
857+
t.Run("Find multiple prefixes", func(t *testing.T) {
858+
expected := []string{"INPUT_FOO", "INPUT_BAR", "NO_PREFIX", "INPUT_FOOBAR"}
859+
result := findEnvs("INPUT_", "NO_PREFIX")
860+
if !reflect.DeepEqual(result, expected) {
861+
t.Errorf("Expected %v, but got %v", expected, result)
862+
}
863+
})
864+
865+
t.Run("Find non-existing prefix", func(t *testing.T) {
866+
expected := []string{}
867+
result := findEnvs("NON_EXISTING_")
868+
if !reflect.DeepEqual(result, expected) {
869+
t.Errorf("Expected %v, but got %v", expected, result)
870+
}
871+
})
872+
}
873+
874+
func TestAllEnvs(t *testing.T) {
875+
var (
876+
buffer bytes.Buffer
877+
expected = `
878+
======CMD======
879+
echo "[${INPUT_1}]"
880+
echo "[${GITHUB_2}]"
881+
echo "[${PLUGIN_3}]"
882+
======END======
883+
out: [foobar]
884+
out: [foobar]
885+
out: [foobar]
886+
`
887+
)
888+
889+
os.Setenv("INPUT_1", `foobar`)
890+
os.Setenv("GITHUB_2", `foobar`)
891+
os.Setenv("PLUGIN_3", `foobar`)
892+
893+
plugin := Plugin{
894+
Config: Config{
895+
Host: []string{"localhost"},
896+
Username: "drone-scp",
897+
Port: 22,
898+
KeyPath: "./tests/.ssh/test",
899+
Passphrase: "1234",
900+
AllEnvs: true,
901+
Script: []string{
902+
`echo "[${INPUT_1}]"`,
903+
`echo "[${GITHUB_2}]"`,
904+
`echo "[${PLUGIN_3}]"`,
905+
},
906+
CommandTimeout: 10 * time.Second,
907+
Proxy: easyssh.DefaultConfig{
908+
Server: "localhost",
909+
User: "drone-scp",
910+
Port: "22",
911+
KeyPath: "./tests/.ssh/id_rsa",
912+
},
913+
},
914+
Writer: &buffer,
915+
}
916+
917+
err := plugin.Exec()
918+
assert.Nil(t, err)
919+
920+
assert.Equal(t, unindent(expected), unindent(buffer.String()))
921+
}

0 commit comments

Comments
 (0)