Skip to content

Commit bf1b210

Browse files
authored
feat: Include autoattach contexts when listing stack env (#274)
1 parent 4bb59fe commit bf1b210

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

internal/cmd/stack/environment.go

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ type listEnvElementOutput struct {
4646
Name string `json:"name"`
4747
Type string `json:"type"`
4848
Value *string `json:"value"`
49+
4950
// Context specifies the name of the context.
50-
Context *string `json:"context"`
51+
Context *string `json:"context"`
52+
IsAutoattached *bool `json:"isAutoattached"`
53+
5154
// Runtime is not printed, it's just used to determine output formatting.
5255
Runtime bool `json:"runtime"`
5356
// WriteOnly is not printed, it's just used to determine output formatting.
@@ -64,7 +67,14 @@ type runtimeConfig struct {
6467

6568
type listEnvQuery struct {
6669
Stack struct {
67-
RuntimeConfig []runtimeConfig `graphql:"runtimeConfig" json:"runtimeConfig"`
70+
RuntimeConfig []runtimeConfig `graphql:"runtimeConfig" json:"runtimeConfig"`
71+
AttachedContexts []struct {
72+
ContextID string `graphql:"contextId" json:"contextId,omitempty"`
73+
Name string `graphql:"contextName" json:"name,omitempty"`
74+
Priority int `graphql:"priority" json:"priority,omitempty"`
75+
IsAutoattached bool `graphql:"isAutoattached" json:"isAutoattached"`
76+
Config []configElement `graphql:"config" json:"config,omitempty"`
77+
} `graphql:"attachedContexts"`
6878
} `graphql:"stack(id: $stack)" json:"stack"`
6979
}
7080

@@ -140,16 +150,37 @@ func (e *listEnvCommand) listEnv(cliCtx *cli.Context) error {
140150
for _, config := range query.Stack.RuntimeConfig {
141151
config := config
142152
var contextName *string
153+
var isAutoAttached *bool
143154
if config.Context != nil {
144155
contextName = &config.Context.ContextName
156+
157+
f := false
158+
isAutoAttached = &f
145159
}
146-
if element, err := config.Element.toConfigElementOutput(contextName); err == nil {
160+
161+
if element, err := config.Element.toConfigElementOutput(contextName, isAutoAttached); err == nil {
147162
elements = append(elements, element)
148163
} else {
149164
return err
150165
}
151166
}
152167

168+
for _, spcCtx := range query.Stack.AttachedContexts {
169+
// If the context is not autoattached, we will get it with the whole config.
170+
// If it's autoattached, we have to specifically list and attach it.
171+
if !spcCtx.IsAutoattached {
172+
continue
173+
}
174+
175+
for _, config := range spcCtx.Config {
176+
if element, err := config.toConfigElementOutput(&spcCtx.Name, &spcCtx.IsAutoattached); err == nil {
177+
elements = append(elements, element)
178+
} else {
179+
return err
180+
}
181+
}
182+
}
183+
153184
switch outputFormat {
154185
case cmd.OutputFormatTable:
155186
return e.showOutputsTable(elements)
@@ -161,7 +192,7 @@ func (e *listEnvCommand) listEnv(cliCtx *cli.Context) error {
161192
}
162193

163194
func (e *listEnvCommand) showOutputsTable(outputs []listEnvElementOutput) error {
164-
tableData := [][]string{{"Name", "Type", "Value", "Context"}}
195+
tableData := [][]string{{"Name", "Type", "Value", "Context", "IsAutoattached"}}
165196
for _, output := range outputs {
166197
var row []string
167198

@@ -189,6 +220,12 @@ func (e *listEnvCommand) showOutputsTable(outputs []listEnvElementOutput) error
189220
row = append(row, "")
190221
}
191222

223+
if output.IsAutoattached != nil {
224+
row = append(row, fmt.Sprintf("%v", *output.IsAutoattached))
225+
} else {
226+
row = append(row, "")
227+
}
228+
192229
tableData = append(tableData, row)
193230
}
194231
return cmd.OutputTable(tableData, true)
@@ -198,7 +235,7 @@ func (e *listEnvCommand) showOutputsJSON(outputs []listEnvElementOutput) error {
198235
return cmd.OutputJSON(outputs)
199236
}
200237

201-
func (e *configElement) toConfigElementOutput(contextName *string) (listEnvElementOutput, error) {
238+
func (e *configElement) toConfigElementOutput(contextName *string, isAutoAttached *bool) (listEnvElementOutput, error) {
202239
var value = e.Value
203240

204241
if e.Type == fileTypeConfig && e.Value != nil {
@@ -214,12 +251,13 @@ func (e *configElement) toConfigElementOutput(contextName *string) (listEnvEleme
214251
}
215252

216253
return listEnvElementOutput{
217-
Name: e.ID,
218-
Type: string(e.Type),
219-
Value: value,
220-
Context: contextName,
221-
Runtime: e.Runtime,
222-
WriteOnly: e.WriteOnly,
254+
Name: e.ID,
255+
Type: string(e.Type),
256+
Value: value,
257+
Context: contextName,
258+
IsAutoattached: isAutoAttached,
259+
Runtime: e.Runtime,
260+
WriteOnly: e.WriteOnly,
223261
}, nil
224262
}
225263

0 commit comments

Comments
 (0)