mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
reduce complexity
This commit is contained in:
parent
a0e2876741
commit
9fb54a3952
1 changed files with 24 additions and 18 deletions
|
@ -110,19 +110,25 @@ func getConfigDir() (homedir string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(c *cli.Context) (err error) {
|
func setDebugLevel(c *cli.Context) {
|
||||||
if c.GlobalBool("debug") {
|
if c.GlobalBool("debug") {
|
||||||
log.SetLevel("debug")
|
log.SetLevel("debug")
|
||||||
log.Debug("debug mode on")
|
log.Debug("debug mode on")
|
||||||
} else {
|
} else {
|
||||||
log.SetLevel("info")
|
log.SetLevel("info")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigFile() string {
|
||||||
configFile, err := getConfigDir()
|
configFile, err := getConfigDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
configFile = path.Join(configFile, "send.json")
|
return path.Join(configFile, "send.json")
|
||||||
|
}
|
||||||
|
func send(c *cli.Context) (err error) {
|
||||||
|
setDebugLevel(c)
|
||||||
|
|
||||||
crocOptions := croc.Options{
|
crocOptions := croc.Options{
|
||||||
SharedSecret: c.String("code"),
|
SharedSecret: c.String("code"),
|
||||||
|
@ -134,7 +140,7 @@ func send(c *cli.Context) (err error) {
|
||||||
DisableLocal: c.Bool("no-local"),
|
DisableLocal: c.Bool("no-local"),
|
||||||
RelayPorts: strings.Split(c.String("ports"), ","),
|
RelayPorts: strings.Split(c.String("ports"), ","),
|
||||||
}
|
}
|
||||||
b, errOpen := ioutil.ReadFile(configFile)
|
b, errOpen := ioutil.ReadFile(getConfigFile())
|
||||||
if errOpen == nil && !c.GlobalBool("remember") {
|
if errOpen == nil && !c.GlobalBool("remember") {
|
||||||
var rememberedOptions croc.Options
|
var rememberedOptions croc.Options
|
||||||
err = json.Unmarshal(b, &rememberedOptions)
|
err = json.Unmarshal(b, &rememberedOptions)
|
||||||
|
@ -224,14 +230,26 @@ func send(c *cli.Context) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// save the config
|
// save the config
|
||||||
|
saveConfig(c, crocOptions)
|
||||||
|
|
||||||
|
err = cr.Send(croc.TransferOptions{
|
||||||
|
PathToFiles: paths,
|
||||||
|
KeepPathInRemote: haveFolder,
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveConfig(c *cli.Context, crocOptions croc.Options) {
|
||||||
if c.GlobalBool("remember") {
|
if c.GlobalBool("remember") {
|
||||||
|
configFile := getConfigFile()
|
||||||
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
|
||||||
if c.String("code") == "" {
|
if c.String("code") == "" {
|
||||||
crocOptions.SharedSecret = ""
|
crocOptions.SharedSecret = ""
|
||||||
}
|
}
|
||||||
bConfig, err = json.MarshalIndent(crocOptions, "", " ")
|
bConfig, err := json.MarshalIndent(crocOptions, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
|
@ -243,13 +261,6 @@ func send(c *cli.Context) (err error) {
|
||||||
}
|
}
|
||||||
log.Debugf("wrote %s", configFile)
|
log.Debugf("wrote %s", configFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cr.Send(croc.TransferOptions{
|
|
||||||
PathToFiles: paths,
|
|
||||||
KeepPathInRemote: haveFolder,
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func receive(c *cli.Context) (err error) {
|
func receive(c *cli.Context) (err error) {
|
||||||
|
@ -266,12 +277,7 @@ func receive(c *cli.Context) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// load options here
|
// load options here
|
||||||
if c.GlobalBool("debug") {
|
setDebugLevel(c)
|
||||||
log.SetLevel("debug")
|
|
||||||
log.Debug("debug mode on")
|
|
||||||
} else {
|
|
||||||
log.SetLevel("info")
|
|
||||||
}
|
|
||||||
configFile, err := getConfigDir()
|
configFile, err := getConfigDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue