Load loads server aliases from the configuration file.
()
| 36 | |
| 37 | // Load loads server aliases from the configuration file. |
| 38 | func Load() (Aliases, error) { |
| 39 | configPath, err := GetConfigPath() |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | |
| 44 | aliases := make(Aliases) |
| 45 | |
| 46 | var statErr error |
| 47 | if _, statErr = os.Stat(configPath); os.IsNotExist(statErr) { |
| 48 | return aliases, nil |
| 49 | } |
| 50 | |
| 51 | configFile, err := os.ReadFile(configPath) // #nosec G304 - configPath is generated internally by GetConfigPath |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("failed to read alias config file: %w", err) |
| 54 | } |
| 55 | |
| 56 | if len(configFile) == 0 { |
| 57 | return aliases, nil |
| 58 | } |
| 59 | |
| 60 | if unmarshalErr := json.Unmarshal(configFile, &aliases); unmarshalErr != nil { |
| 61 | return nil, fmt.Errorf("failed to parse alias config file: %w", unmarshalErr) |
| 62 | } |
| 63 | |
| 64 | return aliases, nil |
| 65 | } |
| 66 | |
| 67 | // Save saves server aliases to the configuration file. |
| 68 | func Save(aliases Aliases) error { |