Skip to content

Commit

Permalink
Merge pull request #707 from schollz:schollz/issue706
Browse files Browse the repository at this point in the history
ux: improve the environmental variable messaging for sending/receiving
  • Loading branch information
schollz authored May 23, 2024
2 parents 6181903 + 66f0d12 commit 3acac5d
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ func send(c *cli.Context) (err error) {
return errors.New("must specify file: croc send [filename(s) or folder]")
}

// if operating system is UNIX, then use environmental variable to set the code
if (runtime.GOOS == "linux" && c.IsSet("code")) || os.Getenv("CROC_SECRET") != "" {
crocOptions.SharedSecret = os.Getenv("CROC_SECRET")
if crocOptions.SharedSecret == "" {
fmt.Printf(`On linux, to send with a custom code phrase,
you need to set the environmental variable CROC_SECRET:
export CROC_SECRET="****"
croc send file.txt
Or you can have the code phrase automaticlaly generated:
croc send file.txt
`)
os.Exit(0)
}
}

if len(crocOptions.SharedSecret) == 0 {
// generate code phrase
crocOptions.SharedSecret = utils.GetRandomName()
Expand All @@ -313,19 +332,6 @@ func send(c *cli.Context) (err error) {
return
}

// if operating system is UNIX, then use environmental variable to set the code
if runtime.GOOS == "linux" {
log.Debug("forcing code phrase from environmental variable")
crocOptions.SharedSecret = os.Getenv("CROC_SECRET")
if crocOptions.SharedSecret == "" {
fmt.Printf(`To use croc you need to set a code phrase using your environmental variables:
export CROC_SECRET="yourcodephrasetouse"
`)
os.Exit(0)
}
}

cr, err := croc.New(crocOptions)
if err != nil {
return
Expand Down Expand Up @@ -508,6 +514,26 @@ func receive(c *cli.Context) (err error) {
}
}

if crocOptions.SharedSecret == "" && os.Getenv("CROC_SECRET") != "" {
crocOptions.SharedSecret = os.Getenv("CROC_SECRET")
} else if runtime.GOOS == "linux" && crocOptions.SharedSecret != "" {
crocOptions.SharedSecret = os.Getenv("CROC_SECRET")
if crocOptions.SharedSecret == "" {
fmt.Printf(`On linux, to receive with croc you either need
to set a code phrase using your environmental variables:
export CROC_SECRET="****"
croc
Or you can specify the code phrase when you run croc without
declaring the secret on the command line:
croc
Enter receive code: ****
`)
os.Exit(0)
}
}
if crocOptions.SharedSecret == "" {
l, err := readline.NewEx(&readline.Config{
Prompt: "Enter receive code: ",
Expand All @@ -526,17 +552,6 @@ func receive(c *cli.Context) (err error) {
return err
}
}
// if operating system is UNIX, then use environmental variable to set the code
if runtime.GOOS == "linux" {
crocOptions.SharedSecret = os.Getenv("CROC_SECRET")
if crocOptions.SharedSecret == "" {
fmt.Printf(`To use croc you need to set a code phrase using your environmental variables:
export CROC_SECRET="yourcodephrasetouse"
`)
os.Exit(0)
}
}

cr, err := croc.New(crocOptions)
if err != nil {
Expand Down

0 comments on commit 3acac5d

Please sign in to comment.