Skip to content

Commit

Permalink
Reorder encrypt and decrypt funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
70sh1 committed Mar 3, 2024
1 parent d930a84 commit 244236e
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions eddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ func doneMessage(startTime time.Time, noEmojiAndColor bool) {
fmt.Printf(format.CondPrefix("✨ ", "Done in %v\n", noEmojiAndColor), deltaTime)
}

func decrypt(cCtx *cli.Context) error {
func encrypt(cCtx *cli.Context) error {
var noPasswordProvided bool
var numProcessed uint64
var err error

outputDir := cCtx.String("output")
passGenLen := cCtx.Int("passgenlen")
overwrite := cCtx.Bool("overwrite")
noEmojiAndColor := cCtx.Bool("no-emoji")
password := cCtx.String("unsafe-password")
Expand All @@ -127,28 +130,40 @@ func decrypt(cCtx *cli.Context) error {
if paths, outputDir, err = pathutils.CleanAndCheckPaths(paths, outputDir); err != nil {
return err
}
if password == "" {
if password, err = scanPassword(core.Decryption, noEmojiAndColor); err != nil {
if password == "" && passGenLen == 0 {
if password, err = scanPassword(core.Encryption, noEmojiAndColor); err != nil {
return err
}
passGenLen = 6
}

startTime := time.Now()
if err := core.DecryptFiles(paths, outputDir, password, overwrite, noEmojiAndColor); err != nil {
if password == "" {
if password, err = core.GeneratePassphrase(passGenLen); err != nil {
return fmt.Errorf("failed to generate passphrase; %v", err)
}
noPasswordProvided = true
}

numProcessed, err = core.EncryptFiles(paths, outputDir, password, overwrite, noEmojiAndColor)
if err != nil {
return err
}
if noPasswordProvided && (numProcessed > 0) {
fmt.Println()
fmt.Printf(
format.CondPrefix("🔑 ", "NOTE: This passphrase was generated and used: '%v'\n", noEmojiAndColor), password,
)
}
doneMessage(startTime, noEmojiAndColor)

return nil
}

func encrypt(cCtx *cli.Context) error {
var noPasswordProvided bool
var numProcessed uint64
func decrypt(cCtx *cli.Context) error {
var err error

outputDir := cCtx.String("output")
passGenLen := cCtx.Int("passgenlen")
overwrite := cCtx.Bool("overwrite")
noEmojiAndColor := cCtx.Bool("no-emoji")
password := cCtx.String("unsafe-password")
Expand All @@ -157,31 +172,16 @@ func encrypt(cCtx *cli.Context) error {
if paths, outputDir, err = pathutils.CleanAndCheckPaths(paths, outputDir); err != nil {
return err
}
if password == "" && passGenLen == 0 {
if password, err = scanPassword(core.Encryption, noEmojiAndColor); err != nil {
if password == "" {
if password, err = scanPassword(core.Decryption, noEmojiAndColor); err != nil {
return err
}
passGenLen = 6
}

startTime := time.Now()
if password == "" {
if password, err = core.GeneratePassphrase(passGenLen); err != nil {
return fmt.Errorf("failed to generate passphrase; %v", err)
}
noPasswordProvided = true
}

numProcessed, err = core.EncryptFiles(paths, outputDir, password, overwrite, noEmojiAndColor)
if err != nil {
if err := core.DecryptFiles(paths, outputDir, password, overwrite, noEmojiAndColor); err != nil {
return err
}
if noPasswordProvided && (numProcessed > 0) {
fmt.Println()
fmt.Printf(
format.CondPrefix("🔑 ", "NOTE: This passphrase was generated and used: '%v'\n", noEmojiAndColor), password,
)
}
doneMessage(startTime, noEmojiAndColor)

return nil
Expand Down

0 comments on commit 244236e

Please sign in to comment.