From 6d8c11497986cfec47aa9cbbc890fdd6d299bf0c Mon Sep 17 00:00:00 2001 From: teutates <10206665+teutat3s@users.noreply.github.com> Date: Thu, 29 Dec 2022 14:15:21 +0100 Subject: [PATCH] feat(tar): add unlink-first flag (#141) --- main.go | 6 ++++++ plugin.go | 5 +++++ plugin_test.go | 23 +++++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 9bfd676..782ccbf 100644 --- a/main.go +++ b/main.go @@ -259,6 +259,11 @@ func main() { Usage: "use --overwrite flag with tar", EnvVars: []string{"PLUGIN_OVERWRITE", "SCP_OVERWRITE", "INPUT_OVERWRITE"}, }, + &cli.BoolFlag{ + Name: "unlink.first", + Usage: "use --unlink-first flag with tar", + EnvVars: []string{"PLUGIN_UNLINK_FIRST", "SCP_UNLINK_FIRST", "INPUT_UNLINK_FIRST"}, + }, } // Override a template @@ -334,6 +339,7 @@ func run(c *cli.Context) error { TarExec: c.String("tar.exec"), TarTmpPath: c.String("tar.tmp-path"), Overwrite: c.Bool("overwrite"), + UnlinkFirst: c.Bool("unlink.first"), Ciphers: c.StringSlice("ciphers"), UseInsecureCipher: c.Bool("useInsecureCipher"), Proxy: easyssh.DefaultConfig{ diff --git a/plugin.go b/plugin.go index 209a6f7..50bdbdb 100644 --- a/plugin.go +++ b/plugin.go @@ -63,6 +63,7 @@ type ( Proxy easyssh.DefaultConfig Debug bool Overwrite bool + UnlinkFirst bool Ciphers []string UseInsecureCipher bool } @@ -225,6 +226,10 @@ func (p *Plugin) buildArgs(target string) []string { args = append(args, "--overwrite") } + if p.Config.UnlinkFirst { + args = append(args, "--unlink-first") + } + args = append(args, "-C", target, diff --git a/plugin_test.go b/plugin_test.go index 46dc56c..77d1a91 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -638,8 +638,9 @@ func TestPlugin_buildArgs(t *testing.T) { name: "default command", fields: fields{ Config: Config{ - Overwrite: false, - TarExec: "tar", + Overwrite: false, + UnlinkFirst: false, + TarExec: "tar", }, DestFile: "foo.tar", }, @@ -653,6 +654,7 @@ func TestPlugin_buildArgs(t *testing.T) { fields: fields{ Config: Config{ Overwrite: false, + UnlinkFirst: false, TarExec: "tar", StripComponents: 2, }, @@ -670,6 +672,7 @@ func TestPlugin_buildArgs(t *testing.T) { TarExec: "tar", StripComponents: 2, Overwrite: true, + UnlinkFirst: false, }, DestFile: "foo.tar", }, @@ -678,6 +681,22 @@ func TestPlugin_buildArgs(t *testing.T) { }, want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "--overwrite", "-C", "foo"}, }, + { + name: "unlink first", + fields: fields{ + Config: Config{ + TarExec: "tar", + StripComponents: 2, + Overwrite: true, + UnlinkFirst: true, + }, + DestFile: "foo.tar", + }, + args: args{ + target: "foo", + }, + want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "--overwrite", "--unlink-first", "-C", "foo"}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {