Implement CLI, version command

This commit is contained in:
Mitchell Hashimoto 2014-05-24 12:04:43 -07:00
parent 6983a0414a
commit a4a4e3784d
7 changed files with 155 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.dll
*.exe
example.tf
vendor/

39
command/version.go Normal file
View File

@ -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"
}

11
command/version_test.go Normal file
View File

@ -0,0 +1,11 @@
package command
import (
"testing"
"github.com/mitchellh/cli"
)
func TestVersionCommand_implements(t *testing.T) {
var _ cli.Command = &VersionCommand{}
}

26
commands.go Normal file
View File

@ -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
},
}
}

45
main.go Normal file
View File

@ -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
}

View File

@ -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
}

12
version.go Normal file
View File

@ -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"