Skip to content

Commit

Permalink
Aovoid to create config dir if not required
Browse files Browse the repository at this point in the history
If `--remember` is not set, the config dir should not created.
  • Loading branch information
a1lu committed Apr 1, 2024
1 parent cd89e80 commit 4baec42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ func setDebugLevel(c *cli.Context) {
}
}

func getSendConfigFile() string {
configFile, err := utils.GetConfigDir()
func getSendConfigFile(requireValidPath bool) string {
configFile, err := utils.GetConfigDir(requireValidPath)
if err != nil {
log.Error(err)
return ""
}
return path.Join(configFile, "send.json")
}

func getReceiveConfigFile() (string, error) {
configFile, err := utils.GetConfigDir()
func getReceiveConfigFile(requireValidPath bool) (string, error) {
configFile, err := utils.GetConfigDir(requireValidPath)
if err != nil {
log.Error(err)
return "", err
Expand Down Expand Up @@ -228,7 +228,7 @@ func send(c *cli.Context) (err error) {
} else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 {
crocOptions.RelayAddress = ""
}
b, errOpen := os.ReadFile(getSendConfigFile())
b, errOpen := os.ReadFile(getSendConfigFile(false))
if errOpen == nil && !c.Bool("remember") {
var rememberedOptions croc.Options
err = json.Unmarshal(b, &rememberedOptions)
Expand Down Expand Up @@ -364,7 +364,7 @@ func makeTempFileWithString(s string) (fnames []string, err error) {

func saveConfig(c *cli.Context, crocOptions croc.Options) {
if c.Bool("remember") {
configFile := getSendConfigFile()
configFile := getSendConfigFile(true)
log.Debug("saving config file")
var bConfig []byte
// if the code wasn't set, don't save it
Expand Down
8 changes: 4 additions & 4 deletions src/models/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var publicDNS = []string{
"[2620:119:53::53]", // Cisco OpenDNS
}

func getConfigFile() (fname string, err error) {
configFile, err := utils.GetConfigDir()
func getConfigFile(requireValidPath bool) (fname string, err error) {
configFile, err := utils.GetConfigDir(requireValidPath)
if err != nil {
return
}
Expand All @@ -66,14 +66,14 @@ func init() {
}
if doRemember {
// save in config file
fname, err := getConfigFile()
fname, err := getConfigFile(true)
if err == nil {
f, _ := os.Create(fname)
f.Close()
}
}
if !INTERNAL_DNS {
fname, err := getConfigFile()
fname, err := getConfigFile(false)
if err == nil {
INTERNAL_DNS = utils.Exists(fname)
}
Expand Down
12 changes: 9 additions & 3 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ const NbPinNumbers = 4
const NbBytesWords = 4

// Get or create home directory
func GetConfigDir() (homedir string, err error) {
func GetConfigDir(requireValidPath bool) (homedir string, err error) {
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 {
if !requireValidPath {
err = nil
homedir = ""
}
return
}
homedir = path.Join(homedir, ".config", "croc")
}

if _, err = os.Stat(homedir); os.IsNotExist(err) {
err = os.MkdirAll(homedir, 0o700)
if requireValidPath {
if _, err = os.Stat(homedir); os.IsNotExist(err) {
err = os.MkdirAll(homedir, 0o700)
}
}
return
}
Expand Down

0 comments on commit 4baec42

Please sign in to comment.