aboutsummaryrefslogtreecommitdiff
path: root/cmd/eno/cobra.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/eno/cobra.go')
-rw-r--r--cmd/eno/cobra.go61
1 files changed, 37 insertions, 24 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index 179c610..69f621f 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -1,6 +1,7 @@
package main
import (
+ "bufio"
"fmt"
"os"
@@ -18,31 +19,36 @@ var encryptCmd = &cobra.Command{
Use: "encrypt <file>",
Short: "encrypt a file",
Args: cobra.ExactArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
- output, _ := cmd.Flags().GetString("output")
+ RunE: func(cmd *cobra.Command, args []string) error {
+ output, err := cmd.Flags().GetString("output")
+ if err != nil {
+ return fmt.Errorf("failed to get output flag: %v", err)
+ }
- if _, err := os.ReadFile(output); err == nil {
- var overwriteConfirm string
+ if _, err := os.Stat(output); err == nil {
+ scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output)
- fmt.Scanln(&overwriteConfirm)
+ scanner.Scan()
+ overwriteConfirm := scanner.Text()
+ if err := scanner.Err(); err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- cmd.PrintErrln("Error:", "write aborted")
+ cmd.PrintErrln("Error: write aborted")
os.Exit(1)
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read input file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read input file: %v", err)
}
fmt.Printf("Enter a super secure passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read passphrase: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read passphrase: %v", err)
}
encrypted, err := crypt.EncryptRawBytes(fileBytes, password)
@@ -52,9 +58,10 @@ var encryptCmd = &cobra.Command{
}
if err := os.WriteFile(output, encrypted, 0644); err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to write output file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to write output file: %v", err)
}
+
+ return nil
},
}
@@ -62,31 +69,36 @@ var decryptCmd = &cobra.Command{
Use: "decrypt <file>",
Short: "decrypt a file",
Args: cobra.ExactArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
- output, _ := cmd.Flags().GetString("output")
+ RunE: func(cmd *cobra.Command, args []string) error {
+ output, err := cmd.Flags().GetString("output")
+ if err != nil {
+ return fmt.Errorf("failed to get output flag: %v", err)
+ }
- if _, err := os.ReadFile(output); err == nil {
- var overwriteConfirm string
+ if _, err := os.Stat(output); err == nil {
+ scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output)
- fmt.Scanln(&overwriteConfirm)
+ scanner.Scan()
+ overwriteConfirm := scanner.Text()
+ if err := scanner.Err(); err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- cmd.PrintErrln("Error:", "write aborted")
+ cmd.PrintErrln("Error: write aborted")
os.Exit(1)
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read input file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read input file: %v", err)
}
fmt.Printf("Enter the super secure passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read passphrase: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read passphrase: %v", err)
}
plaintext, err := crypt.DecryptRawBytes(fileBytes, password)
@@ -96,9 +108,10 @@ var decryptCmd = &cobra.Command{
}
if err := os.WriteFile(output, plaintext, 0644); err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to write output file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to write output file: %v", err)
}
+
+ return nil
},
}