mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
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.
This commit is contained in:
parent
956598c427
commit
e599e56415
1 changed files with 15 additions and 7 deletions
|
@ -153,7 +153,7 @@ func setDebugLevel(c *cli.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfigFile() string {
|
func getSendConfigFile() string {
|
||||||
configFile, err := utils.GetConfigDir()
|
configFile, err := utils.GetConfigDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
@ -162,6 +162,15 @@ func getConfigFile() string {
|
||||||
return path.Join(configFile, "send.json")
|
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) {
|
func determinePass(c *cli.Context) (pass string) {
|
||||||
pass = c.String("pass")
|
pass = c.String("pass")
|
||||||
b, err := os.ReadFile(pass)
|
b, err := os.ReadFile(pass)
|
||||||
|
@ -219,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(getConfigFile())
|
b, errOpen := os.ReadFile(getSendConfigFile())
|
||||||
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)
|
||||||
|
@ -355,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 := getConfigFile()
|
configFile := getSendConfigFile()
|
||||||
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
|
||||||
|
@ -443,12 +452,11 @@ func receive(c *cli.Context) (err error) {
|
||||||
|
|
||||||
// load options here
|
// load options here
|
||||||
setDebugLevel(c)
|
setDebugLevel(c)
|
||||||
configFile, err := utils.GetConfigDir()
|
|
||||||
if err != nil {
|
configFile, err := getReceiveConfigFile()
|
||||||
log.Error(err)
|
if err != nil && c.Bool("remember") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
configFile = path.Join(configFile, "receive.json")
|
|
||||||
b, errOpen := os.ReadFile(configFile)
|
b, errOpen := os.ReadFile(configFile)
|
||||||
if errOpen == nil && !c.Bool("remember") {
|
if errOpen == nil && !c.Bool("remember") {
|
||||||
var rememberedOptions croc.Options
|
var rememberedOptions croc.Options
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue