diff --git a/command/apply.go b/command/apply.go index d93537777..1f36a18c4 100644 --- a/command/apply.go +++ b/command/apply.go @@ -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 { - Ui cli.Ui + 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 } diff --git a/commands.go b/commands.go index fa48dfaf8..347ca9da3 100644 --- a/commands.go +++ b/commands.go @@ -25,7 +25,8 @@ func init() { Commands = map[string]cli.CommandFactory{ "apply": func() (cli.Command, error) { return &command.ApplyCommand{ - Ui: ui, + TFConfig: &TFConfig, + Ui: ui, }, nil },