From 6464d9999fb37a8bf9579ce2df406aefe2d3f0e3 Mon Sep 17 00:00:00 2001 From: Vladimir Sigalkin Date: Thu, 13 Apr 2023 04:13:07 +0300 Subject: [PATCH] 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}`. --- main.go | 8 ++++++++ plugin.go | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index efd172d..4680f0b 100644 --- a/main.go +++ b/main.go @@ -196,6 +196,12 @@ func main() { Usage: "debug mode", EnvVars: []string{"PLUGIN_DEBUG", "INPUT_DEBUG"}, }, + &cli.StringFlag{ + Name: "envs.format", + Usage: "", + EnvVars: []string{"PLUGIN_ENVS_FORMAT"}, + Value: "export {NAME}={VALUE}", + }, } // Override a template @@ -241,6 +247,7 @@ func run(c *cli.Context) error { if s := c.String("script.string"); s != "" { scripts = append(scripts, s) } + plugin := Plugin{ Config: Config{ Key: c.String("ssh-key"), @@ -256,6 +263,7 @@ func run(c *cli.Context) error { Script: scripts, ScriptStop: c.Bool("script.stop"), Envs: c.StringSlice("envs"), + EnvsFormat: c.String("envs.format"), Debug: c.Bool("debug"), Sync: c.Bool("sync"), Ciphers: c.StringSlice("ciphers"), diff --git a/plugin.go b/plugin.go index b2cd752..6ea1654 100644 --- a/plugin.go +++ b/plugin.go @@ -40,6 +40,7 @@ type ( Sync bool Ciphers []string UseInsecureCipher bool + EnvsFormat string } // Plugin structure @@ -103,7 +104,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) { for _, key := range p.Config.Envs { key = strings.ToUpper(key) 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{}) { if p.Writer == nil { p.Writer = os.Stdout