2019-06-04 17:17:50 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-06-06 13:52:12 -04:00
|
|
|
"bufio"
|
2020-05-12 05:24:22 -04:00
|
|
|
"context"
|
2019-06-04 17:17:50 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-06 13:52:12 -04:00
|
|
|
"github.com/chzyer/readline"
|
|
|
|
"github.com/hashicorp/packer/helper/wrappedreadline"
|
2020-04-09 17:38:17 -04:00
|
|
|
"github.com/hashicorp/packer/helper/wrappedstreams"
|
2019-06-04 17:17:50 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
2020-06-05 11:23:54 -04:00
|
|
|
var TiniestBuilder = strings.NewReader(`{
|
2019-06-04 17:17:50 -04:00
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type":"null",
|
|
|
|
"communicator": "none"
|
|
|
|
}
|
|
|
|
]
|
2020-06-05 11:23:54 -04:00
|
|
|
}`)
|
2019-06-04 17:17:50 -04:00
|
|
|
|
|
|
|
type ConsoleCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConsoleCommand) Run(args []string) int {
|
2020-05-12 05:29:09 -04:00
|
|
|
ctx := context.Background()
|
2020-05-12 05:24:22 -04:00
|
|
|
|
|
|
|
cfg, ret := c.ParseArgs(args)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.RunContext(ctx, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConsoleCommand) ParseArgs(args []string) (*ConsoleArgs, int) {
|
|
|
|
var cfg ConsoleArgs
|
2019-06-04 17:17:50 -04:00
|
|
|
flags := c.Meta.FlagSet("console", FlagSetVars)
|
|
|
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
2020-05-12 05:24:22 -04:00
|
|
|
cfg.AddFlagSets(flags)
|
2019-06-04 17:17:50 -04:00
|
|
|
if err := flags.Parse(args); err != nil {
|
2020-05-12 05:24:22 -04:00
|
|
|
return &cfg, 1
|
2019-06-04 17:17:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
2020-06-05 11:23:54 -04:00
|
|
|
if len(args) == 1 {
|
|
|
|
cfg.Path = args[0]
|
|
|
|
}
|
2020-05-12 05:24:22 -04:00
|
|
|
return &cfg, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConsoleCommand) RunContext(ctx context.Context, cla *ConsoleArgs) int {
|
2020-06-05 11:23:54 -04:00
|
|
|
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
2019-06-04 17:17:50 -04:00
|
|
|
}
|
|
|
|
|
2020-09-02 11:19:56 -04:00
|
|
|
_ = packerStarter.Initialize()
|
2020-07-24 04:58:03 -04:00
|
|
|
|
2019-06-06 13:52:12 -04:00
|
|
|
// Determine if stdin is a pipe. If so, we evaluate directly.
|
|
|
|
if c.StdinPiped() {
|
2020-06-05 11:23:54 -04:00
|
|
|
return c.modePiped(packerStarter)
|
2019-06-06 13:52:12 -04:00
|
|
|
}
|
|
|
|
|
2020-06-05 11:23:54 -04:00
|
|
|
return c.modeInteractive(packerStarter)
|
2019-06-04 17:17:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*ConsoleCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: packer console [options] [TEMPLATE]
|
|
|
|
|
|
|
|
Creates a console for testing variable interpolation.
|
|
|
|
If a template is provided, this command will load the template and any
|
|
|
|
variables defined therein into its context to be referenced during
|
|
|
|
interpolation.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-var 'key=value' Variable for templates, can be used multiple times.
|
2021-01-11 05:50:23 -05:00
|
|
|
-var-file=path JSON or HCL2 file containing user variables.
|
2019-06-04 17:17:50 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ConsoleCommand) Synopsis() string {
|
2019-08-08 14:22:59 -04:00
|
|
|
return "creates a console for testing variable interpolation"
|
2019-06-04 17:17:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*ConsoleCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ConsoleCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{
|
|
|
|
"-var": complete.PredictNothing,
|
|
|
|
"-var-file": complete.PredictNothing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 11:23:54 -04:00
|
|
|
func (c *ConsoleCommand) modePiped(cfg packer.Evaluator) int {
|
2019-06-06 13:52:12 -04:00
|
|
|
var lastResult string
|
2020-04-09 17:38:17 -04:00
|
|
|
scanner := bufio.NewScanner(wrappedstreams.Stdin())
|
2020-06-05 11:23:54 -04:00
|
|
|
ret := 0
|
2019-06-06 13:52:12 -04:00
|
|
|
for scanner.Scan() {
|
2020-06-05 11:23:54 -04:00
|
|
|
result, _, diags := cfg.EvaluateExpression(strings.TrimSpace(scanner.Text()))
|
|
|
|
if len(diags) > 0 {
|
|
|
|
ret = writeDiags(c.Ui, nil, diags)
|
2019-06-06 13:52:12 -04:00
|
|
|
}
|
|
|
|
// Store the last result
|
|
|
|
lastResult = result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output the final result
|
|
|
|
c.Ui.Message(lastResult)
|
2020-06-05 11:23:54 -04:00
|
|
|
return ret
|
2019-06-06 13:52:12 -04:00
|
|
|
}
|
|
|
|
|
2020-06-05 11:23:54 -04:00
|
|
|
func (c *ConsoleCommand) modeInteractive(cfg packer.Evaluator) int {
|
|
|
|
// Setup the UI so we can output directly to stdout
|
2019-06-06 13:52:12 -04:00
|
|
|
l, err := readline.NewEx(wrappedreadline.Override(&readline.Config{
|
|
|
|
Prompt: "> ",
|
|
|
|
InterruptPrompt: "^C",
|
|
|
|
EOFPrompt: "exit",
|
|
|
|
HistorySearchFold: true,
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing console: %s",
|
|
|
|
err))
|
|
|
|
return 1
|
|
|
|
}
|
2019-06-04 17:17:50 -04:00
|
|
|
for {
|
|
|
|
// Read a line
|
2019-06-06 13:52:12 -04:00
|
|
|
line, err := l.Readline()
|
|
|
|
if err == readline.ErrInterrupt {
|
|
|
|
if len(line) == 0 {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-04 17:17:50 -04:00
|
|
|
} else if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2020-06-05 11:23:54 -04:00
|
|
|
out, exit, diags := cfg.EvaluateExpression(line)
|
|
|
|
ret := writeDiags(c.Ui, nil, diags)
|
|
|
|
if exit {
|
|
|
|
return ret
|
2019-06-04 17:17:50 -04:00
|
|
|
}
|
|
|
|
c.Ui.Say(out)
|
2020-06-05 11:23:54 -04:00
|
|
|
if exit {
|
|
|
|
return ret
|
|
|
|
}
|
2019-06-04 17:17:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|