Add config path argument to env commands

In order to operate in parity with other commands, the env command
should take a path argument to locate the configuration.

This however introduces the issue of a possible name conflict between a
path and subcommand, or printing an incorrect current environment for
the bare `env` command. In favor of simplicity this removes the current
env output and only prints usage when no subcommand is provided.
This commit is contained in:
James Bardin 2017-02-23 14:14:51 -05:00
parent c8526484b3
commit 06663991d1
5 changed files with 36 additions and 45 deletions

View File

@ -1,12 +1,6 @@
package command
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/backend"
"github.com/mitchellh/cli"
)
import "strings"
// EnvCommand is a Command Implementation that manipulates local state
// environments.
@ -19,34 +13,7 @@ func (c *EnvCommand) Run(args []string) int {
cmdFlags := c.Meta.flagSet("env")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) > 0 {
c.Ui.Error("0 arguments expected.\n")
return cli.RunResultHelp
}
// Load the backend
b, err := c.Backend(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
return 1
}
multi, ok := b.(backend.MultiState)
if !ok {
c.Ui.Error(envNotSupported)
return 1
}
_, current, err := multi.States()
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(fmt.Sprintf("Current environment is %q\n", current))
c.Ui.Output(c.Help())
return 0
}

View File

@ -26,15 +26,21 @@ func (c *EnvDeleteCommand) Run(args []string) int {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
if len(args) == 0 {
c.Ui.Error("expected NAME.\n")
return cli.RunResultHelp
}
delEnv := args[0]
configPath, err := ModulePath(args[1:])
if err != nil {
c.Ui.Error(err.Error())
return 1
}
// Load the backend
b, err := c.Backend(nil)
b, err := c.Backend(&BackendOpts{ConfigPath: configPath})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
return 1
@ -134,7 +140,7 @@ func (c *EnvDeleteCommand) Run(args []string) int {
}
func (c *EnvDeleteCommand) Help() string {
helpText := `
Usage: terraform env delete [OPTIONS] NAME
Usage: terraform env delete [OPTIONS] NAME [DIR]
Delete a Terraform environment

View File

@ -21,8 +21,14 @@ func (c *EnvListCommand) Run(args []string) int {
return 1
}
configPath, err := ModulePath(args)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
// Load the backend
b, err := c.Backend(nil)
b, err := c.Backend(&BackendOpts{ConfigPath: configPath})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
return 1
@ -56,7 +62,7 @@ func (c *EnvListCommand) Run(args []string) int {
func (c *EnvListCommand) Help() string {
helpText := `
Usage: terraform env list
Usage: terraform env list [DIR]
List Terraform environments.
`

View File

@ -29,15 +29,21 @@ func (c *EnvNewCommand) Run(args []string) int {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
if len(args) == 0 {
c.Ui.Error("expected NAME.\n")
return cli.RunResultHelp
}
newEnv := args[0]
configPath, err := ModulePath(args[1:])
if err != nil {
c.Ui.Error(err.Error())
return 1
}
// Load the backend
b, err := c.Backend(nil)
b, err := c.Backend(&BackendOpts{ConfigPath: configPath})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
return 1
@ -116,7 +122,7 @@ func (c *EnvNewCommand) Run(args []string) int {
func (c *EnvNewCommand) Help() string {
helpText := `
Usage: terraform env new [OPTIONS] NAME
Usage: terraform env new [OPTIONS] NAME [DIR]
Create a new Terraform environment.

View File

@ -21,13 +21,19 @@ func (c *EnvSelectCommand) Run(args []string) int {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
if len(args) == 0 {
c.Ui.Error("expected NAME.\n")
return cli.RunResultHelp
}
configPath, err := ModulePath(args[1:])
if err != nil {
c.Ui.Error(err.Error())
return 1
}
// Load the backend
b, err := c.Backend(nil)
b, err := c.Backend(&BackendOpts{ConfigPath: configPath})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
return 1
@ -81,7 +87,7 @@ func (c *EnvSelectCommand) Run(args []string) int {
func (c *EnvSelectCommand) Help() string {
helpText := `
Usage: terraform env select NAME
Usage: terraform env select NAME [DIR]
Change Terraform environment.
`