Browse Source

feat: support build command.

update-packer-186
Bo-Yi Wu 7 years ago
parent
commit
6ef6599bae
  1. 18
      main.go
  2. 46
      plugin.go

18
main.go

@ -55,6 +55,21 @@ func main() {
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_SYNTAX_ONLY",
},
cli.BoolFlag{
Name: "parallel",
Usage: "Disable parallelization (on by default)",
EnvVar: "PLUGIN_PARALLEL",
},
}
app.Version = Version
@ -95,6 +110,9 @@ func run(c *cli.Context) error {
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"),
},
}

46
plugin.go

@ -20,6 +20,9 @@ type (
SyntaxOnly bool
Except []string
Only []string
Color bool
Debug bool
Parallel bool
}
// Plugin values
@ -61,6 +64,47 @@ func pkValidate(config Config) *exec.Cmd {
)
}
func pkBuild(config Config) *exec.Cmd {
args := []string{
"build",
}
for _, v := range config.VarFiles {
args = append(args, "-var-file", fmt.Sprintf("%s", v))
}
for k, v := range config.Vars {
args = append(args, "-var")
args = append(args, fmt.Sprintf("%s=%s", k, v))
}
if len(config.Except) > 0 {
args = append(args, "-except="+strings.Join(config.Except, ","))
}
if len(config.Only) > 0 {
args = append(args, "-only="+strings.Join(config.Only, ","))
}
if config.Parallel {
args = append(args, "-parallel=true")
}
if config.Color {
args = append(args, "-color=true")
}
if config.Debug {
args = append(args, "-debug")
}
args = append(args, config.Template)
return exec.Command(
"packer",
args...,
)
}
// Exec executes the plugin.
func (p *Plugin) Exec() error {
var commands []*exec.Cmd
@ -79,6 +123,8 @@ func (p *Plugin) Exec() error {
switch action {
case "validate":
commands = append(commands, pkValidate(p.Config))
case "build":
commands = append(commands, pkBuild(p.Config))
default:
return fmt.Errorf("valid actions are: validate, build You provided %s", action)
}

Loading…
Cancel
Save