Skip to content

Commit 524502b

Browse files
committed
feat: allow spacectl usage in hooks
Signed-off-by: Jakub <[email protected]>
1 parent fa1efbb commit 524502b

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

internal/cmd/stack/list.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, e
162162
)
163163
}
164164

165-
result, err := searchStacks(ctx, pageInput)
165+
result, err := searchStacks[stack](ctx, pageInput)
166166
if err != nil {
167167
return nil, err
168168
}
@@ -179,6 +179,24 @@ func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, e
179179
return out, nil
180180
}
181181

182+
type hasIDAndName interface {
183+
GetID() string
184+
GetName() string
185+
}
186+
187+
type stackID struct {
188+
ID string `graphql:"id" json:"id,omitempty"`
189+
Name string `graphql:"name" json:"name,omitempty"`
190+
}
191+
192+
func (s stackID) GetID() string {
193+
return s.ID
194+
}
195+
196+
func (s stackID) GetName() string {
197+
return s.Name
198+
}
199+
182200
type stack struct {
183201
ID string `graphql:"id" json:"id,omitempty"`
184202
Administrative bool `graphql:"administrative" json:"administrative,omitempty"`
@@ -237,3 +255,11 @@ type stack struct {
237255
Name string `graphql:"name" json:"name,omitempty"`
238256
} `graphql:"workerPool" json:"workerPool,omitempty"`
239257
}
258+
259+
func (s stack) GetID() string {
260+
return s.ID
261+
}
262+
263+
func (s stack) GetName() string {
264+
return s.Name
265+
}

internal/cmd/stack/local_preview.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func localPreview() cli.ActionFunc {
3333
})
3434
}
3535

36-
stack, err := getStack(cliCtx)
36+
stack, err := getStack[stack](cliCtx)
3737
if err != nil {
3838
return err
3939
}

internal/cmd/stack/open_command.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func openCommandInBrowser(cliCtx *cli.Context) error {
6060
}
6161

6262
func findAndOpenStackInBrowser(ctx context.Context, p *stackSearchParams) error {
63-
got, err := findAndSelectStack(ctx, p, false)
63+
got, err := findAndSelectStack[stack](ctx, p, false)
6464
if errors.Is(err, errNoStackFound) {
6565
return errors.New("No stacks using the provided search parameters, maybe it's in a different subdir?")
6666
}
@@ -163,16 +163,16 @@ type stackSearchParams struct {
163163
branch *string
164164
}
165165

166-
type searchStacksResult struct {
167-
Stacks []stack
166+
type searchStacksResult[T hasIDAndName] struct {
167+
Stacks []T
168168
PageInfo structs.PageInfo
169169
}
170170

171-
func searchStacks(ctx context.Context, input structs.SearchInput) (searchStacksResult, error) {
171+
func searchStacks[T hasIDAndName](ctx context.Context, input structs.SearchInput) (searchStacksResult[T], error) {
172172
var query struct {
173173
SearchStacksOutput struct {
174174
Edges []struct {
175-
Node stack `graphql:"node"`
175+
Node T `graphql:"node"`
176176
} `graphql:"edges"`
177177
PageInfo structs.PageInfo `graphql:"pageInfo"`
178178
} `graphql:"searchStacks(input: $input)"`
@@ -184,15 +184,15 @@ func searchStacks(ctx context.Context, input structs.SearchInput) (searchStacksR
184184
map[string]interface{}{"input": input},
185185
graphql.WithHeader("Spacelift-GraphQL-Query", "StacksPage"),
186186
); err != nil {
187-
return searchStacksResult{}, errors.Wrap(err, "failed search for stacks")
187+
return searchStacksResult[T]{}, errors.Wrap(err, "failed search for stacks")
188188
}
189189

190-
stacks := make([]stack, 0)
190+
stacks := make([]T, 0)
191191
for _, q := range query.SearchStacksOutput.Edges {
192192
stacks = append(stacks, q.Node)
193193
}
194194

195-
return searchStacksResult{
195+
return searchStacksResult[T]{
196196
Stacks: stacks,
197197
PageInfo: query.SearchStacksOutput.PageInfo,
198198
}, nil

internal/cmd/stack/stack_selector.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3030
func 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

Comments
 (0)