diff options
| author | ishaan <contact@ishaan.cc> | 2026-08-03 11:26:55 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-08-03 11:26:55 +0530 |
| commit | c7b546fb94687a6cbe9bab9db041d15104c5111d (patch) | |
| tree | 4b2792f91a8afc091d19e74b427ab028d881ebde /cmd | |
| parent | 2e2cc2cf85cc57d5fb46be64169072ade479319d (diff) | |
return errors from encrypt/decrypt instead of os.Exit
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/eno/cobra_decrypt.go | 39 | ||||
| -rw-r--r-- | cmd/eno/cobra_encrypt.go | 48 |
2 files changed, 33 insertions, 54 deletions
diff --git a/cmd/eno/cobra_decrypt.go b/cmd/eno/cobra_decrypt.go index df81f22..5f33ab5 100644 --- a/cmd/eno/cobra_decrypt.go +++ b/cmd/eno/cobra_decrypt.go @@ -15,11 +15,10 @@ var decryptCmd = &cobra.Command{ Use: "decrypt <file>", Short: "decrypt a file", Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { output, err := cmd.Flags().GetString("output") if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if output == "" { @@ -29,68 +28,60 @@ var decryptCmd = &cobra.Command{ if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if !ok { - cmd.PrintErrln("Error: write aborted") - os.Exit(1) + return fmt.Errorf("write aborted") } } fileBytes, err := os.ReadFile(args[0]) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } authMode, err := utils.CheckFileAuthMode(args[0]) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if authMode == crypt.SSH { if config.Conf.DefaultSSHPrivPath == "" { - cmd.PrintErrln("Error: SSH not configured. Run 'eno ssh init' first") - os.Exit(1) + return fmt.Errorf("SSH not configured. Run 'eno ssh init' first") } plaintext, err := crypt.DecryptBytesSSH(fileBytes, config.Conf.DefaultSSHPrivPath) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } crypt.TryMlock(plaintext) defer clear(plaintext) if err := os.WriteFile(output, plaintext, 0600); err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } - return + return nil } fmt.Printf("Enter the passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } crypt.TryMlock(password) defer clear(password) plaintext, err := crypt.DecryptBytesPass(fileBytes, password) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } crypt.TryMlock(plaintext) defer clear(plaintext) if err := os.WriteFile(output, plaintext, 0600); err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } + + return nil }, } diff --git a/cmd/eno/cobra_encrypt.go b/cmd/eno/cobra_encrypt.go index 6293a02..1082bae 100644 --- a/cmd/eno/cobra_encrypt.go +++ b/cmd/eno/cobra_encrypt.go @@ -15,11 +15,10 @@ var encryptCmd = &cobra.Command{ Use: "encrypt <file>", Short: "encrypt a file", Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { output, err := cmd.Flags().GetString("output") if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if output == "" { @@ -29,80 +28,69 @@ var encryptCmd = &cobra.Command{ if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if !ok { - cmd.PrintErrln("Error: write aborted") - os.Exit(1) + return fmt.Errorf("write aborted") } } fileBytes, err := os.ReadFile(args[0]) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } crypt.TryMlock(fileBytes) defer clear(fileBytes) sshFlag, err := cmd.Flags().GetBool("ssh") if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if sshFlag { if config.Conf.DefaultSSHPubPath == "" { - cmd.PrintErrln("Error: SSH not configured. Run 'eno ssh init' first") - os.Exit(1) + return fmt.Errorf("SSH not configured. Run 'eno ssh init' first") } defaultPubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } pubKeys, _, err := crud.PromptRecipientKeys(defaultPubKeyBytes) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if len(pubKeys) == 0 { - cmd.PrintErrln("Error: at least one recipient public key is required") - os.Exit(1) + return fmt.Errorf("at least one recipient public key is required") } encrypted, _, err := crypt.EncryptBytesSSH(fileBytes, pubKeys) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if err := os.WriteFile(output, encrypted, 0600); err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } - return + return nil } fmt.Printf("Enter a passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } crypt.TryMlock(password) defer clear(password) encrypted, err := crypt.EncryptBytesPass(fileBytes, password) if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } if err := os.WriteFile(output, encrypted, 0600); err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) + return err } + + return nil }, } |