aboutsummaryrefslogtreecommitdiff
path: root/cmd/eno
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/eno')
-rw-r--r--cmd/eno/cobra.go223
1 files changed, 0 insertions, 223 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index 93cba8b..7283df1 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -1,16 +1,10 @@
package main
import (
- "fmt"
"os"
- "path/filepath"
- "strings"
"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 cobraCmd = &cobra.Command{
@@ -18,173 +12,6 @@ var cobraCmd = &cobra.Command{
Short: "file encryption tool",
}
-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
- },
-}
-
-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
- },
-}
-
var initCmd = &cobra.Command{
Use: "init",
Short: "intialize Eno config",
@@ -200,56 +27,6 @@ var initCmd = &cobra.Command{
},
}
-var editCmd = &cobra.Command{
- Use: "edit <note>",
- Short: "edit an existing encrypted note",
- Args: cobra.ExactArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
-
- target := filepath.Clean(args[0])
-
- ok, err := utils.AskUserYesNo("Edit "+target+" ?", true)
- if err != nil {
- cmd.PrintErrln("Error:", err)
- os.Exit(1)
- }
- if !ok {
- cmd.PrintErrln("Error: write aborted")
- os.Exit(1)
- }
-
- target = strings.TrimSuffix(target, ".eno")
- target += ".eno"
-
- if _, err := os.Stat(target); os.IsNotExist(err) {
- alt := filepath.Join(config.Conf.DataDir, target)
- if _, err2 := os.Stat(alt); err2 == nil {
- target = alt
- }
- }
-
- authMode, err := utils.CheckFileAuthMode(target)
- if err != nil {
- cmd.PrintErrln("Error:", err)
- os.Exit(1)
- }
-
- switch authMode {
- case crypt.SSH:
- err = crud.EditNoteSSH(target, config.Conf.DefaultSSHPrivPath, config.Conf.EditorString)
- case crypt.Pass:
- err = crud.EditNotePass(target, config.Conf.EditorString)
- default:
- cmd.PrintErrln("Error: unknown auth mode:", authMode)
- os.Exit(1)
- }
- if err != nil {
- cmd.PrintErrln("Error:", err)
- os.Exit(1)
- }
- },
-}
-
func init() {
encryptCmd.Flags().StringP("output", "o", "", "output file (required)")
encryptCmd.Flags().Bool("ssh", false, "encrypt using SSH public keys (interactive)")