forked from drone-plugins/drone-rsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (112 loc) · 2.7 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
_ "github.com/joho/godotenv/autoload"
)
var (
buildCommit string
version string // build number set at compile-time
)
func main() {
fmt.Printf("Drone Rsync Plugin built from %s\n", buildCommit)
app := cli.NewApp()
app.Name = "Rsync"
app.Usage = "Uses rsync to upload files to a remote server"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "host",
Usage: "connect to host",
EnvVar: "PLUGIN_HOST,SSH_HOST",
},
cli.StringFlag{
Name: "user",
Usage: "connect as user",
EnvVar: "PLUGIN_USER,SSH_USER",
Value: "root",
},
cli.IntFlag{
Name: "port",
Usage: "connect to port",
EnvVar: "PLUGIN_PORT,SSH_PORT",
Value: 22,
},
cli.StringFlag{
Name: "source",
Usage: "source path from which files are copied",
EnvVar: "PLUGIN_SOURCE,SOURCE",
Value: "./",
},
cli.StringFlag{
Name: "target",
Usage: "target path to which files are copied",
EnvVar: "PLUGIN_TARGET,TARGET",
Value: "/",
},
cli.BoolFlag{
Name: "delete",
Usage: "delete extraneous files from the target dir",
EnvVar: "PLUGIN_DELETE,DELETE",
},
cli.BoolFlag{
Name: "recursive",
Usage: "recursively transfer all files",
EnvVar: "PLUGIN_RECURSIVE,RECURSIVE",
},
cli.StringSliceFlag{
Name: "include",
Usage: "include files matching the specified pattern",
EnvVar: "PLUGIN_INCLUDE,INCLUDE",
},
cli.StringSliceFlag{
Name: "exclude",
Usage: "exclude files matching the specified pattern",
EnvVar: "PLUGIN_EXCLUDE,EXCLUDE",
},
cli.StringSliceFlag{
Name: "filter",
Usage: "include or exclude files according to filtering rules",
EnvVar: "PLUGIN_FILTER,FILTER",
},
cli.StringSliceFlag{
Name: "script",
Usage: "execute commands on the remote host after files are copied",
EnvVar: "PLUGIN_SCRIPT,SCRIPT",
},
cli.StringFlag{
Name: "ssh-key",
Usage: "private ssh key",
EnvVar: "PLUGIN_SSH_KEY,PLUGIN_KEY,SSH_KEY",
},
}
app.Run(os.Args)
}
func run(c *cli.Context) {
plugin := Plugin{
Config: Config{
Hosts: c.StringSlice("host"),
User: c.String("user"),
Port: c.Int("port"),
Source: c.String("source"),
Target: c.String("target"),
Delete: c.Bool("delete"),
Recursive: c.Bool("recursive"),
Include: c.StringSlice("include"),
Exclude: c.StringSlice("exclude"),
Filter: c.StringSlice("filter"),
Commands: c.StringSlice("script"),
Key: c.String("ssh-key"),
},
}
if len(plugin.Config.Key) == 0 {
fmt.Println("No SSH key")
os.Exit(1)
}
if err := plugin.Exec(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}