@@ -28,18 +28,18 @@ const (
28
28
// 2. Check the --run flag, if set, try to get the stack associated with the run.
29
29
// 2. Check the current directory to determine repository and subdirectory and search for a stack.
30
30
func getStackID (cliCtx * cli.Context ) (string , error ) {
31
- stack , err := getStack (cliCtx )
31
+ stack , err := getStack [ stackID ] (cliCtx )
32
32
if err != nil {
33
33
return "" , err
34
34
}
35
35
36
36
return stack .ID , nil
37
37
}
38
38
39
- func getStack (cliCtx * cli.Context ) (* stack , error ) {
39
+ func getStack [ T hasIDAndName ] (cliCtx * cli.Context ) (* T , error ) {
40
40
if cliCtx .IsSet (flagStackID .Name ) {
41
41
stackID := cliCtx .String (flagStackID .Name )
42
- stack , err := stackGetByID (cliCtx .Context , stackID )
42
+ stack , err := stackGetByID [ T ] (cliCtx .Context , stackID )
43
43
if errors .Is (err , errNoStackFound ) {
44
44
return nil , fmt .Errorf ("stack with id %q could not be found. Please check that the stack exists and that you have access to it. To list available stacks run: spacectl stack list" , stackID )
45
45
}
@@ -50,7 +50,7 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
50
50
return stack , nil
51
51
} else if cliCtx .IsSet (flagRun .Name ) {
52
52
runID := cliCtx .String (flagRun .Name )
53
- stack , err := stackGetByRunID (cliCtx .Context , runID )
53
+ stack , err := stackGetByRunID [ T ] (cliCtx .Context , runID )
54
54
if errors .Is (err , errNoStackFound ) {
55
55
return nil , fmt .Errorf ("run with id %q was not found. Please check that the run exists and that you have access to it. To list available stacks run: spacectl stack run list" , runID )
56
56
}
@@ -73,7 +73,7 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
73
73
74
74
skip := os .Getenv (envPromptSkipKey ) == "true"
75
75
76
- got , err := findAndSelectStack (cliCtx .Context , & stackSearchParams {
76
+ got , err := findAndSelectStack [ T ] (cliCtx .Context , & stackSearchParams {
77
77
count : 50 ,
78
78
projectRoot : & subdir ,
79
79
repositoryName : name ,
@@ -89,11 +89,9 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
89
89
return got , nil
90
90
}
91
91
92
- func stackGetByID (ctx context.Context , stackID string ) (* stack , error ) {
92
+ func stackGetByID [ T hasIDAndName ] (ctx context.Context , stackID string ) (* T , error ) {
93
93
var query struct {
94
- Stack struct {
95
- stack
96
- } `graphql:"stack(id: $id)"`
94
+ Stack T `graphql:"stack(id: $id)"`
97
95
}
98
96
99
97
variables := map [string ]interface {}{
@@ -105,18 +103,16 @@ func stackGetByID(ctx context.Context, stackID string) (*stack, error) {
105
103
return nil , fmt .Errorf ("failed to query GraphQL API when checking if a stack exists: %w" , err )
106
104
}
107
105
108
- if query .Stack .ID != stackID {
106
+ if query .Stack .GetID () != stackID {
109
107
return nil , errNoStackFound
110
108
}
111
109
112
- return & query .Stack . stack , nil
110
+ return & query .Stack , nil
113
111
}
114
112
115
- func stackGetByRunID (ctx context.Context , runID string ) (* stack , error ) {
113
+ func stackGetByRunID [ T hasIDAndName ] (ctx context.Context , runID string ) (* T , error ) {
116
114
var query struct {
117
- RunStack struct {
118
- stack
119
- } `graphql:"runStack(runId: $runId)"`
115
+ RunStack T `graphql:"runStack(runId: $runId)"`
120
116
}
121
117
122
118
variables := map [string ]interface {}{
@@ -132,10 +128,10 @@ func stackGetByRunID(ctx context.Context, runID string) (*stack, error) {
132
128
return nil , fmt .Errorf ("failed to query GraphQL API when getting stack by run id: %w" , err )
133
129
}
134
130
135
- return & query .RunStack . stack , nil
131
+ return & query .RunStack , nil
136
132
}
137
133
138
- func findAndSelectStack (ctx context.Context , p * stackSearchParams , forcePrompt bool ) (* stack , error ) {
134
+ func findAndSelectStack [ T hasIDAndName ] (ctx context.Context , p * stackSearchParams , forcePrompt bool ) (* T , error ) {
139
135
conditions := []structs.QueryPredicate {
140
136
{
141
137
Field : graphql .String ("repository" ),
@@ -169,16 +165,16 @@ func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt b
169
165
Predicates : & conditions ,
170
166
}
171
167
172
- result , err := searchStacks (ctx , input )
168
+ result , err := searchStacks [ T ] (ctx , input )
173
169
if err != nil {
174
170
return nil , err
175
171
}
176
172
177
173
items := []string {}
178
- found := map [string ]stack {}
174
+ found := map [string ]T {}
179
175
for _ , s := range result .Stacks {
180
- items = append (items , s .Name )
181
- found [s .Name ] = s
176
+ items = append (items , s .GetName () )
177
+ found [s .GetName () ] = s
182
178
}
183
179
184
180
if len (found ) == 0 {
0 commit comments