MCPcopy Create free account
hub / github.com/github/gh-aw / collectInputsWithMap

Function collectInputsWithMap

pkg/cli/run_interactive.go:271–333  ·  view source on GitHub ↗

collectInputsWithMap collects inputs using a map to properly capture values

(ctx context.Context, inputs map[string]*workflow.InputDefinition)

Source from the content-addressed store, hash-verified

269
270// collectInputsWithMap collects inputs using a map to properly capture values
271func collectInputsWithMap(ctx context.Context, inputs map[string]*workflow.InputDefinition) ([]string, error) {
272 // Create a map to store string values for the form
273 inputValues := make(map[string]string)
274 // Create a map to track the string pointers we'll pass to huh
275 inputPtrs := make(map[string]*string)
276 var formGroups []*huh.Group
277
278 // Create input fields for each workflow input
279 for name, input := range inputs {
280 inputName := name
281 inputDef := input
282
283 // Initialize with default value (convert any to string)
284 defaultStr := ""
285 if inputDef.Default != nil {
286 defaultStr = fmt.Sprintf("%v", inputDef.Default)
287 }
288 inputValues[inputName] = defaultStr
289
290 // Create a string variable for this input that huh can update
291 valueStr := defaultStr
292 inputPtrs[inputName] = &valueStr
293
294 // Create input field that updates the string variable
295 field := huh.NewInput().
296 Title(fmt.Sprintf("Enter value for '%s'", inputName)).
297 Value(inputPtrs[inputName])
298
299 if inputDef.Description != "" {
300 field = field.Description(inputDef.Description)
301 }
302
303 if inputDef.Required {
304 field = field.Validate(func(s string) error {
305 if s == "" {
306 return errors.New("this input is required")
307 }
308 return nil
309 })
310 }
311
312 group := huh.NewGroup(field)
313 formGroups = append(formGroups, group)
314 }
315
316 // Show the form
317 form := huh.NewForm(formGroups...).WithTheme(styles.HuhTheme).WithAccessible(console.IsAccessibleMode())
318 if err := form.RunWithContext(ctx); err != nil {
319 return nil, fmt.Errorf("input collection cancelled: %w", err)
320 }
321
322 // Collect the final values from the pointers
323 var result []string
324 for name, valuePtr := range inputPtrs {
325 value := *valuePtr
326 if value != "" {
327 result = append(result, fmt.Sprintf("%s=%s", name, value))
328 }

Callers 1

collectWorkflowInputsFunction · 0.85

Calls 4

IsAccessibleModeFunction · 0.92
ValidateMethod · 0.45
ErrorfMethod · 0.45
PrintfMethod · 0.45

Tested by

no test coverage detected