-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.go
199 lines (177 loc) · 5.53 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
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
// Copyright (c) 2020, the Drone Plugins project authors.
// Please see the AUTHORS file for details. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
// DO NOT MODIFY THIS FILE DIRECTLY
package main
import (
"os"
"github.com/drone-plugins/drone-plugin-lib/errors"
"github.com/drone-plugins/drone-plugin-lib/urfave"
"github.com/drone-plugins/drone-s3-cache/plugin"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)
var version = "unknown"
func main() {
settings := &plugin.Settings{}
if _, err := os.Stat("/run/drone/env"); err == nil {
_ = godotenv.Overload("/run/drone/env")
}
app := &cli.App{
Name: "drone-s3-cache",
Usage: "cache build artifacts to s3 storage",
Version: version,
Flags: append(settingsFlags(settings), urfave.Flags()...),
Action: run(settings),
}
if err := app.Run(os.Args); err != nil {
errors.HandleExit(err)
}
}
func run(settings *plugin.Settings) cli.ActionFunc {
return func(ctx *cli.Context) error {
urfave.LoggingFromContext(ctx)
plugin := plugin.New(
*settings,
urfave.PipelineFromContext(ctx),
urfave.NetworkFromContext(ctx),
)
if err := plugin.Validate(); err != nil {
if e, ok := err.(errors.ExitCoder); ok {
return e
}
return errors.ExitMessagef("validation failed: %w", err)
}
if err := plugin.Execute(); err != nil {
if e, ok := err.(errors.ExitCoder); ok {
return e
}
return errors.ExitMessagef("execution failed: %w", err)
}
return nil
}
}
// settingsFlags has the cli.Flags for the plugin.Settings.
func settingsFlags(settings *plugin.Settings) []cli.Flag {
return []cli.Flag{
// Cache information
&cli.StringFlag{
Name: "mode",
Usage: "set plugin mode (rebuild,restore,flush)",
EnvVars: []string{"PLUGIN_MODE"},
Destination: &settings.Mode,
},
&cli.StringFlag{
Name: "filename",
Usage: "filename for the cache archive",
EnvVars: []string{"PLUGIN_FILENAME"},
Destination: &settings.Filename,
},
&cli.StringFlag{
Name: "root",
Usage: "storage root of cache files",
EnvVars: []string{"PLUGIN_ROOT"},
Destination: &settings.Root,
},
&cli.StringFlag{
Name: "path",
Usage: "path to cache files relative to root",
EnvVars: []string{"PLUGIN_PATH"},
Destination: &settings.Path,
},
&cli.StringFlag{
Name: "fallback-path",
Usage: "path to default cache files relative to the root",
EnvVars: []string{"PLUGIN_FALLBACK_PATH"},
Destination: &settings.FallbackPath,
},
&cli.StringFlag{
Name: "flush-path",
Usage: "path to flushable cache files relative to the root",
EnvVars: []string{"PLUGIN_FLUSH_PATH"},
Destination: &settings.FlushPath,
},
&cli.StringSliceFlag{
Name: "mount",
Usage: "directories to cache",
EnvVars: []string{"PLUGIN_MOUNT"},
Destination: &settings.Mount,
},
&cli.IntFlag{
Name: "flush-age",
Usage: "flush cache files older than # days",
EnvVars: []string{"PLUGIN_FLUSH_AGE"},
Value: 30,
Destination: &settings.FlushAge,
},
// Cache information (deprecated)
&cli.BoolFlag{
Name: "rebuild",
Usage: "rebuild the cache directories",
EnvVars: []string{"PLUGIN_REBUILD"},
Destination: &settings.Rebuild,
},
&cli.BoolFlag{
Name: "restore",
Usage: "restore the cache directories",
EnvVars: []string{"PLUGIN_RESTORE"},
Destination: &settings.Restore,
},
&cli.BoolFlag{
Name: "flush",
Usage: "flush the cache",
EnvVars: []string{"PLUGIN_FLUSH"},
Destination: &settings.Flush,
},
// S3 information
&cli.StringFlag{
Name: "endpoint",
Usage: "s3 endpoint",
EnvVars: []string{"PLUGIN_SERVER", "PLUGIN_ENDPOINT", "CACHE_S3_ENDPOINT", "CACHE_S3_SERVER", "S3_ENDPOINT"},
Destination: &settings.S3Options.Endpoint,
},
&cli.StringFlag{
Name: "accelerated-endpoint",
Usage: "s3 accelerated endpoint",
EnvVars: []string{"PLUGIN_ACCELERATED_ENDPOINT", "CACHE_S3_ACCELERATED_ENDPOINT"},
Destination: &settings.S3Options.AcceleratedEndpoint,
},
&cli.StringFlag{
Name: "access-key",
Usage: "s3 access key",
EnvVars: []string{"PLUGIN_ACCESS_KEY", "CACHE_S3_ACCESS_KEY", "AWS_ACCESS_KEY_ID"},
Destination: &settings.S3Options.Access,
},
&cli.StringFlag{
Name: "secret-key",
Usage: "s3 secret key",
EnvVars: []string{"PLUGIN_SECRET_KEY", "CACHE_S3_SECRET_KEY", "AWS_SECRET_ACCESS_KEY"},
Destination: &settings.S3Options.Secret,
},
&cli.StringFlag{
Name: "session-token",
Usage: "s3 session token",
EnvVars: []string{"PLUGIN_SESSION_TOKEN", "CACHE_S3_SESSION_TOKEN", "AWS_SESSION_TOKEN"},
Destination: &settings.S3Options.Token,
},
&cli.StringFlag{
Name: "region",
Usage: "s3 region",
EnvVars: []string{"PLUGIN_REGION", "CACHE_S3_REGION"},
Destination: &settings.S3Options.Region,
},
&cli.StringFlag{
Name: "file-credentials",
Usage: "path to s3 credentials file",
EnvVars: []string{"PLUGIN_FILE_CREDENTIALS", "CACHE_FILE_CREDENTIALS", "AWS_SHARED_CREDENTIALS_FILE"},
Destination: &settings.S3Options.FileCredentials,
},
&cli.StringFlag{
Name: "profile",
Usage: "s3 profile name",
EnvVars: []string{"PLUGIN_PROFILE", "CACHE_S3_PROFILE", "AWS_PROFILE"},
Destination: &settings.S3Options.Profile,
},
}
}