SaveConfigToFile writes the config to the given path in YAML format.
(cfg *config.Config, path string)
| 717 | |
| 718 | // SaveConfigToFile writes the config to the given path in YAML format. |
| 719 | func SaveConfigToFile(cfg *config.Config, path string) error { |
| 720 | // Check if SOPS is being used (read file to check) |
| 721 | data, err := os.ReadFile(path) |
| 722 | isSOPS := false |
| 723 | if err == nil { |
| 724 | isSOPS = config.IsSOPSEncrypted(path, data) |
| 725 | } |
| 726 | |
| 727 | // If not using SOPS, encrypt sensitive fields before saving |
| 728 | if !isSOPS { |
| 729 | // Create a copy of the config to encrypt (don't modify the original) |
| 730 | cfgCopy := *cfg |
| 731 | cfgCopy.Profiles = make(map[string]config.ProfileConfig) |
| 732 | for k, v := range cfg.Profiles { |
| 733 | cfgCopy.Profiles[k] = v |
| 734 | } |
| 735 | |
| 736 | // Encrypt sensitive fields |
| 737 | if err := config.EncryptConfigSensitiveFields(&cfgCopy); err != nil { |
| 738 | // Log warning but continue - allow saving even if encryption fails |
| 739 | // This allows users to save cleartext if they prefer |
| 740 | if config.DebugEnabled { |
| 741 | fmt.Println(display.IconText("⚠️", fmt.Sprintf("Warning: Failed to encrypt some fields: %v", err), cfg.ShowIcons)) |
| 742 | } |
| 743 | } else { |
| 744 | cfg = &cfgCopy |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // Use the config package's YAML marshaling |
| 749 | data, err = yaml.Marshal(cfg) |
| 750 | if err != nil { |
| 751 | return err |
| 752 | } |
| 753 | |
| 754 | // Ensure the target directory exists using OS-agnostic path operations |
| 755 | dir := filepath.Dir(path) |
| 756 | if err := os.MkdirAll(dir, 0o750); err != nil { |
| 757 | return err |
| 758 | } |
| 759 | |
| 760 | return os.WriteFile(path, data, 0o600) |
| 761 | } |
| 762 | |
| 763 | // LaunchConfigWizard launches the configuration wizard and returns the result. |
| 764 | func LaunchConfigWizard(cfg *config.Config, configPath string, activeProfile string) WizardResult { |
no test coverage detected