Compare commits
4 Commits
master
...
poc/packer
Author | SHA1 | Date | |
---|---|---|---|
|
d597e93e70 | ||
|
1bb154de5a | ||
|
8069ae4f43 | ||
|
e8f7076416 |
@ -142,3 +142,18 @@ type HCL2UpgradeArgs struct {
|
|||||||
MetaArgs
|
MetaArgs
|
||||||
OutputFile string
|
OutputFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ia *InitArgs) AddFlagSets(flags *flag.FlagSet) {
|
||||||
|
flags.StringVar(&ia.PluginDir, "plugin-dir", "", "")
|
||||||
|
flags.BoolVar(&ia.GetPlugins, "get-plugins", true, "")
|
||||||
|
flags.BoolVar(&ia.Upgrade, "upgrade", false, "")
|
||||||
|
ia.MetaArgs.AddFlagSets(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
//InitArgs respresents a parsed cli line for packer init
|
||||||
|
type InitArgs struct {
|
||||||
|
MetaArgs
|
||||||
|
GetPlugins bool
|
||||||
|
PluginDir string
|
||||||
|
Upgrade bool
|
||||||
|
}
|
||||||
|
86
command/init.go
Normal file
86
command/init.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InitCommand initializes a Packer working directory.
|
||||||
|
type InitCommand struct {
|
||||||
|
Meta
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *InitCommand) Run(args []string) int {
|
||||||
|
ctx, cleanup := handleTermInterrupt(c.Ui)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
cla, ret := c.ParseArgs(args)
|
||||||
|
if ret != 0 {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.RunContext(ctx, cla)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *InitCommand) ParseArgs(args []string) (*InitArgs, int) {
|
||||||
|
var cfg InitArgs
|
||||||
|
flags := c.Meta.FlagSet("init", FlagSetVars)
|
||||||
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
||||||
|
cfg.AddFlagSets(flags)
|
||||||
|
if err := flags.Parse(args); err != nil {
|
||||||
|
return &cfg, 1
|
||||||
|
}
|
||||||
|
|
||||||
|
args = flags.Args()
|
||||||
|
if len(args) > 0 {
|
||||||
|
cfg.Path = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init command should treat an empty invocation as if it was run
|
||||||
|
// against the current working directory.
|
||||||
|
if cfg.Path == "" {
|
||||||
|
cfg.Path, _ = os.Getwd()
|
||||||
|
}
|
||||||
|
|
||||||
|
return &cfg, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *InitCommand) RunContext(ctx context.Context, cla *InitArgs) int {
|
||||||
|
|
||||||
|
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
|
||||||
|
if ret != 0 {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = packerStarter.Initialize()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *InitCommand) Help() string {
|
||||||
|
helpText := `
|
||||||
|
Usage: packer init [options]
|
||||||
|
|
||||||
|
Initialize a new or existing Packer working directory by downloading
|
||||||
|
builders, provisioners, and post-processors defined in the template.
|
||||||
|
|
||||||
|
This is the first command that should be executed when working with a new
|
||||||
|
or existing template. Running this command in an empty directory will
|
||||||
|
will perform no operation, and will need to be executed once a template
|
||||||
|
has been created to initialize the working directory.
|
||||||
|
|
||||||
|
It is safe to run init multiple times on a template to update the builders,
|
||||||
|
provisioners, post-processors with changes in the template configuration file.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-get-plugins=false Skips plugin installation.
|
||||||
|
-plugin-dir=PATH Skips plugin installation and loads plugins only from the specified directory.
|
||||||
|
-upgrade=true Updates installed plugins to the latest available version.
|
||||||
|
`
|
||||||
|
return helpText
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *InitCommand) Synopsis() string {
|
||||||
|
return "Initializes a Packer working directory"
|
||||||
|
}
|
@ -64,5 +64,11 @@ func init() {
|
|||||||
Meta: *CommandMeta,
|
Meta: *CommandMeta,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"init": func() (cli.Command, error) {
|
||||||
|
return &command.InitCommand{
|
||||||
|
Meta: *CommandMeta,
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,9 +71,9 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st
|
|||||||
if len(hclFiles)+len(jsonFiles) == 0 {
|
if len(hclFiles)+len(jsonFiles) == 0 {
|
||||||
diags = append(diags, &hcl.Diagnostic{
|
diags = append(diags, &hcl.Diagnostic{
|
||||||
Severity: hcl.DiagError,
|
Severity: hcl.DiagError,
|
||||||
Summary: "Could not find any config file in " + filename,
|
Summary: "No configuration files found in " + filename,
|
||||||
Detail: "A config file must be suffixed with `.pkr.hcl` or " +
|
Detail: "A valid configuration file must be present. Configuration \n" +
|
||||||
"`.pkr.json`. A folder can be referenced.",
|
"files must be suffixed with `.pkr.hcl` or `.pkr.json`.",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, filename := range hclFiles {
|
for _, filename := range hclFiles {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user