diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-21 20:36:04 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-21 20:36:04 +0530 |
| commit | a92d0201b50c5c8e87638bd2ebb2e9e2b5b0983c (patch) | |
| tree | baa134e8baa73a5f6094ea56bb83d7e8603dfdef /cmd/eno/cobra_encrypt.go | |
| parent | e48a02646f60b6fdff8db5c4105896756584ac63 (diff) | |
update gitignore so CI tests don't fail, update filenames
Diffstat (limited to 'cmd/eno/cobra_encrypt.go')
| -rw-r--r-- | cmd/eno/cobra_encrypt.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/cmd/eno/cobra_encrypt.go b/cmd/eno/cobra_encrypt.go new file mode 100644 index 0000000..2cf6c01 --- /dev/null +++ b/cmd/eno/cobra_encrypt.go @@ -0,0 +1,101 @@ +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" + "gitlab.com/whiteboardguy/eno/internal/config" + "gitlab.com/whiteboardguy/eno/internal/crud" + "gitlab.com/whiteboardguy/eno/internal/crypt" + "gitlab.com/whiteboardguy/eno/internal/utils" +) + +var encryptCmd = &cobra.Command{ + Use: "encrypt <file>", + Short: "encrypt 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 = 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) + } + 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) + } + + if sshFlag { + if config.Conf.DefaultSSHPubPath == "" { + return fmt.Errorf("SSH not configured. Run 'eno ssh init' first") + } + defaultPubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath) + if err != nil { + return fmt.Errorf("failed to read default SSH public key: %v", err) + } + pubKeys, _, err := crud.PromptRecipientKeys(defaultPubKeyBytes) + if err != nil { + return err + } + if len(pubKeys) == 0 { + return fmt.Errorf("at least one recipient public key is required") + } + 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) + } + + return nil + } + + fmt.Printf("Enter a passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + 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) + } + + return nil + }, +} |