|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +const defaultServerPort = 5533 |
| 15 | + |
| 16 | +var errInvalidServerPort = errors.New("server port must be between 1 and 65535") |
| 17 | + |
| 18 | +func validateServerPort(port int) error { |
| 19 | + if port < 1 || port > 65535 { |
| 20 | + return fmt.Errorf("%w: %d", errInvalidServerPort, port) |
| 21 | + } |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// desktopConfigPath returns the service config file in the app data directory. |
| 26 | +func desktopConfigPath(dataDir string) string { |
| 27 | + return filepath.Join(dataDir, "config.yaml") |
| 28 | +} |
| 29 | + |
| 30 | +// ensureDesktopConfig creates the config with the desktop default port, or |
| 31 | +// adds server.port when an existing config does not specify it. Existing |
| 32 | +// settings and YAML comments are retained through the YAML node representation. |
| 33 | +func ensureDesktopConfig(path string) error { |
| 34 | + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 35 | + return fmt.Errorf("create config directory: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + data, err := os.ReadFile(path) |
| 39 | + if errors.Is(err, os.ErrNotExist) { |
| 40 | + data = []byte("server:\n port: " + strconv.Itoa(defaultServerPort) + "\n") |
| 41 | + if err := os.WriteFile(path, data, 0o600); err != nil { |
| 42 | + return fmt.Errorf("write default config: %w", err) |
| 43 | + } |
| 44 | + return nil |
| 45 | + } |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("read config: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + var document yaml.Node |
| 51 | + if err := yaml.Unmarshal(data, &document); err != nil { |
| 52 | + return fmt.Errorf("parse config %s: %w", path, err) |
| 53 | + } |
| 54 | + if len(document.Content) == 0 { |
| 55 | + document.Content = []*yaml.Node{{Kind: yaml.MappingNode, Tag: "!!map"}} |
| 56 | + } |
| 57 | + root := document.Content[0] |
| 58 | + if root.Kind != yaml.MappingNode { |
| 59 | + return fmt.Errorf("invalid config %s: top level must be a mapping", path) |
| 60 | + } |
| 61 | + |
| 62 | + server := mappingValue(root, "server") |
| 63 | + if server == nil { |
| 64 | + server = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"} |
| 65 | + appendMappingPair(root, "server", server) |
| 66 | + } |
| 67 | + if server.Kind != yaml.MappingNode { |
| 68 | + return fmt.Errorf("invalid config %s: server must be a mapping", path) |
| 69 | + } |
| 70 | + |
| 71 | + port := mappingValue(server, "port") |
| 72 | + if port != nil { |
| 73 | + if port.Kind != yaml.ScalarNode || port.Tag != "!!int" { |
| 74 | + return fmt.Errorf("invalid server.port in %s: must be an integer", path) |
| 75 | + } |
| 76 | + value, err := strconv.Atoi(strings.TrimSpace(port.Value)) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("invalid server.port in %s: %w", path, err) |
| 79 | + } |
| 80 | + if err := validateServerPort(value); err != nil { |
| 81 | + return fmt.Errorf("invalid server.port in %s: %w", path, err) |
| 82 | + } |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + appendMappingPair(server, "port", &yaml.Node{ |
| 87 | + Kind: yaml.ScalarNode, |
| 88 | + Tag: "!!int", |
| 89 | + Value: strconv.Itoa(defaultServerPort), |
| 90 | + }) |
| 91 | + updated, err := yaml.Marshal(&document) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("encode config %s: %w", path, err) |
| 94 | + } |
| 95 | + if err := os.WriteFile(path, updated, 0o600); err != nil { |
| 96 | + return fmt.Errorf("save config %s: %w", path, err) |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// setServerPort writes a validated port into the existing server.port field, |
| 102 | +// preserving other YAML configuration and comments. |
| 103 | +func setServerPort(path string, port int) error { |
| 104 | + if err := validateServerPort(port); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if err := ensureDesktopConfig(path); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + data, err := os.ReadFile(path) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("read config: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + var document yaml.Node |
| 117 | + if err := yaml.Unmarshal(data, &document); err != nil { |
| 118 | + return fmt.Errorf("parse config %s: %w", path, err) |
| 119 | + } |
| 120 | + if len(document.Content) == 0 || document.Content[0].Kind != yaml.MappingNode { |
| 121 | + return fmt.Errorf("invalid config %s: top level must be a mapping", path) |
| 122 | + } |
| 123 | + server := mappingValue(document.Content[0], "server") |
| 124 | + if server == nil || server.Kind != yaml.MappingNode { |
| 125 | + return fmt.Errorf("invalid config %s: server must be a mapping", path) |
| 126 | + } |
| 127 | + portNode := mappingValue(server, "port") |
| 128 | + if portNode == nil { |
| 129 | + appendMappingPair(server, "port", &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.Itoa(port)}) |
| 130 | + } else { |
| 131 | + portNode.Kind = yaml.ScalarNode |
| 132 | + portNode.Tag = "!!int" |
| 133 | + portNode.Value = strconv.Itoa(port) |
| 134 | + portNode.Style = 0 |
| 135 | + } |
| 136 | + |
| 137 | + updated, err := yaml.Marshal(&document) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("encode config %s: %w", path, err) |
| 140 | + } |
| 141 | + if err := os.WriteFile(path, updated, 0o600); err != nil { |
| 142 | + return fmt.Errorf("save config %s: %w", path, err) |
| 143 | + } |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func mappingValue(mapping *yaml.Node, key string) *yaml.Node { |
| 148 | + for i := 0; i+1 < len(mapping.Content); i += 2 { |
| 149 | + if mapping.Content[i].Value == key { |
| 150 | + return mapping.Content[i+1] |
| 151 | + } |
| 152 | + } |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func appendMappingPair(mapping *yaml.Node, key string, value *yaml.Node) { |
| 157 | + mapping.Content = append(mapping.Content, |
| 158 | + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: key}, |
| 159 | + value, |
| 160 | + ) |
| 161 | +} |
0 commit comments