From 956598c427621dbd52fe81b33d262cf0ff3a9271 Mon Sep 17 00:00:00 2001 From: a1lu Date: Sat, 30 Mar 2024 23:21:42 +0100 Subject: [PATCH 1/2] Check $HOME as last --- src/utils/utils.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/utils/utils.go b/src/utils/utils.go index 602b71d..7df077c 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -32,16 +32,15 @@ const NbBytesWords = 4 // Get or create home directory func GetConfigDir() (homedir string, err error) { - homedir, err = os.UserHomeDir() - if err != nil { - return - } - if envHomedir, isSet := os.LookupEnv("CROC_CONFIG_DIR"); isSet { homedir = envHomedir } else if xdgConfigHome, isSet := os.LookupEnv("XDG_CONFIG_HOME"); isSet { homedir = path.Join(xdgConfigHome, "croc") } else { + homedir, err = os.UserHomeDir() + if err != nil { + return + } homedir = path.Join(homedir, ".config", "croc") } From e599e5641501b4d5377bc6954a52c96fc4e7bd93 Mon Sep 17 00:00:00 2001 From: a1lu Date: Sat, 30 Mar 2024 23:53:17 +0100 Subject: [PATCH 2/2] Gracefully handle non existend receive config file If croc shall not remember the config, it should not exit if the config file could not be opened. This is similar to the handling of the send config. --- src/cli/cli.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index 23dea9b..2f639b4 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -153,7 +153,7 @@ func setDebugLevel(c *cli.Context) { } } -func getConfigFile() string { +func getSendConfigFile() string { configFile, err := utils.GetConfigDir() if err != nil { log.Error(err) @@ -162,6 +162,15 @@ func getConfigFile() string { return path.Join(configFile, "send.json") } +func getReceiveConfigFile() (string, error) { + configFile, err := utils.GetConfigDir() + if err != nil { + log.Error(err) + return "", err + } + return path.Join(configFile, "receive.json"), nil +} + func determinePass(c *cli.Context) (pass string) { pass = c.String("pass") b, err := os.ReadFile(pass) @@ -219,7 +228,7 @@ func send(c *cli.Context) (err error) { } else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 { crocOptions.RelayAddress = "" } - b, errOpen := os.ReadFile(getConfigFile()) + b, errOpen := os.ReadFile(getSendConfigFile()) if errOpen == nil && !c.Bool("remember") { var rememberedOptions croc.Options err = json.Unmarshal(b, &rememberedOptions) @@ -355,7 +364,7 @@ func makeTempFileWithString(s string) (fnames []string, err error) { func saveConfig(c *cli.Context, crocOptions croc.Options) { if c.Bool("remember") { - configFile := getConfigFile() + configFile := getSendConfigFile() log.Debug("saving config file") var bConfig []byte // if the code wasn't set, don't save it @@ -443,12 +452,11 @@ func receive(c *cli.Context) (err error) { // load options here setDebugLevel(c) - configFile, err := utils.GetConfigDir() - if err != nil { - log.Error(err) + + configFile, err := getReceiveConfigFile() + if err != nil && c.Bool("remember") { return } - configFile = path.Join(configFile, "receive.json") b, errOpen := os.ReadFile(configFile) if errOpen == nil && !c.Bool("remember") { var rememberedOptions croc.Options