|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform/helper/wrappedreadline" |
| 11 | + "github.com/hashicorp/terraform/repl" |
| 12 | + |
| 13 | + "github.com/chzyer/readline" |
| 14 | + "github.com/mitchellh/cli" |
| 15 | +) |
| 16 | + |
| 17 | +// ConsoleCommand is a Command implementation that applies a Terraform |
| 18 | +// configuration and actually builds or changes infrastructure. |
| 19 | +type ConsoleCommand struct { |
| 20 | + Meta |
| 21 | + |
| 22 | + // When this channel is closed, the apply will be cancelled. |
| 23 | + ShutdownCh <-chan struct{} |
| 24 | +} |
| 25 | + |
| 26 | +func (c *ConsoleCommand) Run(args []string) int { |
| 27 | + args = c.Meta.process(args, true) |
| 28 | + cmdFlags := c.Meta.flagSet("console") |
| 29 | + cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") |
| 30 | + cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } |
| 31 | + if err := cmdFlags.Parse(args); err != nil { |
| 32 | + return 1 |
| 33 | + } |
| 34 | + |
| 35 | + pwd, err := os.Getwd() |
| 36 | + if err != nil { |
| 37 | + c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) |
| 38 | + return 1 |
| 39 | + } |
| 40 | + |
| 41 | + var configPath string |
| 42 | + args = cmdFlags.Args() |
| 43 | + if len(args) > 1 { |
| 44 | + c.Ui.Error("The console command expects at most one argument.") |
| 45 | + cmdFlags.Usage() |
| 46 | + return 1 |
| 47 | + } else if len(args) == 1 { |
| 48 | + configPath = args[0] |
| 49 | + } else { |
| 50 | + configPath = pwd |
| 51 | + } |
| 52 | + |
| 53 | + // Build the context based on the arguments given |
| 54 | + ctx, _, err := c.Context(contextOpts{ |
| 55 | + Path: configPath, |
| 56 | + PathEmptyOk: true, |
| 57 | + StatePath: c.Meta.statePath, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + c.Ui.Error(err.Error()) |
| 61 | + return 1 |
| 62 | + } |
| 63 | + |
| 64 | + // Setup the UI so we can output directly to stdout |
| 65 | + ui := &cli.BasicUi{ |
| 66 | + Writer: c.Stdout(), |
| 67 | + ErrorWriter: c.Stderr(), |
| 68 | + } |
| 69 | + |
| 70 | + // IO Loop |
| 71 | + session := &repl.Session{ |
| 72 | + Interpolater: ctx.Interpolater(), |
| 73 | + } |
| 74 | + |
| 75 | + // Determine if stdin is a pipe. If so, we evaluate directly. |
| 76 | + if c.StdinPiped() { |
| 77 | + return c.modePiped(session, ui) |
| 78 | + } |
| 79 | + |
| 80 | + return c.modeInteractive(session, ui) |
| 81 | +} |
| 82 | + |
| 83 | +func (c *ConsoleCommand) modePiped(session *repl.Session, ui cli.Ui) int { |
| 84 | + var lastResult string |
| 85 | + scanner := bufio.NewScanner(c.Stdin()) |
| 86 | + for scanner.Scan() { |
| 87 | + // Handle it. If there is an error exit immediately |
| 88 | + result, err := session.Handle(strings.TrimSpace(scanner.Text())) |
| 89 | + if err != nil { |
| 90 | + ui.Error(err.Error()) |
| 91 | + return 1 |
| 92 | + } |
| 93 | + |
| 94 | + // Store the last result |
| 95 | + lastResult = result |
| 96 | + } |
| 97 | + |
| 98 | + // Output the final result |
| 99 | + ui.Output(lastResult) |
| 100 | + |
| 101 | + return 0 |
| 102 | +} |
| 103 | + |
| 104 | +func (c *ConsoleCommand) modeInteractive(session *repl.Session, ui cli.Ui) int { |
| 105 | + // Configure input |
| 106 | + l, err := readline.NewEx(wrappedreadline.Override(&readline.Config{ |
| 107 | + Prompt: "> ", |
| 108 | + InterruptPrompt: "^C", |
| 109 | + EOFPrompt: "exit", |
| 110 | + HistorySearchFold: true, |
| 111 | + })) |
| 112 | + if err != nil { |
| 113 | + c.Ui.Error(fmt.Sprintf( |
| 114 | + "Error initializing console: %s", |
| 115 | + err)) |
| 116 | + return 1 |
| 117 | + } |
| 118 | + defer l.Close() |
| 119 | + |
| 120 | + for { |
| 121 | + // Read a line |
| 122 | + line, err := l.Readline() |
| 123 | + if err == readline.ErrInterrupt { |
| 124 | + if len(line) == 0 { |
| 125 | + break |
| 126 | + } else { |
| 127 | + continue |
| 128 | + } |
| 129 | + } else if err == io.EOF { |
| 130 | + break |
| 131 | + } |
| 132 | + |
| 133 | + out, err := session.Handle(line) |
| 134 | + if err == repl.ErrSessionExit { |
| 135 | + break |
| 136 | + } |
| 137 | + if err != nil { |
| 138 | + ui.Error(err.Error()) |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + ui.Output(out) |
| 143 | + } |
| 144 | + |
| 145 | + return 0 |
| 146 | +} |
| 147 | + |
| 148 | +func (c *ConsoleCommand) Help() string { |
| 149 | + helpText := ` |
| 150 | +Usage: terraform console [options] [DIR] |
| 151 | +
|
| 152 | + Starts an interactive console for experimenting with Terraform |
| 153 | + interpolations. |
| 154 | +
|
| 155 | + This will open an interactive console that you can use to type |
| 156 | + interpolations into and inspect their values. This command loads the |
| 157 | + current state. This lets you explore and test interpolations before |
| 158 | + using them in future configurations. |
| 159 | +
|
| 160 | + This command will never modify your state. |
| 161 | +
|
| 162 | + DIR can be set to a directory with a Terraform state to load. By |
| 163 | + default, this will default to the current working directory. |
| 164 | +
|
| 165 | +Options: |
| 166 | +
|
| 167 | + -state=path Path to read state. Defaults to "terraform.tfstate" |
| 168 | +
|
| 169 | + -var 'foo=bar' Set a variable in the Terraform configuration. This |
| 170 | + flag can be set multiple times. |
| 171 | +
|
| 172 | + -var-file=foo Set variables in the Terraform configuration from |
| 173 | + a file. If "terraform.tfvars" is present, it will be |
| 174 | + automatically loaded if this flag is not specified. |
| 175 | +
|
| 176 | +
|
| 177 | +` |
| 178 | + return strings.TrimSpace(helpText) |
| 179 | +} |
| 180 | + |
| 181 | +func (c *ConsoleCommand) Synopsis() string { |
| 182 | + return "Interactive console for Terraform interpolations" |
| 183 | +} |
0 commit comments