From ed83305de8c680f220641834484fd38ce3262c1b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 1 Aug 2017 16:54:34 +0800 Subject: [PATCH] add debug mode. (#92) Signed-off-by: Bo-Yi Wu --- main.go | 6 ++++++ plugin.go | 12 +++++++++++- plugin_test.go | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index e68a83b..c31aefe 100644 --- a/main.go +++ b/main.go @@ -125,6 +125,11 @@ func main() { Usage: "Pass envs", EnvVar: "PLUGIN_ENVS", }, + cli.BoolFlag{ + Name: "debug", + Usage: "debug mode", + EnvVar: "PLUGIN_DEBUG", + }, } // Override a template @@ -181,6 +186,7 @@ func run(c *cli.Context) error { Script: c.StringSlice("script"), Secrets: c.StringSlice("secrets"), Envs: c.StringSlice("envs"), + Debug: c.Bool("debug"), Proxy: easyssh.DefaultConfig{ Key: c.String("proxy.ssh-key"), KeyPath: c.String("proxy.key-path"), diff --git a/plugin.go b/plugin.go index 3905c7f..e577565 100644 --- a/plugin.go +++ b/plugin.go @@ -33,6 +33,7 @@ type ( Secrets []string Envs []string Proxy easyssh.DefaultConfig + Debug bool } // Plugin structure @@ -89,6 +90,10 @@ func (p Plugin) Exec() error { }, } + p.log(host, "======CMD======") + p.log(host, strings.Join(p.Config.Script, "\n")) + p.log(host, "======END======") + env := []string{} for _, key := range p.Config.Envs { key = strings.ToUpper(key) @@ -99,7 +104,12 @@ func (p Plugin) Exec() error { p.Config.Script = append(env, p.Config.Script...) - p.log(host, "commands: ", strings.Join(p.Config.Script, "\n")) + if p.Config.Debug { + p.log(host, "======ENV======") + p.log(host, strings.Join(env, "\n")) + p.log(host, "======END======") + } + stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout) if err != nil { errChannel <- err diff --git a/plugin_test.go b/plugin_test.go index 9466bef..975199f 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -241,6 +241,7 @@ func TestSetENV(t *testing.T) { KeyPath: "./tests/.ssh/id_rsa", Secrets: []string{"FOO"}, Envs: []string{"FOO"}, + Debug: true, Script: []string{"whoami; echo $FOO"}, CommandTimeout: 1, Proxy: easyssh.DefaultConfig{