@@ -28,18 +28,18 @@ const (
2828// 2. Check the --run flag, if set, try to get the stack associated with the run.
2929// 2. Check the current directory to determine repository and subdirectory and search for a stack.
3030func getStackID (cliCtx * cli.Context ) (string , error ) {
31- stack , err := getStack (cliCtx )
31+ stack , err := getStack [ stackID ] (cliCtx )
3232 if err != nil {
3333 return "" , err
3434 }
3535
3636 return stack .ID , nil
3737}
3838
39- func getStack (cliCtx * cli.Context ) (* stack , error ) {
39+ func getStack [ T hasIDAndName ] (cliCtx * cli.Context ) (* T , error ) {
4040 if cliCtx .IsSet (flagStackID .Name ) {
4141 stackID := cliCtx .String (flagStackID .Name )
42- stack , err := stackGetByID (cliCtx .Context , stackID )
42+ stack , err := stackGetByID [ T ] (cliCtx .Context , stackID )
4343 if errors .Is (err , errNoStackFound ) {
4444 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 )
4545 }
@@ -50,7 +50,7 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
5050 return stack , nil
5151 } else if cliCtx .IsSet (flagRun .Name ) {
5252 runID := cliCtx .String (flagRun .Name )
53- stack , err := stackGetByRunID (cliCtx .Context , runID )
53+ stack , err := stackGetByRunID [ T ] (cliCtx .Context , runID )
5454 if errors .Is (err , errNoStackFound ) {
5555 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 )
5656 }
@@ -73,7 +73,7 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
7373
7474 skip := os .Getenv (envPromptSkipKey ) == "true"
7575
76- got , err := findAndSelectStack (cliCtx .Context , & stackSearchParams {
76+ got , err := findAndSelectStack [ T ] (cliCtx .Context , & stackSearchParams {
7777 count : 50 ,
7878 projectRoot : & subdir ,
7979 repositoryName : name ,
@@ -89,11 +89,9 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
8989 return got , nil
9090}
9191
92- func stackGetByID (ctx context.Context , stackID string ) (* stack , error ) {
92+ func stackGetByID [ T hasIDAndName ] (ctx context.Context , stackID string ) (* T , error ) {
9393 var query struct {
94- Stack struct {
95- stack
96- } `graphql:"stack(id: $id)"`
94+ Stack T `graphql:"stack(id: $id)"`
9795 }
9896
9997 variables := map [string ]interface {}{
@@ -105,18 +103,16 @@ func stackGetByID(ctx context.Context, stackID string) (*stack, error) {
105103 return nil , fmt .Errorf ("failed to query GraphQL API when checking if a stack exists: %w" , err )
106104 }
107105
108- if query .Stack .ID != stackID {
106+ if query .Stack .GetID () != stackID {
109107 return nil , errNoStackFound
110108 }
111109
112- return & query .Stack . stack , nil
110+ return & query .Stack , nil
113111}
114112
115- func stackGetByRunID (ctx context.Context , runID string ) (* stack , error ) {
113+ func stackGetByRunID [ T hasIDAndName ] (ctx context.Context , runID string ) (* T , error ) {
116114 var query struct {
117- RunStack struct {
118- stack
119- } `graphql:"runStack(runId: $runId)"`
115+ RunStack T `graphql:"runStack(runId: $runId)"`
120116 }
121117
122118 variables := map [string ]interface {}{
@@ -132,10 +128,10 @@ func stackGetByRunID(ctx context.Context, runID string) (*stack, error) {
132128 return nil , fmt .Errorf ("failed to query GraphQL API when getting stack by run id: %w" , err )
133129 }
134130
135- return & query .RunStack . stack , nil
131+ return & query .RunStack , nil
136132}
137133
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 ) {
139135 conditions := []structs.QueryPredicate {
140136 {
141137 Field : graphql .String ("repository" ),
@@ -169,16 +165,16 @@ func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt b
169165 Predicates : & conditions ,
170166 }
171167
172- result , err := searchStacks (ctx , input )
168+ result , err := searchStacks [ T ] (ctx , input )
173169 if err != nil {
174170 return nil , err
175171 }
176172
177173 items := []string {}
178- found := map [string ]stack {}
174+ found := map [string ]T {}
179175 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
182178 }
183179
184180 if len (found ) == 0 {
0 commit comments