From: Jeff Halter Date: Thu, 29 Jul 2021 01:17:13 +0000 (-0700) Subject: Add per-OS default config path to accomodate Windows X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/95753255a17026750f5b53cc470999785e0c515f?ds=inline Add per-OS default config path to accomodate Windows --- diff --git a/client/main.go b/client/main.go index 3ce4c7e..ec6f895 100644 --- a/client/main.go +++ b/client/main.go @@ -11,10 +11,10 @@ import ( "log" "os" "os/signal" + "runtime" "syscall" ) - func main() { _, cancelRoot := context.WithCancel(context.Background()) @@ -70,7 +70,9 @@ func main() { cancelRoot() }() - client := hotline.NewClient("", logger) + cfgPath := defaultConfigPath() + + client := hotline.NewClient(cfgPath, logger) client.DebugBuf = db client.UI.Start() @@ -94,3 +96,19 @@ var zapLogLevel = map[string]zapcore.Level{ "warn": zap.WarnLevel, "error": zap.ErrorLevel, } + +func defaultConfigPath() (cfgPath string) { + os := runtime.GOOS + switch os { + case "windows": + cfgPath = "mobius-client-config.yaml" + case "darwin": + cfgPath = "/usr/local/etc/mobius-client-config.yaml" + case "linux": + cfgPath = "/usr/local/etc/mobius-client-config.yaml" + default: + fmt.Printf("unsupported OS") + } + + return cfgPath +} diff --git a/hotline/client.go b/hotline/client.go index f73e37a..35ec7fb 100644 --- a/hotline/client.go +++ b/hotline/client.go @@ -22,7 +22,6 @@ import ( "time" ) -const clientConfigPath = "/usr/local/etc/mobius-client-config.yaml" const ( trackerListPage = "trackerList" ) @@ -66,6 +65,7 @@ func readConfig(cfgPath string) (*ClientPrefs, error) { } type Client struct { + cfgPath string DebugBuf *DebugBuffer Connection net.Conn Login *[]byte @@ -234,7 +234,11 @@ func (ui *UI) renderSettingsForm() *tview.Flex { // TODO: handle err } // TODO: handle err - _ = ioutil.WriteFile(clientConfigPath, out, 0666) + err = ioutil.WriteFile(ui.HLClient.cfgPath, out, 0666) + if err != nil { + println(ui.HLClient.cfgPath) + panic(err) + } ui.Pages.RemovePage("settings") }) settingsForm.SetBorder(true) @@ -551,17 +555,19 @@ func (ui *UI) Start() { } } -func NewClient(username string, logger *zap.SugaredLogger) *Client { +func NewClient(cfgPath string, logger *zap.SugaredLogger) *Client { c := &Client{ + cfgPath: cfgPath, Logger: logger, activeTasks: make(map[uint32]*Transaction), Handlers: clientHandlers, } c.UI = NewUI(c) - prefs, err := readConfig(clientConfigPath) + prefs, err := readConfig(cfgPath) if err != nil { - return c + fmt.Printf("unable to read config file") + logger.Fatal("unable to read config file", "path", cfgPath) } c.pref = prefs