mirror of
https://github.com/appleboy/drone-scp.git
synced 2026-03-05 23:17:02 -05:00
feat(tar): add Overwrite flag (#102)
* feat(tar): add Overwrite flag * chore: remove * chore: output * chore: output
This commit is contained in:
parent
933b45bc15
commit
c85ca1ffd2
6
main.go
6
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"),
|
||||
|
||||
20
plugin.go
20
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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user