-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
143 lines (132 loc) · 3.46 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
package main
import (
"encoding/json"
"os"
"github.com/rs/zerolog/log"
"github.com/urfave/cli"
)
// Version set at compile-time
var (
Version string
BuildNum string
)
func main() {
app := cli.NewApp()
app.Name = "packer plugin"
app.Usage = "packer plugin"
app.Action = run
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "actions",
Usage: "a list of actions to have packer perform",
EnvVar: "PLUGIN_ACTIONS",
},
cli.StringFlag{
Name: "vars",
Usage: "a map of variables to pass to the Packer `build` commands. Each value is passed as a `<key>=<value>` option",
EnvVar: "PLUGIN_VARS",
},
cli.StringSliceFlag{
Name: "var_files",
Usage: "a list of var files to use. Each value is passed as -var-file=<value>",
EnvVar: "PLUGIN_VAR_FILES",
},
cli.StringSliceFlag{
Name: "except",
Usage: "validate or build all builds other than these",
EnvVar: "PLUGIN_EXCEPT",
},
cli.StringSliceFlag{
Name: "only",
Usage: "validate or build only the specified builds",
EnvVar: "PLUGIN_ONLY",
},
cli.StringFlag{
Name: "template",
Usage: "Will execute multiple builds in parallel as defined in the template",
EnvVar: "PLUGIN_TEMPLATE",
},
cli.StringFlag{
Name: "context",
Usage: "The context directory path to use for executing `packer` command, defaults to root of the git repo",
Value: ".",
EnvVar: "PLUGIN_CONTEXT",
},
cli.BoolFlag{
Name: "syntax_only",
Usage: "Only check syntax. Do not verify config of the template",
EnvVar: "PLUGIN_SYNTAX_ONLY",
},
cli.BoolFlag{
Name: "color",
Usage: "Disable color output (on by default)",
EnvVar: "PLUGIN_COLOR",
},
cli.BoolFlag{
Name: "debug",
Usage: "Debug mode enabled for builds",
EnvVar: "PLUGIN_DEBUG",
},
cli.BoolFlag{
Name: "parallel",
Usage: "Disable parallelization (on by default)",
EnvVar: "PLUGIN_PARALLEL",
},
cli.BoolFlag{
Name: "readable",
Usage: "Machine-readable output",
EnvVar: "PLUGIN_READABLE",
},
cli.BoolFlag{
Name: "force",
Usage: "Force a build to continue if artifacts exist, deletes existing artifacts",
EnvVar: "PLUGIN_FORCE",
},
cli.BoolFlag{
Name: "upgrade",
Usage: "On top of installing missing plugins, update installed plugins to the latest available version",
EnvVar: "PLUGIN_UPGRADE",
},
}
app.Version = Version
if BuildNum != "" {
app.Version = app.Version + "+" + BuildNum
}
if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("can't start app")
}
}
func run(c *cli.Context) error {
var version string
if BuildNum != "" {
version = Version + "+" + BuildNum
} else {
version = Version
}
log.Info().Str("revision", version).Msg("Drone Packer Plugin Version")
var vars map[string]string
if c.String("vars") != "" {
if err := json.Unmarshal([]byte(c.String("vars")), &vars); err != nil {
log.Fatal().Err(err).Msg("json unmarshal")
}
}
plugin := Plugin{
Config: Config{
Actions: c.StringSlice("actions"),
Vars: vars,
Template: c.String("template"),
Context: c.String("context"),
VarFiles: c.StringSlice("var_files"),
Except: c.StringSlice("except"),
Only: c.StringSlice("only"),
SyntaxOnly: c.Bool("syntax_only"),
Color: c.Bool("color"),
Debug: c.Bool("debug"),
Parallel: c.Bool("parallel"),
Readable: c.Bool("readable"),
Force: c.Bool("force"),
IsUpgrade: c.Bool("upgrade"),
},
}
return plugin.Exec()
}