forked from drone-plugins/drone-gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (106 loc) · 2.77 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
package main
import (
"context"
"encoding/json"
"log"
"os"
"cloud.google.com/go/storage"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
var (
version = "unknown"
)
func main() {
app := cli.NewApp()
app.Name = "gcs plugin"
app.Usage = "gcs plugin"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "token",
Usage: "google auth key",
EnvVar: "PLUGIN_TOKEN,GOOGLE_CREDENTIALS,TOKEN",
},
cli.StringSliceFlag{
Name: "acl",
Usage: "a list of access rules applied to the uploaded files, in a form of entity:role",
EnvVar: "PLUGIN_ACL",
},
cli.StringFlag{
Name: "source",
Usage: "location of files to upload",
EnvVar: "PLUGIN_SOURCE",
},
cli.StringFlag{
Name: "ignore",
Usage: "skip files matching this pattern, relative to source",
EnvVar: "PLUGIN_IGNORE",
},
cli.StringFlag{
Name: "target",
Usage: "destination to copy files to, including bucket name",
EnvVar: "PLUGIN_TARGET",
},
cli.StringSliceFlag{
Name: "gzip",
Usage: `files with the specified extensions will be gzipped and uploaded with "gzip" Content-Encoding header`,
EnvVar: "PLUGIN_GZIP",
},
cli.StringFlag{
Name: "cache-control",
Usage: "Cache-Control header",
EnvVar: "PLUGIN_CACHE_CONTROL",
},
cli.StringFlag{
Name: "metadata",
Usage: "an arbitrary dictionary with custom metadata applied to all objects",
EnvVar: "PLUGIN_METADATA",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
plugin := Plugin{
Config: Config{
Token: c.String("token"),
ACL: c.StringSlice("acl"),
Source: c.String("source"),
Target: c.String("target"),
Ignore: c.String("ignore"),
Gzip: c.StringSlice("gzip"),
CacheControl: c.String("cache-control"),
},
}
if m := c.String("metadata"); m != "" {
var metadata map[string]string
if err := json.Unmarshal([]byte(m), &metadata); err != nil {
return errors.Wrap(err, "error parsing metadata field")
}
plugin.Config.Metadata = metadata
}
if plugin.Config.Token == "" {
return errors.New("Missing google credentials")
}
if plugin.Config.Source == "" {
return errors.New("Missing source")
}
if plugin.Config.Target == "" {
return errors.New("Missing target")
}
auth, err := google.JWTConfigFromJSON([]byte(plugin.Config.Token), storage.ScopeFullControl)
if err != nil {
return errors.Wrap(err, "failed to authenticate token")
}
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithTokenSource(auth.TokenSource(ctx)))
if err != nil {
return errors.Wrap(err, "failed to initialize storage")
}
return plugin.Exec(client)
}