package main import ( "encoding/json" "os" "path/filepath" ) // AgentConfig holds user-editable settings for the agent. type AgentConfig struct { Server string `json:"server"` HeadscaleURL string `json:"headscale_url"` HeadscaleAuthKey string `json:"headscale_auth_key"` NodeID string `json:"node_id"` DataDir string `json:"data_dir"` } const configFileName = "studioE5-config.json" // defaultConfig returns sensible defaults for a first run. func defaultConfig(dataDir string) *AgentConfig { return &AgentConfig{ Server: "ws://localhost:3001", HeadscaleURL: "", HeadscaleAuthKey: "", NodeID: defaultNodeID(), DataDir: dataDir, } } // configPath returns the absolute path to the config file. func configPath(dataDir string) string { return filepath.Join(dataDir, configFileName) } // loadOrCreateConfig loads the config file. If it does not exist, it creates // one with default values and returns it (the caller can then open the settings UI). func loadOrCreateConfig(dataDir string) (*AgentConfig, bool, error) { cp := configPath(dataDir) if _, err := os.Stat(cp); err == nil { data, err := os.ReadFile(cp) if err != nil { return nil, false, err } var cfg AgentConfig if err := json.Unmarshal(data, &cfg); err != nil { return nil, false, err } return &cfg, false, nil } cfg := defaultConfig(dataDir) if err := saveConfig(dataDir, cfg); err != nil { return nil, true, err } return cfg, true, nil } // saveConfig writes the config file to disk. func saveConfig(dataDir string, cfg *AgentConfig) error { cp := configPath(dataDir) data, err := json.MarshalIndent(cfg, "", " ") if err != nil { return err } return os.WriteFile(cp, data, 0644) }