aboutsummaryrefslogtreecommitdiff
path: root/cmd/eno/cobra_decrypt.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-21 20:36:04 +0530
committerishaan <contact@ishaan.cc>2026-07-21 20:36:04 +0530
commita92d0201b50c5c8e87638bd2ebb2e9e2b5b0983c (patch)
treebaa134e8baa73a5f6094ea56bb83d7e8603dfdef /cmd/eno/cobra_decrypt.go
parente48a02646f60b6fdff8db5c4105896756584ac63 (diff)
update gitignore so CI tests don't fail, update filenames
Diffstat (limited to 'cmd/eno/cobra_decrypt.go')
-rw-r--r--cmd/eno/cobra_decrypt.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/cmd/eno/cobra_decrypt.go b/cmd/eno/cobra_decrypt.go
new file mode 100644
index 0000000..262816e
--- /dev/null
+++ b/cmd/eno/cobra_decrypt.go
@@ -0,0 +1,90 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/spf13/cobra"
+ "gitlab.com/whiteboardguy/eno/internal/config"
+ "gitlab.com/whiteboardguy/eno/internal/crypt"
+ "gitlab.com/whiteboardguy/eno/internal/utils"
+)
+
+var decryptCmd = &cobra.Command{
+ Use: "decrypt <file>",
+ Short: "decrypt a file",
+ Args: cobra.ExactArgs(1),
+ 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 output == "" {
+ output = strings.TrimSuffix(args[0], ".eno")
+ }
+
+ if _, err := os.Stat(output); err == nil {
+ ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return fmt.Errorf("write aborted")
+ }
+ }
+
+ fileBytes, err := os.ReadFile(args[0])
+ if err != nil {
+ return fmt.Errorf("failed to read input file: %v", err)
+ }
+
+ authMode, err := utils.CheckFileAuthMode(args[0])
+ if err != nil {
+ return fmt.Errorf("failed to check auth mode: %v", err)
+ }
+
+ if authMode == crypt.SSH {
+ if config.Conf.DefaultSSHPrivPath == "" {
+ 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)
+ }
+ crypt.TryMlock(plaintext)
+ defer clear(plaintext)
+
+ if err := os.WriteFile(output, plaintext, 0600); err != nil {
+ return fmt.Errorf("failed to write output file: %v", err)
+ }
+
+ return nil
+ }
+
+ fmt.Printf("Enter the passphrase: ")
+ password, err := utils.TakeSecureUserInput()
+ if err != nil {
+ return fmt.Errorf("failed to read passphrase: %v", err)
+ }
+ crypt.TryMlock(password)
+ defer clear(password)
+
+ plaintext, err := crypt.DecryptBytesPass(fileBytes, password)
+ if err != nil {
+ clear(password)
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+ crypt.TryMlock(plaintext)
+ defer clear(plaintext)
+
+ if err := os.WriteFile(output, plaintext, 0600); err != nil {
+ return fmt.Errorf("failed to write output file: %v", err)
+ }
+
+ return nil
+ },
+}