applyCommand resolves the tool/provider/model triple exactly like launch (interactive wizard on TTY, auto-resolve otherwise), then writes the tool's native config file — and stops. It does NOT exec the agent. This is the cc-switch "switch" operation: point an agent at a provider without running it.
(state *globalState)
| 17 | // native config file — and stops. It does NOT exec the agent. This is the |
| 18 | // cc-switch "switch" operation: point an agent at a provider without running it. |
| 19 | func (a *App) applyCommand(state *globalState) *cobra.Command { |
| 20 | var ( |
| 21 | endpointName string |
| 22 | modelName string |
| 23 | ) |
| 24 | cmd := &cobra.Command{ |
| 25 | Use: "apply [TOOL]", |
| 26 | Aliases: []string{"ap"}, |
| 27 | Short: "Write a provider's config into an agent's config file without launching it", |
| 28 | Args: cobra.MaximumNArgs(1), |
| 29 | RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | registry, err := tools.LoadDefault() |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | var positionalTool string |
| 36 | if len(args) > 0 { |
| 37 | positionalTool = args[0] |
| 38 | } |
| 39 | |
| 40 | // Validate the positional tool name BEFORE touching providers |
| 41 | // so an unknown name surfaces the right error even when no |
| 42 | // providers config exists. |
| 43 | pinned := launchSelection{ |
| 44 | EndpointName: endpointName, |
| 45 | Model: modelName, |
| 46 | } |
| 47 | if positionalTool != "" { |
| 48 | tool, ok := lookupTool(registry, positionalTool) |
| 49 | if !ok { |
| 50 | return fmt.Errorf("Unknown tool: %s", positionalTool) |
| 51 | } |
| 52 | pinned.Tool = tool |
| 53 | } |
| 54 | |
| 55 | file, perr := makeProviderAPI(state).File(context.Background()) |
| 56 | if perr != nil { |
| 57 | return perr |
| 58 | } |
| 59 | |
| 60 | sel, cancelled, err := resolveLaunchSelection( |
| 61 | cmd.OutOrStdout(), |
| 62 | cmd.ErrOrStderr(), |
| 63 | file, registry, pinned, |
| 64 | ) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | if cancelled { |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | apiKey := providers.ResolveAPIKey(sel.Endpoint, os.Getenv) |
| 73 | |
| 74 | path, werr := tools.WriteConfig(sel.Tool, sel.Endpoint, sel.EndpointName, sel.Model, apiKey) |
| 75 | if werr != nil { |
| 76 | return fmt.Errorf("apply: write %s config: %w", sel.Tool.Name, werr) |
no test coverage detected