-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.go
More file actions
218 lines (207 loc) · 5.57 KB
/
Copy pathmain.go
File metadata and controls
218 lines (207 loc) · 5.57 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"log"
"os"
"strconv"
"time"
"github.com/appleboy/com/random"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)
// Version set at compile-time
var (
Version string
)
func main() {
// Load env-file if it exists first
if filename, found := os.LookupEnv("PLUGIN_ENV_FILE"); found {
_ = godotenv.Load(filename)
}
if _, err := os.Stat("/run/drone/env"); err == nil {
_ = godotenv.Overload("/run/drone/env")
}
app := cli.NewApp()
app.Name = "git-push plugin"
app.Usage = "git-push plugin"
app.Copyright = "Copyright (c) " + strconv.Itoa(time.Now().Year()) + " Bo-Yi Wu"
app.Action = run
app.Version = Version
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "commit.author.name",
Usage: "git author name",
EnvVars: []string{
"PLUGIN_AUTHOR_NAME",
"DRONE_COMMIT_AUTHOR",
"CI_COMMIT_AUTHOR",
"INPUT_AUTHOR_NAME",
"GITEA_ACTOR",
},
},
&cli.StringFlag{
Name: "commit.author.email",
Usage: "git author email",
EnvVars: []string{
"PLUGIN_AUTHOR_EMAIL",
"DRONE_COMMIT_AUTHOR_EMAIL",
"CI_COMMIT_AUTHOR_EMAIL",
"INPUT_AUTHOR_EMAIL",
"GITEA_ACTOR_EMAIL",
},
},
&cli.StringFlag{
Name: "netrc.machine",
Usage: "netrc machine",
EnvVars: []string{"PLUGIN_NETRC_MACHINE", "DRONE_NETRC_MACHINE", "INPUT_NETRC_MACHINE"},
},
&cli.StringFlag{
Name: "netrc.username",
Usage: "netrc username",
EnvVars: []string{
"PLUGIN_USERNAME",
"DRONE_NETRC_USERNAME",
"GITHUB_USERNAME",
"INPUT_USERNAME",
"GITEA_USERNAME",
},
},
&cli.StringFlag{
Name: "netrc.password",
Usage: "netrc password",
EnvVars: []string{
"PLUGIN_PASSWORD",
"DRONE_NETRC_PASSWORD",
"GITHUB_PASSWORD",
"INPUT_PASSWORD",
"GITEA_PASSWORD",
},
},
&cli.StringFlag{
Name: "ssh-key",
Usage: "private ssh key",
EnvVars: []string{"PLUGIN_SSH_KEY", "GIT_PUSH_SSH_KEY", "INPUT_SSH_KEY"},
},
&cli.StringFlag{
Name: "remote",
Usage: "url of the remote repo",
EnvVars: []string{"PLUGIN_REMOTE", "GIT_PUSH_REMOTE", "INPUT_REMOTE"},
},
&cli.StringFlag{
Name: "remote-name",
Usage: "name of the remote repo",
Value: "",
EnvVars: []string{"PLUGIN_REMOTE_NAME", "GIT_PUSH_REMOTE_NAME", "INPUT_REMOTE_NAME"},
},
&cli.StringFlag{
Name: "branch",
Usage: "name of remote branch",
EnvVars: []string{"PLUGIN_BRANCH", "GIT_PUSH_BRANCH", "INPUT_BRANCH"},
Value: "master",
},
&cli.StringFlag{
Name: "local-branch",
Usage: "name of local branch",
Value: "HEAD",
EnvVars: []string{"PLUGIN_LOCAL_BRANCH", "GIT_PUSH_LOCAL_BRANCH", "INPUT_LOCAL_BRANCH"},
},
&cli.StringFlag{
Name: "path",
Usage: "path to git repo",
EnvVars: []string{"PLUGIN_PATH", "GIT_PUSH_PATH", "INPUT_PATH"},
},
&cli.BoolFlag{
Name: "force",
Usage: "force push to remote",
EnvVars: []string{"PLUGIN_FORCE", "GIT_PUSH_FORCE", "INPUT_FORCE"},
},
&cli.BoolFlag{
Name: "followtags",
Usage: "push to remote with tags",
EnvVars: []string{"PLUGIN_FOLLOWTAGS", "GIT_PUSH_FOLLOWTAGS", "INPUT_FOLLOWTAGS"},
},
&cli.BoolFlag{
Name: "skip-verify",
Usage: "skip ssl verification",
EnvVars: []string{"PLUGIN_SKIP_VERIFY", "GIT_PUSH_SKIP_VERIFY", "INPUT_SKIP_VERIFY"},
},
&cli.BoolFlag{
Name: "commit",
Usage: "commit dirty changes",
EnvVars: []string{"PLUGIN_COMMIT", "GIT_PUSH_COMMIT", "INPUT_COMMIT"},
},
&cli.StringFlag{
Name: "commit-message",
Usage: "commit message",
EnvVars: []string{
"PLUGIN_COMMIT_MESSAGE",
"GIT_PUSH_COMMIT_MESSAGE",
"INPUT_COMMIT_MESSAGE",
},
},
&cli.StringFlag{
Name: "tag",
Usage: "tag to add to the commit",
EnvVars: []string{"PLUGIN_TAG", "GIT_PUSH_TAG", "INPUT_TAG"},
},
&cli.BoolFlag{
Name: "empty-commit",
Usage: "empty commit",
EnvVars: []string{"PLUGIN_EMPTY_COMMIT", "GIT_PUSH_EMPTY_COMMIT", "INPUT_EMPTY_COMMIT"},
},
&cli.BoolFlag{
Name: "no-verify",
Usage: "bypasses the pre-commit and commit-msg hooks",
EnvVars: []string{"PLUGIN_NO_VERIFY", "GIT_PUSH_NO_VERIFY", "INPUT_NO_VERIFY"},
},
&cli.BoolFlag{
Name: "rebase",
Usage: "pull rebase branch before push",
EnvVars: []string{"PLUGIN_REBASE", "GIT_PUSH_REBASE", "INPUT_REBASE"},
},
&cli.BoolFlag{
Name: "mirror",
Usage: "push all refs to remote repository with --mirror",
EnvVars: []string{"PLUGIN_MIRROR", "GIT_PUSH_MIRROR", "INPUT_MIRROR"},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
plugin := Plugin{
Netrc: Netrc{
Login: c.String("netrc.username"),
Machine: c.String("netrc.machine"),
Password: c.String("netrc.password"),
},
Commit: Commit{
Author: Author{
Name: c.String("commit.author.name"),
Email: c.String("commit.author.email"),
},
},
Config: Config{
Key: c.String("ssh-key"),
Remote: c.String("remote"),
RemoteName: c.String("remote-name"),
Branch: c.String("branch"),
LocalBranch: c.String("local-branch"),
Path: c.String("path"),
Force: c.Bool("force"),
FollowTags: c.Bool("followtags"),
SkipVerify: c.Bool("skip-verify"),
Commit: c.Bool("commit"),
CommitMessage: c.String("commit-message"),
Tag: c.String("tag"),
EmptyCommit: c.Bool("empty-commit"),
NoVerify: c.Bool("no-verify"),
Rebase: c.Bool("rebase"),
Mirror: c.Bool("mirror"),
},
}
if plugin.Config.RemoteName == "" {
plugin.Config.RemoteName = random.String(10)
}
return plugin.Exec(c.Context)
}