package cli import ( "context" "encoding/json" "flag" "fmt" "git.a-wan.net/alan/pipelines/internal/harness" "os" ) // choose-model is used by the installer without piping over its interactive input. func (a App) chooseModel(ctx context.Context, args []string) error { f := flag.NewFlagSet("choose-model", flag.ContinueOnError) f.SetOutput(a.Err) options := f.String("options-file", "", "JSON array of available exact model IDs") output := f.String("output", "", "Private new file for the chosen model") title := f.String("title", "Choose model", "Picker title") purpose := f.String("purpose", "", "Explanation of this model's purpose") step := f.String("step", "", "Step label such as 'Step 1 of 2'") current := f.String("current", "", "Saved model to preselect") if err := f.Parse(args); err != nil { return err } if *options == "" || *output == "" || len(f.Args()) > 0 { return fmt.Errorf("choose-model requires --options-file and --output") } data, err := os.ReadFile(*options) if err != nil { return err } var models []string if len(data) > 1<<20 || json.Unmarshal(data, &models) != nil { return fmt.Errorf("invalid model catalog") } for _, m := range models { if !harness.ValidModel(m) { return fmt.Errorf("invalid model ID %q", m) } } selected, err := harness.Pick(ctx, harness.Selection{ Title: *title, Purpose: *purpose, Step: *step, Options: models, Current: *current, }, a.In, a.Out) if err != nil { return err } file, err := os.OpenFile(*output, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) if err != nil { return err } if _, err = file.WriteString(selected + "\n"); err != nil { file.Close() return err } return file.Close() }