feat: support strip components flag for tar command (#65)
Remove the specified number of leading path elements.
This commit is contained in:
parent
8dba1e8e32
commit
35c2f7a14c
28
main.go
28
main.go
|
@ -178,6 +178,11 @@ func main() {
|
|||
Usage: "proxy connection timeout",
|
||||
EnvVar: "PLUGIN_PROXY_TIMEOUT,PROXY_SSH_TIMEOUT",
|
||||
},
|
||||
cli.IntFlag{
|
||||
Name: "strip.components",
|
||||
Usage: "Remove the specified number of leading path elements.",
|
||||
EnvVar: "PLUGIN_STRIP_COMPONENTS,TAR_STRIP_COMPONENTS",
|
||||
},
|
||||
}
|
||||
|
||||
// Override a template
|
||||
|
@ -236,17 +241,18 @@ func run(c *cli.Context) error {
|
|||
Link: c.String("build.link"),
|
||||
},
|
||||
Config: Config{
|
||||
Host: c.StringSlice("host"),
|
||||
Port: c.String("port"),
|
||||
Username: c.String("username"),
|
||||
Password: c.String("password"),
|
||||
Timeout: c.Duration("timeout"),
|
||||
CommandTimeout: c.Int("command.timeout"),
|
||||
Key: c.String("key"),
|
||||
KeyPath: c.String("key-path"),
|
||||
Target: c.StringSlice("target"),
|
||||
Source: c.StringSlice("source"),
|
||||
Remove: c.Bool("rm"),
|
||||
Host: c.StringSlice("host"),
|
||||
Port: c.String("port"),
|
||||
Username: c.String("username"),
|
||||
Password: c.String("password"),
|
||||
Timeout: c.Duration("timeout"),
|
||||
CommandTimeout: c.Int("command.timeout"),
|
||||
Key: c.String("key"),
|
||||
KeyPath: c.String("key-path"),
|
||||
Target: c.StringSlice("target"),
|
||||
Source: c.StringSlice("source"),
|
||||
Remove: c.Bool("rm"),
|
||||
StripComponents: c.Int("strip.components"),
|
||||
Proxy: easyssh.DefaultConfig{
|
||||
Key: c.String("proxy.ssh-key"),
|
||||
KeyPath: c.String("proxy.key-path"),
|
||||
|
|
31
plugin.go
31
plugin.go
|
@ -38,18 +38,19 @@ type (
|
|||
|
||||
// Config for the plugin.
|
||||
Config struct {
|
||||
Host []string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Key string
|
||||
KeyPath string
|
||||
Timeout time.Duration
|
||||
CommandTimeout int
|
||||
Target []string
|
||||
Source []string
|
||||
Remove bool
|
||||
Proxy easyssh.DefaultConfig
|
||||
Host []string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Key string
|
||||
KeyPath string
|
||||
Timeout time.Duration
|
||||
CommandTimeout int
|
||||
Target []string
|
||||
Source []string
|
||||
Remove bool
|
||||
StripComponents int
|
||||
Proxy easyssh.DefaultConfig
|
||||
}
|
||||
|
||||
// Plugin values.
|
||||
|
@ -252,7 +253,11 @@ func (p *Plugin) Exec() error {
|
|||
|
||||
// untar file
|
||||
p.log(host, "untar file", p.DestFile)
|
||||
_, _, _, err = ssh.Run(fmt.Sprintf("tar -xf %s -C %s", p.DestFile, target), p.Config.CommandTimeout)
|
||||
if p.Config.StripComponents > 0 {
|
||||
_, _, _, err = ssh.Run(fmt.Sprintf("tar -xf %s --strip-components=%d -C %s", p.DestFile, p.Config.StripComponents, target), p.Config.CommandTimeout)
|
||||
} else {
|
||||
_, _, _, err = ssh.Run(fmt.Sprintf("tar -xf %s -C %s", p.DestFile, target), p.Config.CommandTimeout)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
|
|
|
@ -197,6 +197,42 @@ func TestSCPFromProxySetting(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStripComponentsFlag(t *testing.T) {
|
||||
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
||||
exec.Command("eval", "`ssh-agent -k`").Run()
|
||||
}
|
||||
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/global/*"},
|
||||
StripComponents: 2,
|
||||
Target: []string{filepath.Join(u.HomeDir, "123")},
|
||||
CommandTimeout: 60,
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/c.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/d.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// func TestSCPFileFromSSHAgent(t *testing.T) {
|
||||
// if os.Getenv("SSH_AUTH_SOCK") == "" {
|
||||
// exec.Command("eval", "`ssh-agent -s`").Run()
|
||||
|
@ -305,6 +341,10 @@ func TestGlobList(t *testing.T) {
|
|||
paterns = []string{"tests/aa.txt", "tests/b.txt"}
|
||||
expects = []string{"tests/b.txt"}
|
||||
assert.Equal(t, expects, globList(paterns))
|
||||
|
||||
paterns = []string{"./tests/b.txt"}
|
||||
expects = []string{"./tests/b.txt"}
|
||||
assert.Equal(t, expects, globList(paterns))
|
||||
}
|
||||
|
||||
func TestRemoveDestFile(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue