0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

save send remember

This commit is contained in:
Zack Scholl 2019-07-17 16:19:32 -06:00
parent f18c2eae7e
commit 1d7976a61a

View file

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -65,7 +66,7 @@ func Run() (err error) {
}, },
} }
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.BoolFlag{Name: "config", Usage: "save these settings to reuse next time"}, cli.BoolFlag{Name: "remember", Usage: "save these settings to reuse next time"},
cli.BoolFlag{Name: "debug", Usage: "increase verbosity (a lot)"}, cli.BoolFlag{Name: "debug", Usage: "increase verbosity (a lot)"},
cli.BoolFlag{Name: "yes", Usage: "automatically agree to all prompts"}, cli.BoolFlag{Name: "yes", Usage: "automatically agree to all prompts"},
cli.BoolFlag{Name: "stdout", Usage: "redirect file to stdout"}, cli.BoolFlag{Name: "stdout", Usage: "redirect file to stdout"},
@ -103,19 +104,26 @@ func getConfigDir() (homedir string, err error) {
log.Error(err) log.Error(err)
return return
} }
log.SetLevel("debug")
homedir = path.Join(homedir, ".config", "croc") homedir = path.Join(homedir, ".config", "croc")
if _, err := os.Stat(homedir); os.IsNotExist(err) { if _, err := os.Stat(homedir); os.IsNotExist(err) {
log.Debugf("creating home directory %s", homedir) log.Debugf("creating home directory %s", homedir)
err = os.MkdirAll(homedir, 0700) err = os.MkdirAll(homedir, 0700)
} }
return return
} }
func send(c *cli.Context) (err error) { func send(c *cli.Context) (err error) {
makeConfigDir() if c.GlobalBool("debug") {
os.Exit(0) log.SetLevel("debug")
log.Debug("debug mode on")
}
configFile, err := getConfigDir()
if err != nil {
log.Error(err)
return
}
configFile = path.Join(configFile, "send.json")
var fnames []string var fnames []string
stat, _ := os.Stdin.Stat() stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 { if (stat.Mode() & os.ModeCharDevice) == 0 {
@ -181,7 +189,7 @@ func send(c *cli.Context) (err error) {
paths = append(paths, filepath.ToSlash(fname)) paths = append(paths, filepath.ToSlash(fname))
} }
} }
cr, err := croc.New(croc.Options{ crocOptions := croc.Options{
SharedSecret: sharedSecret, SharedSecret: sharedSecret,
IsSender: true, IsSender: true,
Debug: c.GlobalBool("debug"), Debug: c.GlobalBool("debug"),
@ -190,11 +198,29 @@ func send(c *cli.Context) (err error) {
Stdout: c.GlobalBool("stdout"), Stdout: c.GlobalBool("stdout"),
DisableLocal: c.Bool("no-local"), DisableLocal: c.Bool("no-local"),
RelayPorts: strings.Split(c.String("ports"), ","), RelayPorts: strings.Split(c.String("ports"), ","),
}) }
cr, err := croc.New(crocOptions)
if err != nil { if err != nil {
return return
} }
// save the config
if c.GlobalBool("remember") {
log.Debug("saving config file")
var bConfig []byte
bConfig, err = json.MarshalIndent(crocOptions, "", " ")
if err != nil {
log.Error(err)
return
}
err = ioutil.WriteFile(configFile, bConfig, 0644)
if err != nil {
log.Error(err)
return
}
log.Debugf("wrote %s", configFile)
}
err = cr.Send(croc.TransferOptions{ err = cr.Send(croc.TransferOptions{
PathToFiles: paths, PathToFiles: paths,
KeepPathInRemote: haveFolder, KeepPathInRemote: haveFolder,