forked from drone-plugins/drone-azure-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
85 lines (73 loc) · 1.67 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
type (
Config struct {
AccountKey string
Account string
Container string
Source string
Destination string
Operation string
Include string
}
Plugin struct {
Config Config
}
)
func (p Plugin) Exec() error {
cmd, err := p.command()
if err != nil {
return err
}
return p.execute(cmd)
}
func (p *Plugin) command() (*exec.Cmd, error) {
args := []string{}
switch p.Config.Operation {
case "upload":
args = append(args, "upload")
args = append(args, fmt.Sprintf("--local-path=%s", p.Config.Source))
args = append(
args,
fmt.Sprintf("--remote-path=%s/%s", p.Config.Container, p.Config.Destination),
)
case "download":
args = append(args, "download")
args = append(
args,
fmt.Sprintf("--remote-path=%s/%s", p.Config.Container, p.Config.Source),
)
args = append(args, fmt.Sprintf("--local-path=%s", p.Config.Destination))
default:
return nil, errors.New(fmt.Sprintf("Invalid operation: %s", p.Config.Operation))
}
if p.Config.Include != "" {
args = append(args, fmt.Sprintf("--include=%s", p.Config.Include))
}
args = append(args, fmt.Sprintf("--storage-account=%s", p.Config.Account))
args = append(
args,
fmt.Sprintf("--storage-account-key=%s", p.Config.AccountKey),
)
args = append(args, "--verbose")
return exec.Command(
"blobxfer",
args...,
), nil
}
func (p Plugin) execute(cmd *exec.Cmd) error {
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
p.trace(cmd)
return cmd.Run()
}
func (p *Plugin) trace(cmd *exec.Cmd) {
fmt.Println("+", strings.Replace(strings.Join(cmd.Args, " "), p.Config.AccountKey, "***", -1))
}