From a92d0201b50c5c8e87638bd2ebb2e9e2b5b0983c Mon Sep 17 00:00:00 2001 From: ishaan Date: Tue, 21 Jul 2026 20:36:04 +0530 Subject: update gitignore so CI tests don't fail, update filenames --- cmd/eno/cobra_decrypt.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 cmd/eno/cobra_decrypt.go (limited to 'cmd/eno/cobra_decrypt.go') 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 ", + 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 + }, +} -- cgit v1.3