/
main.go
145 lines (128 loc) · 4.05 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
// 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"
"time"
"github.com/drone-plugins/drone-downstream/plugin"
"github.com/drone-plugins/drone-plugin-lib/errors"
"github.com/drone-plugins/drone-plugin-lib/urfave"
"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-downstream",
Usage: "trigger a downstream drone build",
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{
&cli.StringSliceFlag{
Name: "repositories",
Usage: "List of repositories to trigger",
EnvVars: []string{"PLUGIN_REPOSITORIES"},
Destination: &settings.Repos,
},
&cli.StringFlag{
Name: "server",
Usage: "Trigger a drone build on a custom server",
EnvVars: []string{"PLUGIN_SERVER", "DOWNSTREAM_SERVER"},
Destination: &settings.Server,
},
&cli.StringFlag{
Name: "token",
Usage: "Drone API token from your user settings",
EnvVars: []string{"PLUGIN_TOKEN", "DRONE_TOKEN", "DOWNSTREAM_TOKEN"},
Destination: &settings.Token,
},
&cli.BoolFlag{
Name: "wait",
Usage: "Wait for any currently running builds to finish",
EnvVars: []string{"PLUGIN_WAIT"},
Destination: &settings.Wait,
},
&cli.DurationFlag{
Name: "timeout",
Value: time.Duration(60) * time.Second,
Usage: "How long to wait on any currently running builds",
EnvVars: []string{"PLUGIN_WAIT_TIMEOUT"},
Destination: &settings.Timeout,
},
&cli.BoolFlag{
Name: "last-successful",
Usage: "Trigger last successful build",
EnvVars: []string{"PLUGIN_LAST_SUCCESSFUL"},
Destination: &settings.LastSuccessful,
},
&cli.StringSliceFlag{
Name: "params",
Usage: "List of params (key=value or file paths of params) to pass to triggered builds",
EnvVars: []string{"PLUGIN_PARAMS"},
Destination: &settings.Params,
},
&cli.StringSliceFlag{
Name: "params-from-env",
Usage: "List of environment variables to pass to triggered builds",
EnvVars: []string{"PLUGIN_PARAMS_FROM_ENV"},
Destination: &settings.ParamsEnv,
},
&cli.StringFlag{
Name: "deploy",
Usage: "Environment to trigger deploy for the respective build",
EnvVars: []string{"PLUGIN_DEPLOY"},
Destination: &settings.Deploy,
},
&cli.BoolFlag{
Name: "block",
Usage: "Block until the triggered build is finished, makes this build fail if triggered build fails",
EnvVars: []string{"PLUGIN_BLOCK"},
Destination: &settings.Block,
},
&cli.DurationFlag{
Name: "blockTimeout",
Value: time.Duration(60) * time.Minute,
Usage: "How long to block until the triggered build is finished",
EnvVars: []string{"PLUGIN_BLOCK_TIMEOUT"},
Destination: &settings.BlockTimeout,
},
}
}