feat: added env format option

This commit is contained in:
iam1337 2023-02-18 23:46:11 +03:00
parent f218df05e9
commit 0e7ced8995
2 changed files with 12 additions and 12 deletions

10
main.go
View File

@ -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"),

View File

@ -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