From c85ca1ffd2afac729713aafc4260758683b13bf7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 28 Sep 2019 16:59:01 +0800 Subject: [PATCH] feat(tar): add Overwrite flag (#102) * feat(tar): add Overwrite flag * chore: remove * chore: output * chore: output --- main.go | 6 ++++ plugin.go | 20 ++++++++++--- plugin_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index aa6a0ea..13eca48 100644 --- a/main.go +++ b/main.go @@ -203,6 +203,11 @@ func main() { Usage: "remove target folder before upload data", EnvVar: "PLUGIN_DEBUG,DEBUG,INPUT_DEBUG", }, + cli.BoolFlag{ + Name: "overwrite", + Usage: "use --overwrite flag with tar", + EnvVar: "PLUGIN_OVERWRITE,SCP_OVERWRITE,INPUT_OVERWRITE", + }, } // Override a template @@ -279,6 +284,7 @@ func run(c *cli.Context) error { StripComponents: c.Int("strip.components"), TarExec: c.String("tar.exec"), TarTmpPath: c.String("tar.tmp-path"), + Overwrite: c.Bool("overwrite"), Proxy: easyssh.DefaultConfig{ Key: c.String("proxy.ssh-key"), KeyPath: c.String("proxy.key-path"), diff --git a/plugin.go b/plugin.go index 5444f8e..1c726bd 100644 --- a/plugin.go +++ b/plugin.go @@ -61,6 +61,7 @@ type ( TarTmpPath string Proxy easyssh.DefaultConfig Debug bool + Overwrite bool } // Plugin values. @@ -196,8 +197,7 @@ type fileList struct { Source []string } -// Args get tar command -func (p *Plugin) Args(target string) []string { +func (p *Plugin) buildArgs(target string) []string { args := []string{} args = append(args, @@ -211,6 +211,10 @@ func (p *Plugin) Args(target string) []string { args = append(args, strconv.Itoa(p.Config.StripComponents)) } + if p.Config.Overwrite { + args = append(args, "--overwrite") + } + args = append(args, "-C", target, @@ -326,11 +330,19 @@ func (p *Plugin) Exec() error { // untar file p.log(host, "untar file", p.DestFile) - commamd := strings.Join(p.Args(target), " ") + commamd := strings.Join(p.buildArgs(target), " ") if p.Config.Debug { fmt.Println("$", commamd) } - _, _, _, err = ssh.Run(commamd, p.Config.CommandTimeout) + outStr, errStr, _, err := ssh.Run(commamd, p.Config.CommandTimeout) + + if outStr != "" { + p.log(host, "output: ", outStr) + } + + if errStr != "" { + p.log(host, "error: ", errStr) + } if err != nil { errChannel <- err diff --git a/plugin_test.go b/plugin_test.go index 846014d..c432ef7 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -5,6 +5,7 @@ import ( "os/exec" "os/user" "path/filepath" + "reflect" "testing" "time" @@ -457,3 +458,79 @@ func TestRemoveDestFile(t *testing.T) { err = plugin.removeDestFile(ssh) assert.Error(t, err) } + +func TestPlugin_buildArgs(t *testing.T) { + type fields struct { + Repo Repo + Build Build + Config Config + DestFile string + } + type args struct { + target string + } + tests := []struct { + name string + fields fields + args args + want []string + }{ + { + name: "default command", + fields: fields{ + Config: Config{ + Overwrite: false, + TarExec: "tar", + }, + DestFile: "foo.tar", + }, + args: args{ + target: "foo", + }, + want: []string{"tar", "-xf", "foo.tar", "-C", "foo"}, + }, + { + name: "strip components", + fields: fields{ + Config: Config{ + Overwrite: false, + TarExec: "tar", + StripComponents: 2, + }, + DestFile: "foo.tar", + }, + args: args{ + target: "foo", + }, + want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "-C", "foo"}, + }, + { + name: "overwrite", + fields: fields{ + Config: Config{ + TarExec: "tar", + StripComponents: 2, + Overwrite: true, + }, + DestFile: "foo.tar", + }, + args: args{ + target: "foo", + }, + want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "--overwrite", "-C", "foo"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &Plugin{ + Repo: tt.fields.Repo, + Build: tt.fields.Build, + Config: tt.fields.Config, + DestFile: tt.fields.DestFile, + } + if got := p.buildArgs(tt.args.target); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Plugin.buildArgs() = %v, want %v", got, tt.want) + } + }) + } +}