Merge pull request #686 from a1lu/readonly

Avoid to create config dir if not required
This commit is contained in:
Zack 2024-04-01 12:28:36 -07:00 committed by GitHub
commit eaffc6dcac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 17 deletions

View File

@ -153,8 +153,8 @@ func setDebugLevel(c *cli.Context) {
} }
} }
func getSendConfigFile() string { func getSendConfigFile(requireValidPath bool) string {
configFile, err := utils.GetConfigDir() configFile, err := utils.GetConfigDir(requireValidPath)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return "" return ""
@ -162,8 +162,8 @@ func getSendConfigFile() string {
return path.Join(configFile, "send.json") return path.Join(configFile, "send.json")
} }
func getReceiveConfigFile() (string, error) { func getReceiveConfigFile(requireValidPath bool) (string, error) {
configFile, err := utils.GetConfigDir() configFile, err := utils.GetConfigDir(requireValidPath)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return "", err return "", err
@ -228,7 +228,7 @@ func send(c *cli.Context) (err error) {
} else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 { } else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 {
crocOptions.RelayAddress = "" crocOptions.RelayAddress = ""
} }
b, errOpen := os.ReadFile(getSendConfigFile()) b, errOpen := os.ReadFile(getSendConfigFile(false))
if errOpen == nil && !c.Bool("remember") { if errOpen == nil && !c.Bool("remember") {
var rememberedOptions croc.Options var rememberedOptions croc.Options
err = json.Unmarshal(b, &rememberedOptions) err = json.Unmarshal(b, &rememberedOptions)
@ -364,7 +364,7 @@ func makeTempFileWithString(s string) (fnames []string, err error) {
func saveConfig(c *cli.Context, crocOptions croc.Options) { func saveConfig(c *cli.Context, crocOptions croc.Options) {
if c.Bool("remember") { if c.Bool("remember") {
configFile := getSendConfigFile() configFile := getSendConfigFile(true)
log.Debug("saving config file") log.Debug("saving config file")
var bConfig []byte var bConfig []byte
// if the code wasn't set, don't save it // if the code wasn't set, don't save it
@ -455,12 +455,13 @@ func receive(c *cli.Context) (err error) {
// load options here // load options here
setDebugLevel(c) setDebugLevel(c)
configFile, err := getReceiveConfigFile() doRemember := c.Bool("remember")
if err != nil && c.Bool("remember") { configFile, err := getReceiveConfigFile(doRemember)
if err != nil && doRemember {
return return
} }
b, errOpen := os.ReadFile(configFile) b, errOpen := os.ReadFile(configFile)
if errOpen == nil && !c.Bool("remember") { if errOpen == nil && !doRemember {
var rememberedOptions croc.Options var rememberedOptions croc.Options
err = json.Unmarshal(b, &rememberedOptions) err = json.Unmarshal(b, &rememberedOptions)
if err != nil { if err != nil {
@ -519,7 +520,7 @@ func receive(c *cli.Context) (err error) {
} }
// save the config // save the config
if c.Bool("remember") { if doRemember {
log.Debug("saving config file") log.Debug("saving config file")
var bConfig []byte var bConfig []byte
bConfig, err = json.MarshalIndent(crocOptions, "", " ") bConfig, err = json.MarshalIndent(crocOptions, "", " ")

View File

@ -44,8 +44,8 @@ var publicDNS = []string{
"[2620:119:53::53]", // Cisco OpenDNS "[2620:119:53::53]", // Cisco OpenDNS
} }
func getConfigFile() (fname string, err error) { func getConfigFile(requireValidPath bool) (fname string, err error) {
configFile, err := utils.GetConfigDir() configFile, err := utils.GetConfigDir(requireValidPath)
if err != nil { if err != nil {
return return
} }
@ -66,14 +66,14 @@ func init() {
} }
if doRemember { if doRemember {
// save in config file // save in config file
fname, err := getConfigFile() fname, err := getConfigFile(true)
if err == nil { if err == nil {
f, _ := os.Create(fname) f, _ := os.Create(fname)
f.Close() f.Close()
} }
} }
if !INTERNAL_DNS { if !INTERNAL_DNS {
fname, err := getConfigFile() fname, err := getConfigFile(false)
if err == nil { if err == nil {
INTERNAL_DNS = utils.Exists(fname) INTERNAL_DNS = utils.Exists(fname)
} }

View File

@ -31,7 +31,7 @@ const NbPinNumbers = 4
const NbBytesWords = 4 const NbBytesWords = 4
// Get or create home directory // 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 { if envHomedir, isSet := os.LookupEnv("CROC_CONFIG_DIR"); isSet {
homedir = envHomedir homedir = envHomedir
} else if xdgConfigHome, isSet := os.LookupEnv("XDG_CONFIG_HOME"); isSet { } else if xdgConfigHome, isSet := os.LookupEnv("XDG_CONFIG_HOME"); isSet {
@ -39,13 +39,19 @@ func GetConfigDir() (homedir string, err error) {
} else { } else {
homedir, err = os.UserHomeDir() homedir, err = os.UserHomeDir()
if err != nil { if err != nil {
if !requireValidPath {
err = nil
homedir = ""
}
return return
} }
homedir = path.Join(homedir, ".config", "croc") homedir = path.Join(homedir, ".config", "croc")
} }
if _, err = os.Stat(homedir); os.IsNotExist(err) { if requireValidPath {
err = os.MkdirAll(homedir, 0o700) if _, err = os.Stat(homedir); os.IsNotExist(err) {
err = os.MkdirAll(homedir, 0o700)
}
} }
return return
} }