WIP: Add websocket support

This commit is contained in:
Owen Schwartz
2024-11-10 22:04:36 -05:00
parent b348c768c7
commit 2e5531b4a5
6 changed files with 515 additions and 151 deletions

57
websocket/config.go Normal file
View File

@@ -0,0 +1,57 @@
package websocket
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
)
func getConfigPath() string {
var configDir string
switch runtime.GOOS {
case "darwin":
configDir = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "newt-client")
case "windows":
configDir = filepath.Join(os.Getenv("APPDATA"), "newt-client")
default: // linux and others
configDir = filepath.Join(os.Getenv("HOME"), ".config", "newt-client")
}
if err := os.MkdirAll(configDir, 0755); err != nil {
log.Printf("Failed to create config directory: %v", err)
}
return filepath.Join(configDir, "config.json")
}
func (c *Client) loadConfig() error {
configPath := getConfigPath()
data, err := ioutil.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
return err
}
// Only update token from saved config
c.config.Token = config.Token
return nil
}
func (c *Client) saveConfig() error {
configPath := getConfigPath()
data, err := json.MarshalIndent(c.config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(configPath, data, 0644)
}