aboutsummaryrefslogtreecommitdiff
path: root/cmd/eno/cobra.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/eno/cobra.go')
-rw-r--r--cmd/eno/cobra.go162
1 files changed, 138 insertions, 24 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index 69f621f..09fce73 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -1,11 +1,13 @@
package main
import (
- "bufio"
"fmt"
"os"
+ "path/filepath"
+ "strings"
"github.com/spf13/cobra"
+ "gitlab.com/whiteboardguy/eno/internal/crud"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"gitlab.com/whiteboardguy/eno/internal/utils"
)
@@ -25,18 +27,17 @@ var encryptCmd = &cobra.Command{
return fmt.Errorf("failed to get output flag: %v", err)
}
+ if output == "" {
+ output = args[0] + ".eno"
+ }
+
if _, err := os.Stat(output); err == nil {
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output)
- scanner.Scan()
- overwriteConfirm := scanner.Text()
- if err := scanner.Err(); err != nil {
- return fmt.Errorf("failed to read confirmation: %v", err)
+ ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
+ if err != nil {
+ return err
}
-
- if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- cmd.PrintErrln("Error: write aborted")
- os.Exit(1)
+ if !ok {
+ return fmt.Errorf("write aborted")
}
}
@@ -44,12 +45,16 @@ var encryptCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("failed to read input file: %v", err)
}
+ utils.TryMlock(fileBytes)
+ defer clear(fileBytes)
fmt.Printf("Enter a super secure passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
+ utils.TryMlock(password)
+ defer clear(password)
encrypted, err := crypt.EncryptRawBytes(fileBytes, password)
if err != nil {
@@ -75,18 +80,17 @@ var decryptCmd = &cobra.Command{
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 {
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output)
- scanner.Scan()
- overwriteConfirm := scanner.Text()
- if err := scanner.Err(); err != nil {
- return fmt.Errorf("failed to read confirmation: %v", err)
+ ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
+ if err != nil {
+ return err
}
-
- if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- cmd.PrintErrln("Error: write aborted")
- os.Exit(1)
+ if !ok {
+ return fmt.Errorf("write aborted")
}
}
@@ -100,12 +104,16 @@ var decryptCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
+ utils.TryMlock(password)
+ defer clear(password)
plaintext, err := crypt.DecryptRawBytes(fileBytes, password)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
+ utils.TryMlock(plaintext)
+ defer clear(plaintext)
if err := os.WriteFile(output, plaintext, 0644); err != nil {
return fmt.Errorf("failed to write output file: %v", err)
@@ -115,12 +123,118 @@ var decryptCmd = &cobra.Command{
},
}
+var initCmd = &cobra.Command{
+ Use: "init",
+ Short: "intialize Eno config",
+ Args: cobra.ExactArgs(0),
+ Run: func(cmd *cobra.Command, args []string) {
+ err := handlerInit()
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+
+ cmd.Println("Config initialized at ", Conf.ConfPath)
+ },
+}
+
+var newCmd = &cobra.Command{
+ Use: "new <note>",
+ Short: "create a new encrypted note",
+ Args: cobra.MinimumNArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ output, err := cmd.Flags().GetString("output")
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+
+ editFlag, err := cmd.Flags().GetBool("edit")
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+
+ noteName := strings.Join(args, " ")
+
+ if output == "" {
+ if strings.ContainsRune(noteName, filepath.Separator) {
+ output = noteName + ".eno"
+ } else {
+ output = filepath.Join(Conf.DataDir, noteName+".eno")
+ }
+ }
+
+ if _, err := os.Stat(output); err == nil {
+ ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+ if !ok {
+ cmd.PrintErrln("Error: write aborted")
+ os.Exit(1)
+ }
+ }
+
+ err = crud.NewNote(noteName, output)
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+
+ if editFlag {
+ target := filepath.Clean(output)
+ target = strings.TrimSuffix(target, ".eno")
+ target += ".eno"
+
+ err = crud.EditNote(target, Conf.EditorString)
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+ }
+ },
+}
+
+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"
+
+ err = crud.EditNote(target, Conf.EditorString)
+ if err != nil {
+ cmd.PrintErrln("Error:", err)
+ os.Exit(1)
+ }
+ },
+}
+
func init() {
encryptCmd.Flags().StringP("output", "o", "", "output file (required)")
- encryptCmd.MarkFlagRequired("output")
+ // encryptCmd.MarkFlagRequired("output")
decryptCmd.Flags().StringP("output", "o", "", "output file (required)")
- decryptCmd.MarkFlagRequired("output")
+ // decryptCmd.MarkFlagRequired("output")
+
+ newCmd.Flags().StringP("output", "o", "", "output file")
+ newCmd.Flags().BoolP("edit", "e", false, "edit after creation")
- cobraCmd.AddCommand(encryptCmd, decryptCmd)
+ cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd)
}