chore: flexible configuration of environment value transfer (#235)

**Reason:**
I have to use drone-ssh to work with Windows SSH. Initially, drone-ssh is written so that it transmits environment variables through the `export` command. Which makes it unsuitable for working with Power Shell.

**Solution:**
I have added a new option to configure environment variable commands formatting, with default value: `export {NAME}={VALUE}`. When I use drone-ssh with PowerShell I set this option like this: `$env:{NAME} = {VALUE}`.
This commit is contained in:
Vladimir Sigalkin 2023-04-13 04:13:07 +03:00 committed by GitHub
parent 4aabfc90dd
commit 6464d9999f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -196,6 +196,12 @@ func main() {
Usage: "debug mode", Usage: "debug mode",
EnvVars: []string{"PLUGIN_DEBUG", "INPUT_DEBUG"}, EnvVars: []string{"PLUGIN_DEBUG", "INPUT_DEBUG"},
}, },
&cli.StringFlag{
Name: "envs.format",
Usage: "",
EnvVars: []string{"PLUGIN_ENVS_FORMAT"},
Value: "export {NAME}={VALUE}",
},
} }
// Override a template // Override a template
@ -241,6 +247,7 @@ func run(c *cli.Context) error {
if s := c.String("script.string"); s != "" { if s := c.String("script.string"); s != "" {
scripts = append(scripts, s) scripts = append(scripts, s)
} }
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{
Key: c.String("ssh-key"), Key: c.String("ssh-key"),
@ -256,6 +263,7 @@ func run(c *cli.Context) error {
Script: scripts, Script: scripts,
ScriptStop: c.Bool("script.stop"), ScriptStop: c.Bool("script.stop"),
Envs: c.StringSlice("envs"), Envs: c.StringSlice("envs"),
EnvsFormat: c.String("envs.format"),
Debug: c.Bool("debug"), Debug: c.Bool("debug"),
Sync: c.Bool("sync"), Sync: c.Bool("sync"),
Ciphers: c.StringSlice("ciphers"), Ciphers: c.StringSlice("ciphers"),

View File

@ -40,6 +40,7 @@ type (
Sync bool Sync bool
Ciphers []string Ciphers []string
UseInsecureCipher bool UseInsecureCipher bool
EnvsFormat string
} }
// Plugin structure // Plugin structure
@ -103,7 +104,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
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 {
env = append(env, "export "+key+"="+escapeArg(val)) env = append(env, p.format(p.Config.EnvsFormat, "{NAME}", key, "{VALUE}", escapeArg(val)))
} }
} }
@ -150,6 +151,11 @@ loop:
} }
} }
func (p Plugin) format(format string, args ...string) string {
r := strings.NewReplacer(args...)
return r.Replace(format)
}
func (p Plugin) log(host string, message ...interface{}) { func (p Plugin) log(host string, message ...interface{}) {
if p.Writer == nil { if p.Writer == nil {
p.Writer = os.Stdout p.Writer = os.Stdout