command/apply: basic implementation
This commit is contained in:
parent
830ddf0c8e
commit
8aa99687c3
|
@ -1,18 +1,67 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
// ApplyCommand is a Command implementation that applies a Terraform
|
||||
// configuration and actually builds or changes infrastructure.
|
||||
type ApplyCommand struct {
|
||||
TFConfig *terraform.Config
|
||||
Ui cli.Ui
|
||||
}
|
||||
|
||||
func (c *ApplyCommand) Run(_ []string) int {
|
||||
func (c *ApplyCommand) Run(args []string) int {
|
||||
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
if len(args) != 1 {
|
||||
c.Ui.Error(
|
||||
"The apply command expects only one argument with the path\n" +
|
||||
"to a Terraform configuration.\n")
|
||||
cmdFlags.Usage()
|
||||
return 1
|
||||
}
|
||||
|
||||
b, err := config.Load(args[0])
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
tfconfig := c.TFConfig
|
||||
tfconfig.Config = b
|
||||
|
||||
tf, err := terraform.New(tfconfig)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
diff, err := tf.Diff(nil)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error running diff: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
state, err := tf.Apply(nil, diff)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error applying diff: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
c.Ui.Output(strings.TrimSpace(state.String()))
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ func init() {
|
|||
Commands = map[string]cli.CommandFactory{
|
||||
"apply": func() (cli.Command, error) {
|
||||
return &command.ApplyCommand{
|
||||
TFConfig: &TFConfig,
|
||||
Ui: ui,
|
||||
}, nil
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue