From 6c6bc0ae3e4fb92ff58394d2fbdca5cfd4a12426 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 9 Jun 2014 11:53:41 -0700 Subject: [PATCH] commands/diff: starting up, got it loading a Terraform --- command/diff.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ commands.go | 7 +++++ config.go | 7 +++++ 3 files changed, 86 insertions(+) create mode 100644 command/diff.go diff --git a/command/diff.go b/command/diff.go new file mode 100644 index 000000000..01226f3ad --- /dev/null +++ b/command/diff.go @@ -0,0 +1,72 @@ +package command + +import ( + "flag" + "fmt" + "strings" + + "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/cli" +) + +// DiffCommand is a Command implementation that compares a Terraform +// configuration to an actual infrastructure and shows the differences. +type DiffCommand struct { + TFConfig *terraform.Config + Ui cli.Ui +} + +func (c *DiffCommand) Run(args []string) int { + cmdFlags := flag.NewFlagSet("diff", 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 diff 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 + + _, err = terraform.New(tfconfig) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err)) + return 1 + } + + return 0 +} + +func (c *DiffCommand) Help() string { + helpText := ` +Usage: terraform diff [options] [terraform.tf] + + Shows the differences between the Terraform configuration and + the actual state of an infrastructure. + +Options: + + -state=statefile Path to a Terraform state file to use to look + up Terraform-managed resources. + +` + return strings.TrimSpace(helpText) +} + +func (c *DiffCommand) Synopsis() string { + return "Show changes between Terraform config and infrastructure" +} diff --git a/commands.go b/commands.go index 5e8605c05..d8739a22b 100644 --- a/commands.go +++ b/commands.go @@ -20,6 +20,13 @@ func init() { }, nil }, + "diff": func() (cli.Command, error) { + return &command.DiffCommand{ + TFConfig: &TFConfig, + Ui: ui, + }, nil + }, + "version": func() (cli.Command, error) { return &command.VersionCommand{ Revision: GitCommit, diff --git a/config.go b/config.go index a3764741f..941cf9a51 100644 --- a/config.go +++ b/config.go @@ -1,9 +1,16 @@ package main import ( + "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/go-libucl" ) +// TFConfig is the global base configuration that has the +// basic providers registered. Users of this configuration +// should copy it (call the Copy method) before using it so +// that it isn't corrupted. +var TFConfig terraform.Config + // Config is the structure of the configuration for the Terraform CLI. // // This is not the configuration for Terraform itself. That is in the