(fs afero.Fs, cfg ClaudeConfig)
| 785 | } |
| 786 | |
| 787 | func configureClaude(fs afero.Fs, cfg ClaudeConfig) error { |
| 788 | if cfg.ConfigPath == "" { |
| 789 | cfg.ConfigPath = filepath.Join(os.Getenv("HOME"), ".claude.json") |
| 790 | } |
| 791 | var config map[string]any |
| 792 | _, err := fs.Stat(cfg.ConfigPath) |
| 793 | if err != nil { |
| 794 | if !os.IsNotExist(err) { |
| 795 | return xerrors.Errorf("failed to stat claude config: %w", err) |
| 796 | } |
| 797 | // Touch the file to create it if it doesn't exist. |
| 798 | if err = afero.WriteFile(fs, cfg.ConfigPath, []byte(`{}`), 0o600); err != nil { |
| 799 | return xerrors.Errorf("failed to touch claude config: %w", err) |
| 800 | } |
| 801 | } |
| 802 | oldConfigBytes, err := afero.ReadFile(fs, cfg.ConfigPath) |
| 803 | if err != nil { |
| 804 | return xerrors.Errorf("failed to read claude config: %w", err) |
| 805 | } |
| 806 | err = json.Unmarshal(oldConfigBytes, &config) |
| 807 | if err != nil { |
| 808 | return xerrors.Errorf("failed to unmarshal claude config: %w", err) |
| 809 | } |
| 810 | |
| 811 | if cfg.APIKey != "" { |
| 812 | // Stops Claude from requiring the user to generate |
| 813 | // a Claude-specific API key. |
| 814 | config["primaryApiKey"] = cfg.APIKey |
| 815 | } |
| 816 | // Stops Claude from asking for onboarding. |
| 817 | config["hasCompletedOnboarding"] = true |
| 818 | // Stops Claude from asking for permissions. |
| 819 | config["bypassPermissionsModeAccepted"] = true |
| 820 | config["autoUpdaterStatus"] = "disabled" |
| 821 | // Stops Claude from asking for cost threshold. |
| 822 | config["hasAcknowledgedCostThreshold"] = true |
| 823 | |
| 824 | projects, ok := config["projects"].(map[string]any) |
| 825 | if !ok { |
| 826 | projects = make(map[string]any) |
| 827 | } |
| 828 | |
| 829 | project, ok := projects[cfg.ProjectDirectory].(map[string]any) |
| 830 | if !ok { |
| 831 | project = make(map[string]any) |
| 832 | } |
| 833 | |
| 834 | allowedTools, ok := project["allowedTools"].([]string) |
| 835 | if !ok { |
| 836 | allowedTools = []string{} |
| 837 | } |
| 838 | |
| 839 | // Add cfg.AllowedTools to the list if they're not already present. |
| 840 | for _, tool := range cfg.AllowedTools { |
| 841 | for _, existingTool := range allowedTools { |
| 842 | if tool == existingTool { |
| 843 | continue |
| 844 | } |
no test coverage detected