mirror of
https://github.com/appleboy/drone-ssh.git
synced 2025-07-02 21:52:51 +08:00
feat: added env format option
This commit is contained in:
parent
f218df05e9
commit
0e7ced8995
10
main.go
10
main.go
@ -198,10 +198,10 @@ func main() {
|
|||||||
EnvVars: []string{"PLUGIN_DEBUG", "DEBUG", "INPUT_DEBUG"},
|
EnvVars: []string{"PLUGIN_DEBUG", "DEBUG", "INPUT_DEBUG"},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "shell",
|
Name: "envs.format",
|
||||||
Usage: "shell on host",
|
Usage: "",
|
||||||
EnvVars: []string{"PLUGIN_HOST_SHELL"},
|
EnvVars: []string{"PLUGIN_ENVS_FORMAT"},
|
||||||
Value: "bash",
|
Value: "export {NAME}={VALUE}",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,11 +264,11 @@ 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"),
|
||||||
UseInsecureCipher: c.Bool("useInsecureCipher"),
|
UseInsecureCipher: c.Bool("useInsecureCipher"),
|
||||||
Shell: c.String("shell"),
|
|
||||||
Proxy: easyssh.DefaultConfig{
|
Proxy: easyssh.DefaultConfig{
|
||||||
Key: c.String("proxy.ssh-key"),
|
Key: c.String("proxy.ssh-key"),
|
||||||
KeyPath: c.String("proxy.key-path"),
|
KeyPath: c.String("proxy.key-path"),
|
||||||
|
14
plugin.go
14
plugin.go
@ -41,7 +41,7 @@ type (
|
|||||||
Sync bool
|
Sync bool
|
||||||
Ciphers []string
|
Ciphers []string
|
||||||
UseInsecureCipher bool
|
UseInsecureCipher bool
|
||||||
Shell string
|
EnvsFormat string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin structure
|
// 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, strings.Join(p.Config.Script, "\n"))
|
||||||
p.log(host, "======END======")
|
p.log(host, "======END======")
|
||||||
|
|
||||||
shell := p.Config.Shell
|
|
||||||
env := []string{}
|
env := []string{}
|
||||||
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 {
|
||||||
if shell == "bash" {
|
env = append(env, p.format(p.Config.EnvsFormat, "NAME", key, "VALUE", val))
|
||||||
env = append(env, "export "+key+"="+escapeArg(val))
|
|
||||||
} else if shell == "powershell" {
|
|
||||||
env = append(env, "$env:"+key+" = "+escapeArg(val))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +153,11 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
|
|||||||
wg.Done()
|
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{}) {
|
func (p Plugin) log(host string, message ...interface{}) {
|
||||||
if p.Writer == nil {
|
if p.Writer == nil {
|
||||||
p.Writer = os.Stdout
|
p.Writer = os.Stdout
|
||||||
|
Loading…
Reference in New Issue
Block a user