github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/workspace_list.go (about)

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