github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/workspace_list.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/posener/complete"
     9  )
    10  
    11  type WorkspaceListCommand struct {
    12  	Meta
    13  	LegacyName bool
    14  }
    15  
    16  func (c *WorkspaceListCommand) Run(args []string) int {
    17  	args, err := c.Meta.process(args, true)
    18  	if err != nil {
    19  		return 1
    20  	}
    21  
    22  	envCommandShowWarning(c.Ui, c.LegacyName)
    23  
    24  	cmdFlags := c.Meta.flagSet("workspace list")
    25  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    26  	if err := cmdFlags.Parse(args); err != nil {
    27  		return 1
    28  	}
    29  
    30  	args = cmdFlags.Args()
    31  	configPath, err := ModulePath(args)
    32  	if err != nil {
    33  		c.Ui.Error(err.Error())
    34  		return 1
    35  	}
    36  
    37  	cfg, err := c.Config(configPath)
    38  	if err != nil {
    39  		c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
    40  		return 1
    41  	}
    42  
    43  	// Load the backend
    44  	b, err := c.Backend(&BackendOpts{
    45  		Config: cfg,
    46  	})
    47  
    48  	if err != nil {
    49  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    50  		return 1
    51  	}
    52  
    53  	states, err := b.States()
    54  	if err != nil {
    55  		c.Ui.Error(err.Error())
    56  		return 1
    57  	}
    58  
    59  	env, isOverridden := c.WorkspaceOverridden()
    60  
    61  	var out bytes.Buffer
    62  	for _, s := range states {
    63  		if s == env {
    64  			out.WriteString("* ")
    65  		} else {
    66  			out.WriteString("  ")
    67  		}
    68  		out.WriteString(s + "\n")
    69  	}
    70  
    71  	c.Ui.Output(out.String())
    72  
    73  	if isOverridden {
    74  		c.Ui.Output(envIsOverriddenNote)
    75  	}
    76  
    77  	return 0
    78  }
    79  
    80  func (c *WorkspaceListCommand) AutocompleteArgs() complete.Predictor {
    81  	return complete.PredictDirs("")
    82  }
    83  
    84  func (c *WorkspaceListCommand) AutocompleteFlags() complete.Flags {
    85  	return nil
    86  }
    87  
    88  func (c *WorkspaceListCommand) Help() string {
    89  	helpText := `
    90  Usage: terraform workspace list [DIR]
    91  
    92    List Terraform workspaces.
    93  `
    94  	return strings.TrimSpace(helpText)
    95  }
    96  
    97  func (c *WorkspaceListCommand) Synopsis() string {
    98  	return "List Workspaces"
    99  }