From 0e7ced89952f1f19aef1f9962961534704320f2c Mon Sep 17 00:00:00 2001 From: iam1337 Date: Sat, 18 Feb 2023 23:46:11 +0300 Subject: [PATCH] feat: added env format option --- main.go | 10 +++++----- plugin.go | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 2f1b360..1729c46 100644 --- a/main.go +++ b/main.go @@ -198,10 +198,10 @@ func main() { EnvVars: []string{"PLUGIN_DEBUG", "DEBUG", "INPUT_DEBUG"}, }, &cli.StringFlag{ - Name: "shell", - Usage: "shell on host", - EnvVars: []string{"PLUGIN_HOST_SHELL"}, - Value: "bash", + Name: "envs.format", + Usage: "", + EnvVars: []string{"PLUGIN_ENVS_FORMAT"}, + Value: "export {NAME}={VALUE}", }, } @@ -264,11 +264,11 @@ 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"), UseInsecureCipher: c.Bool("useInsecureCipher"), - Shell: c.String("shell"), Proxy: easyssh.DefaultConfig{ Key: c.String("proxy.ssh-key"), KeyPath: c.String("proxy.key-path"), diff --git a/plugin.go b/plugin.go index 3cd5c47..89584a9 100644 --- a/plugin.go +++ b/plugin.go @@ -41,7 +41,7 @@ type ( Sync bool Ciphers []string UseInsecureCipher bool - Shell string + EnvsFormat string } // Plugin structure @@ -100,16 +100,11 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) { p.log(host, strings.Join(p.Config.Script, "\n")) p.log(host, "======END======") - shell := p.Config.Shell env := []string{} for _, key := range p.Config.Envs { key = strings.ToUpper(key) if val, found := os.LookupEnv(key); found { - if shell == "bash" { - env = append(env, "export "+key+"="+escapeArg(val)) - } else if shell == "powershell" { - env = append(env, "$env:"+key+" = "+escapeArg(val)) - } + env = append(env, p.format(p.Config.EnvsFormat, "NAME", key, "VALUE", val)) } } @@ -158,6 +153,11 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) { wg.Done() } +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