2014-10-27 23:21:13 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-05-23 19:30:45 -04:00
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"io"
|
2019-06-06 13:52:12 -04:00
|
|
|
"os"
|
2015-05-23 19:30:45 -04:00
|
|
|
|
2020-11-16 17:23:35 -05:00
|
|
|
kvflag "github.com/hashicorp/packer/command/flag-kv"
|
2020-04-09 17:38:17 -04:00
|
|
|
"github.com/hashicorp/packer/helper/wrappedstreams"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template"
|
2015-05-23 19:30:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// FlagSetFlags is an enum to define what flags are present in the
|
|
|
|
// default FlagSet returned by Meta.FlagSet
|
|
|
|
type FlagSetFlags uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
FlagSetNone FlagSetFlags = 0
|
|
|
|
FlagSetBuildFilter FlagSetFlags = 1 << iota
|
|
|
|
FlagSetVars
|
2014-10-27 23:21:13 -04:00
|
|
|
)
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
// Meta contains the meta-options and functionality that nearly every
|
|
|
|
// Packer command inherits.
|
2014-10-27 23:21:13 -04:00
|
|
|
type Meta struct {
|
2015-05-23 19:30:45 -04:00
|
|
|
CoreConfig *packer.CoreConfig
|
|
|
|
Ui packer.Ui
|
2015-06-29 14:49:45 -04:00
|
|
|
Version string
|
2015-05-23 19:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Core returns the core for the given template given the configured
|
|
|
|
// CoreConfig and user variables on this Meta.
|
2020-05-12 05:24:22 -04:00
|
|
|
func (m *Meta) Core(tpl *template.Template, cla *MetaArgs) (*packer.Core, error) {
|
2015-05-23 19:30:45 -04:00
|
|
|
// Copy the config so we don't modify it
|
|
|
|
config := *m.CoreConfig
|
|
|
|
config.Template = tpl
|
2020-03-12 09:27:56 -04:00
|
|
|
|
|
|
|
fj := &kvflag.FlagJSON{}
|
2020-03-30 04:31:59 -04:00
|
|
|
// First populate fj with contents from var files
|
2020-05-12 05:24:22 -04:00
|
|
|
for _, file := range cla.VarFiles {
|
2020-03-12 09:27:56 -04:00
|
|
|
err := fj.Set(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 04:31:59 -04:00
|
|
|
// Now read fj values back into flagvars and set as config.Variables. Only
|
|
|
|
// add to flagVars if the key doesn't already exist, because flagVars comes
|
|
|
|
// from the command line and should not be overridden by variable files.
|
2020-05-12 05:24:22 -04:00
|
|
|
if cla.Vars == nil {
|
|
|
|
cla.Vars = map[string]string{}
|
2020-03-19 08:57:22 -04:00
|
|
|
}
|
2020-03-12 09:27:56 -04:00
|
|
|
for k, v := range *fj {
|
2020-05-12 05:24:22 -04:00
|
|
|
if _, exists := cla.Vars[k]; !exists {
|
|
|
|
cla.Vars[k] = v
|
2020-03-30 04:31:59 -04:00
|
|
|
}
|
2020-03-12 09:27:56 -04:00
|
|
|
}
|
2020-05-12 05:24:22 -04:00
|
|
|
config.Variables = cla.Vars
|
2015-05-23 19:30:45 -04:00
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
core := packer.NewCore(&config)
|
2015-05-23 19:30:45 -04:00
|
|
|
return core, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagSet returns a FlagSet with the common flags that every
|
|
|
|
// command implements. The exact behavior of FlagSet can be configured
|
|
|
|
// using the flags as the second parameter, for example to disable
|
|
|
|
// build settings on the commands that don't handle builds.
|
2020-08-25 04:51:43 -04:00
|
|
|
func (m *Meta) FlagSet(n string, _ FlagSetFlags) *flag.FlagSet {
|
2015-05-23 19:30:45 -04:00
|
|
|
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
|
|
|
|
|
|
|
// Create an io.Writer that writes to our Ui properly for errors.
|
|
|
|
// This is kind of a hack, but it does the job. Basically: create
|
|
|
|
// a pipe, use a scanner to break it into lines, and output each line
|
|
|
|
// to the UI. Do this forever.
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
errScanner := bufio.NewScanner(errR)
|
|
|
|
go func() {
|
|
|
|
for errScanner.Scan() {
|
|
|
|
m.Ui.Error(errScanner.Text())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f.SetOutput(errW)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateFlags should be called after parsing flags to validate the
|
|
|
|
// given flags
|
|
|
|
func (m *Meta) ValidateFlags() error {
|
|
|
|
// TODO
|
|
|
|
return nil
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
2019-06-06 13:52:12 -04:00
|
|
|
|
|
|
|
// StdinPiped returns true if the input is piped.
|
|
|
|
func (m *Meta) StdinPiped() bool {
|
2020-04-09 17:38:17 -04:00
|
|
|
fi, err := wrappedstreams.Stdin().Stat()
|
2019-06-06 13:52:12 -04:00
|
|
|
if err != nil {
|
|
|
|
// If there is an error, let's just say its not piped
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi.Mode()&os.ModeNamedPipe != 0
|
|
|
|
}
|