diff options
Diffstat (limited to 'cmd/eno/cobra.go')
| -rw-r--r-- | cmd/eno/cobra.go | 84 |
1 files changed, 73 insertions, 11 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go index 1ee441d..179c610 100644 --- a/cmd/eno/cobra.go +++ b/cmd/eno/cobra.go @@ -2,9 +2,11 @@ package main import ( "fmt" + "os" "github.com/spf13/cobra" - "gitlab.com/whiteboardguy/eno/internal/crud" + "gitlab.com/whiteboardguy/eno/internal/crypt" + "gitlab.com/whiteboardguy/eno/internal/utils" ) var cobraCmd = &cobra.Command{ @@ -16,13 +18,43 @@ var encryptCmd = &cobra.Command{ Use: "encrypt <file>", Short: "encrypt a file", Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { output, _ := cmd.Flags().GetString("output") - if output == "" { - return fmt.Errorf("output file required (-o)") + + if _, err := os.ReadFile(output); err == nil { + var overwriteConfirm string + fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output) + fmt.Scanln(&overwriteConfirm) + + if overwriteConfirm != "y" && overwriteConfirm != "Y" { + 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) + } + + 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) + } + + encrypted, err := crypt.EncryptRawBytes(fileBytes, password) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + if err := os.WriteFile(output, encrypted, 0644); err != nil { + cmd.PrintErrln("Error:", fmt.Sprintf("failed to write output file: %v", err)) + os.Exit(1) } - crud.RawEncryptFile(args[0], output) - return nil }, } @@ -30,13 +62,43 @@ var decryptCmd = &cobra.Command{ Use: "decrypt <file>", Short: "decrypt a file", Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { output, _ := cmd.Flags().GetString("output") - if output == "" { - return fmt.Errorf("output file required (-o)") + + if _, err := os.ReadFile(output); err == nil { + var overwriteConfirm string + fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output) + fmt.Scanln(&overwriteConfirm) + + if overwriteConfirm != "y" && overwriteConfirm != "Y" { + 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) + } + + 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) + } + + plaintext, err := crypt.DecryptRawBytes(fileBytes, password) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + if err := os.WriteFile(output, plaintext, 0644); err != nil { + cmd.PrintErrln("Error:", fmt.Sprintf("failed to write output file: %v", err)) + os.Exit(1) } - crud.RawDecryptFile(args[0], output) - return nil }, } |