From a4a4e3784dd07bc1f53387dadd5c74c3180a49bf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 24 May 2014 12:04:43 -0700 Subject: [PATCH] Implement CLI, version command --- .gitignore | 1 + command/version.go | 39 +++++++++++++++++++++++++++++ command/version_test.go | 11 +++++++++ commands.go | 26 ++++++++++++++++++++ main.go | 45 ++++++++++++++++++++++++++++++++++ terraform/resource_provider.go | 21 ++++++++++++++++ version.go | 12 +++++++++ 7 files changed, 155 insertions(+) create mode 100644 command/version.go create mode 100644 command/version_test.go create mode 100644 commands.go create mode 100644 main.go create mode 100644 terraform/resource_provider.go create mode 100644 version.go diff --git a/.gitignore b/.gitignore index 1a16d6f31..3b7a7c264 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.dll +*.exe example.tf vendor/ diff --git a/command/version.go b/command/version.go new file mode 100644 index 000000000..4e433435f --- /dev/null +++ b/command/version.go @@ -0,0 +1,39 @@ +package command + +import ( + "bytes" + "fmt" + + "github.com/mitchellh/cli" +) + +// VersionCommand is a Command implementation prints the version. +type VersionCommand struct { + Revision string + Version string + VersionPrerelease string + Ui cli.Ui +} + +func (c *VersionCommand) Help() string { + return "" +} + +func (c *VersionCommand) Run(_ []string) int { + var versionString bytes.Buffer + fmt.Fprintf(&versionString, "Terraform v%s", c.Version) + if c.VersionPrerelease != "" { + fmt.Fprintf(&versionString, ".%s", c.VersionPrerelease) + + if c.Revision != "" { + fmt.Fprintf(&versionString, " (%s)", c.Revision) + } + } + + c.Ui.Output(versionString.String()) + return 0 +} + +func (c *VersionCommand) Synopsis() string { + return "Prints the Terraform version" +} diff --git a/command/version_test.go b/command/version_test.go new file mode 100644 index 000000000..2a645690f --- /dev/null +++ b/command/version_test.go @@ -0,0 +1,11 @@ +package command + +import ( + "testing" + + "github.com/mitchellh/cli" +) + +func TestVersionCommand_implements(t *testing.T) { + var _ cli.Command = &VersionCommand{} +} diff --git a/commands.go b/commands.go new file mode 100644 index 000000000..73e9862d6 --- /dev/null +++ b/commands.go @@ -0,0 +1,26 @@ +package main + +import ( + "os" + + "github.com/hashicorp/terraform/command" + "github.com/mitchellh/cli" +) + +// Commands is the mapping of all the available Terraform commands. +var Commands map[string]cli.CommandFactory + +func init() { + ui := &cli.BasicUi{Writer: os.Stdout} + + Commands = map[string]cli.CommandFactory{ + "version": func() (cli.Command, error) { + return &command.VersionCommand{ + Revision: GitCommit, + Version: Version, + VersionPrerelease: VersionPrerelease, + Ui: ui, + }, nil + }, + } +} diff --git a/main.go b/main.go new file mode 100644 index 000000000..6a2d88aab --- /dev/null +++ b/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + + "github.com/mitchellh/cli" +) + +func main() { + os.Exit(realMain()) +} + +func realMain() int { + log.SetOutput(ioutil.Discard) + + // Get the command line args. We shortcut "--version" and "-v" to + // just show the version. + args := os.Args[1:] + for _, arg := range args { + if arg == "-v" || arg == "--version" { + newArgs := make([]string, len(args)+1) + newArgs[0] = "version" + copy(newArgs[1:], args) + args = newArgs + break + } + } + + cli := &cli.CLI{ + Args: args, + Commands: Commands, + HelpFunc: cli.BasicHelpFunc("terraform"), + } + + exitCode, err := cli.Run() + if err != nil { + fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) + return 1 + } + + return exitCode +} diff --git a/terraform/resource_provider.go b/terraform/resource_provider.go new file mode 100644 index 000000000..2440d9c36 --- /dev/null +++ b/terraform/resource_provider.go @@ -0,0 +1,21 @@ +package terraform + +// ResourceProvider is an interface that must be implemented by any +// resource provider: the thing that creates and manages the resources in +// a Terraform configuration. +type ResourceProvider interface { + // Configure configures the provider itself with the configuration + // given. This is useful for setting things like access keys. + // + // Configure returns a list of warnings and a potential error. + Configure(config map[string]interface{}) ([]string, error) + + // Resources returns all the available resource types that this provider + // knows how to manage. + Resources() []ResourceType +} + +// ResourceType is a type of resource that a resource provider can manage. +type ResourceType struct { + Name string +} diff --git a/version.go b/version.go new file mode 100644 index 000000000..b0a906a05 --- /dev/null +++ b/version.go @@ -0,0 +1,12 @@ +package main + +// The git commit that was compiled. This will be filled in by the compiler. +var GitCommit string + +// The main version number that is being run at the moment. +const Version = "0.1.0" + +// A pre-release marker for the version. If this is "" (empty string) +// then it means that it is a final release. Otherwise, this is a pre-release +// such as "dev" (in development), "beta", "rc1", etc. +const VersionPrerelease = "dev"