aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-08-03 11:15:02 +0530
committerishaan <contact@ishaan.cc>2026-08-03 11:15:02 +0530
commit5375294e0bed1fa01cb5f4ff73f2c093b74cd8e2 (patch)
tree9049f67a82a40cce28c052a1f9e0de50bbc0e508
parentb65769aadfc79d22672d4bd512443f0dda8cf6ad (diff)
fix new --edit fallthrough, cleanup error handling
-rw-r--r--cmd/eno/cobra.go2
-rw-r--r--cmd/eno/cobra_decrypt.go34
-rw-r--r--cmd/eno/cobra_encrypt.go45
-rw-r--r--cmd/eno/cobra_new.go7
-rw-r--r--internal/crud/cli_edit.go29
-rw-r--r--internal/crypt/headers.go10
-rw-r--r--internal/crypt/salt.go4
-rw-r--r--internal/editor/external.go12
-rw-r--r--internal/utils/cli.go2
-rw-r--r--internal/utils/utils_test.go2
10 files changed, 86 insertions, 61 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index a07ccf1..b77d862 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -42,5 +42,5 @@ func init() {
sshCmd.AddCommand(sshInitCmd)
configCmd.AddCommand(configSetCmd, configGetCmd, configListCmd, configRemoveCmd)
- cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd, sshCmd, configCmd)
+ cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd, rekeyCmd, sshCmd, configCmd)
}
diff --git a/cmd/eno/cobra_decrypt.go b/cmd/eno/cobra_decrypt.go
index 262816e..df81f22 100644
--- a/cmd/eno/cobra_decrypt.go
+++ b/cmd/eno/cobra_decrypt.go
@@ -15,10 +15,11 @@ 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, err := cmd.Flags().GetString("output")
if err != nil {
- return fmt.Errorf("failed to get output flag: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if output == "" {
@@ -28,26 +29,31 @@ var decryptCmd = &cobra.Command{
if _, err := os.Stat(output); err == nil {
ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
if err != nil {
- return err
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if !ok {
- return fmt.Errorf("write aborted")
+ cmd.PrintErrln("Error: write aborted")
+ os.Exit(1)
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
- return fmt.Errorf("failed to read input file: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
authMode, err := utils.CheckFileAuthMode(args[0])
if err != nil {
- return fmt.Errorf("failed to check auth mode: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if authMode == crypt.SSH {
if config.Conf.DefaultSSHPrivPath == "" {
- return fmt.Errorf("SSH not configured. Run 'eno ssh init' first")
+ cmd.PrintErrln("Error: SSH not configured. Run 'eno ssh init' first")
+ os.Exit(1)
}
plaintext, err := crypt.DecryptBytesSSH(fileBytes, config.Conf.DefaultSSHPrivPath)
if err != nil {
@@ -58,23 +64,24 @@ var decryptCmd = &cobra.Command{
defer clear(plaintext)
if err := os.WriteFile(output, plaintext, 0600); err != nil {
- return fmt.Errorf("failed to write output file: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
- return nil
+ return
}
fmt.Printf("Enter the passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
- return fmt.Errorf("failed to read passphrase: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
crypt.TryMlock(password)
defer clear(password)
plaintext, err := crypt.DecryptBytesPass(fileBytes, password)
if err != nil {
- clear(password)
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
@@ -82,9 +89,8 @@ var decryptCmd = &cobra.Command{
defer clear(plaintext)
if err := os.WriteFile(output, plaintext, 0600); err != nil {
- return fmt.Errorf("failed to write output file: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
-
- return nil
},
}
diff --git a/cmd/eno/cobra_encrypt.go b/cmd/eno/cobra_encrypt.go
index 2cf6c01..6293a02 100644
--- a/cmd/eno/cobra_encrypt.go
+++ b/cmd/eno/cobra_encrypt.go
@@ -15,10 +15,11 @@ 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, err := cmd.Flags().GetString("output")
if err != nil {
- return fmt.Errorf("failed to get output flag: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if output == "" {
@@ -28,74 +29,80 @@ var encryptCmd = &cobra.Command{
if _, err := os.Stat(output); err == nil {
ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
if err != nil {
- return err
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if !ok {
- return fmt.Errorf("write aborted")
+ cmd.PrintErrln("Error: write aborted")
+ os.Exit(1)
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
- return fmt.Errorf("failed to read input file: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
crypt.TryMlock(fileBytes)
defer clear(fileBytes)
sshFlag, err := cmd.Flags().GetBool("ssh")
if err != nil {
- return fmt.Errorf("failed to get ssh flag: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if sshFlag {
if config.Conf.DefaultSSHPubPath == "" {
- return fmt.Errorf("SSH not configured. Run 'eno ssh init' first")
+ cmd.PrintErrln("Error: SSH not configured. Run 'eno ssh init' first")
+ os.Exit(1)
}
defaultPubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath)
if err != nil {
- return fmt.Errorf("failed to read default SSH public key: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
pubKeys, _, err := crud.PromptRecipientKeys(defaultPubKeyBytes)
if err != nil {
- return err
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
if len(pubKeys) == 0 {
- return fmt.Errorf("at least one recipient public key is required")
+ cmd.PrintErrln("Error: at least one recipient public key is required")
+ os.Exit(1)
}
encrypted, _, err := crypt.EncryptBytesSSH(fileBytes, pubKeys)
if err != nil {
- clear(fileBytes)
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
if err := os.WriteFile(output, encrypted, 0600); err != nil {
- return fmt.Errorf("failed to write output file: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
- return nil
+ return
}
fmt.Printf("Enter a passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
- return fmt.Errorf("failed to read passphrase: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
crypt.TryMlock(password)
defer clear(password)
encrypted, err := crypt.EncryptBytesPass(fileBytes, password)
if err != nil {
- clear(fileBytes)
- clear(password)
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
if err := os.WriteFile(output, encrypted, 0600); err != nil {
- return fmt.Errorf("failed to write output file: %v", err)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
}
-
- return nil
},
}
diff --git a/cmd/eno/cobra_new.go b/cmd/eno/cobra_new.go
index 7d46546..682c122 100644
--- a/cmd/eno/cobra_new.go
+++ b/cmd/eno/cobra_new.go
@@ -12,9 +12,9 @@ import (
)
var newCmd = &cobra.Command{
- Use: "new <note>",
+ Use: "new <note>",
Short: "create a new encrypted note",
- Args: cobra.MinimumNArgs(1),
+ Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
output, err := cmd.Flags().GetString("output")
@@ -93,13 +93,14 @@ var newCmd = &cobra.Command{
}
return
}
-
+
if editFlag {
err = crud.NewNotePassEdit(output, config.Conf.EditorString)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
+ return
}
err = crud.NewNotePass(output)
diff --git a/internal/crud/cli_edit.go b/internal/crud/cli_edit.go
index 0683439..a51ce0d 100644
--- a/internal/crud/cli_edit.go
+++ b/internal/crud/cli_edit.go
@@ -24,28 +24,39 @@ func EditNotePass(notePath, editorString string) error {
defer clear(password)
ciphertext, err := os.ReadFile(notePath)
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
tmpFile, err := utils.SecureTempFile()
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
plaintext, err := crypt.DecryptBytesPass(ciphertext, password)
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
crypt.TryMlock(plaintext)
defer clear(plaintext)
defer deferredCleanTmpFile(tmpFile, len(plaintext))
defer os.Remove(tmpFile.Name())
-
_, err = tmpFile.Write(plaintext)
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
err = editor.OpenWithEditor(tmpFile.Name(), editorString)
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
newContent, err := os.ReadFile(tmpFile.Name())
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
crypt.TryMlock(newContent)
defer clear(newContent)
@@ -54,7 +65,9 @@ func EditNotePass(notePath, editorString string) error {
}
encrypted, err := crypt.EncryptBytesPass(newContent, password)
- if err != nil { return err }
+ if err != nil {
+ return err
+ }
err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm())
diff --git a/internal/crypt/headers.go b/internal/crypt/headers.go
index c47d39c..ee38560 100644
--- a/internal/crypt/headers.go
+++ b/internal/crypt/headers.go
@@ -30,11 +30,11 @@ type FileHeaders struct {
}
type WrappedKey struct {
- PKType string // "ssh-ed25519"
- PubKeyBytes []byte
- EphemeralPub []byte // of X25519, for ECDH on decrypt (also can replace salt)
- Nonce [12]byte // for decrypting EncNoteKey
- EncNoteKey []byte // encrypted note key (ciphertext + 16B tag)
+ PKType string // "ssh-ed25519"
+ PubKeyBytes []byte
+ EphemeralPub []byte // of X25519, for ECDH on decrypt (also can replace salt)
+ Nonce [12]byte // for decrypting EncNoteKey
+ EncNoteKey []byte // encrypted note key (ciphertext + 16B tag)
}
func GetFileHeaders(b []byte) (FileHeaders, error) {
diff --git a/internal/crypt/salt.go b/internal/crypt/salt.go
index 46874bd..09b6c72 100644
--- a/internal/crypt/salt.go
+++ b/internal/crypt/salt.go
@@ -9,7 +9,7 @@ import (
func NewSalt() ([16]byte, error) {
salt := [16]byte{}
if _, err := io.ReadFull(rand.Reader, salt[:]); err != nil {
- return [16]byte{}, fmt.Errorf("crypt: failed to read salt: %w", err)
+ return [16]byte{}, fmt.Errorf("failed to read salt: %w", err)
}
return salt, nil
@@ -18,7 +18,7 @@ func NewSalt() ([16]byte, error) {
func NewNonce() ([12]byte, error) {
nonce := [12]byte{}
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
- return [12]byte{}, fmt.Errorf("crypt: failed to read nonce: %w", err)
+ return [12]byte{}, fmt.Errorf("failed to read nonce: %w", err)
}
return nonce, nil
diff --git a/internal/editor/external.go b/internal/editor/external.go
index 84a95ff..0b0c430 100644
--- a/internal/editor/external.go
+++ b/internal/editor/external.go
@@ -14,12 +14,12 @@ func OpenWithEditor(filePath, editorString string) error {
cmdStr = strings.Replace(editorString, "%s", filePath, 1)
}
- parts := strings.Fields(cmdStr)
+ parts := strings.Fields(cmdStr)
- cmd := exec.Command(parts[0], parts[1:]...)
- cmd.Stdin = os.Stdin
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
+ cmd := exec.Command(parts[0], parts[1:]...)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
- return cmd.Run() // blocks until user closes editor
+ return cmd.Run() // blocks until user closes editor
}
diff --git a/internal/utils/cli.go b/internal/utils/cli.go
index ba7a07c..71d5163 100644
--- a/internal/utils/cli.go
+++ b/internal/utils/cli.go
@@ -39,5 +39,5 @@ func AskUserYesNo(prompt string, defaultVal bool) (bool, error) {
}
func ClearTerm() {
- fmt.Print("\033[H\033[2J")
+ fmt.Print("\033[H\033[2J")
}
diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go
index 34631df..ccc8338 100644
--- a/internal/utils/utils_test.go
+++ b/internal/utils/utils_test.go
@@ -248,5 +248,3 @@ func TestGetPubKeyFromService_404(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "returned status 404")
}
-
-