@@ -5,18 +5,35 @@ package tfexec
55
66import (
77 "context"
8+ "os/exec"
89 "strings"
910)
1011
12+ type workspaceListConfig struct {
13+ reattachInfo ReattachInfo
14+ }
15+
16+ var defaultWorkspaceListOptions = workspaceListConfig {}
17+
18+ type WorkspaceListOption interface {
19+ configureWorkspaceList (* workspaceListConfig )
20+ }
21+
22+ func (opt * ReattachOption ) configureWorkspaceList (conf * workspaceListConfig ) {
23+ conf .reattachInfo = opt .info
24+ }
25+
1126// WorkspaceList represents the workspace list subcommand to the Terraform CLI.
12- func (tf * Terraform ) WorkspaceList (ctx context.Context ) ([]string , string , error ) {
13- // TODO: [DIR] param option
14- wlCmd := tf .buildTerraformCmd (ctx , nil , "workspace" , "list" , "-no-color" )
27+ func (tf * Terraform ) WorkspaceList (ctx context.Context , opts ... WorkspaceListOption ) ([]string , string , error ) {
28+ wlCmd , err := tf .workspaceListCmd (ctx , opts ... )
29+ if err != nil {
30+ return nil , "" , err
31+ }
1532
1633 var outBuf strings.Builder
1734 wlCmd .Stdout = & outBuf
1835
19- err : = tf .runTerraformCmd (ctx , wlCmd )
36+ err = tf .runTerraformCmd (ctx , wlCmd )
2037 if err != nil {
2138 return nil , "" , err
2239 }
@@ -28,6 +45,25 @@ func (tf *Terraform) WorkspaceList(ctx context.Context) ([]string, string, error
2845
2946const currentWorkspacePrefix = "* "
3047
48+ func (tf * Terraform ) workspaceListCmd (ctx context.Context , opts ... WorkspaceListOption ) (* exec.Cmd , error ) {
49+ c := defaultWorkspaceListOptions
50+
51+ for _ , o := range opts {
52+ o .configureWorkspaceList (& c )
53+ }
54+
55+ mergeEnv := map [string ]string {}
56+ if c .reattachInfo != nil {
57+ reattachStr , err := c .reattachInfo .marshalString ()
58+ if err != nil {
59+ return nil , err
60+ }
61+ mergeEnv [reattachEnvVar ] = reattachStr
62+ }
63+
64+ return tf .buildTerraformCmd (ctx , mergeEnv , "workspace" , "list" , "-no-color" ), nil
65+ }
66+
3167func parseWorkspaceList (stdout string ) ([]string , string ) {
3268 lines := strings .Split (stdout , "\n " )
3369
0 commit comments