aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-12 16:41:00 +0530
committerishaan <contact@ishaan.cc>2026-07-12 16:41:00 +0530
commit050db499d18c61d90a68096027ac0d5aaa842f57 (patch)
tree2d0cd6db07407c2d44c8ea7ec899fb3088766d01 /cmd
parent7b5328536a82755c2724f2075c31035d709588df (diff)
add init/new/edit subcommands, config, mlock/cleared sensitive info
Diffstat (limited to 'cmd')
-rw-r--r--cmd/eno/cobra.go162
-rw-r--r--cmd/eno/config.go89
-rw-r--r--cmd/eno/main.go10
3 files changed, 236 insertions, 25 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)
}
diff --git a/cmd/eno/config.go b/cmd/eno/config.go
new file mode 100644
index 0000000..9cd4e16
--- /dev/null
+++ b/cmd/eno/config.go
@@ -0,0 +1,89 @@
+package main
+
+import (
+ "bufio"
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+type Config struct {
+ EditorString string
+ DataDir string
+ ConfPath string
+}
+var Conf Config
+
+func confPath() string {
+ if d := os.Getenv("XDG_CONFIG_HOME"); d != "" {
+ return filepath.Join(d, "eno", "eno.conf")
+ }
+ home, _ := os.UserHomeDir()
+ return filepath.Join(home, ".config", "eno", "eno.conf")
+}
+
+func defaultDataDir() string {
+ home, _ := os.UserHomeDir()
+ return filepath.Join(home, ".EnoNotes")
+}
+
+func initConfig() error {
+ e := os.Getenv("EDITOR")
+ if e == "" {
+ e = "nvim"
+ }
+ Conf.EditorString = fmt.Sprintf("%s %%s", e)
+ Conf.DataDir = defaultDataDir()
+ Conf.ConfPath = confPath()
+
+ f, err := os.Open(Conf.ConfPath)
+ if err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ fmt.Fprintf(os.Stderr, "no config — run 'eno init'\n")
+ return nil
+ }
+ return fmt.Errorf("reading config: %w", err)
+ }
+ defer f.Close()
+
+ scanner := bufio.NewScanner(f)
+ for scanner.Scan() {
+ line := strings.TrimSpace(scanner.Text())
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+ key, val, ok := strings.Cut(line, "=")
+ if !ok {
+ continue
+ }
+ switch strings.TrimSpace(key) {
+ case "EDITOR_STRING":
+ if v := strings.TrimSpace(val); v != "" {
+ Conf.EditorString = v
+ }
+ case "DATA_DIR":
+ if v := strings.TrimSpace(val); v != "" {
+ Conf.DataDir = v
+ }
+ }
+ }
+ return scanner.Err()
+}
+
+func handlerInit() error {
+ if _, err := os.Stat(Conf.ConfPath); err == nil {
+ return nil
+ }
+ if err := os.MkdirAll(filepath.Dir(Conf.ConfPath), 0755); err != nil {
+ return fmt.Errorf("creating config dir: %w", err)
+ }
+ f, err := os.Create(Conf.ConfPath)
+ if err != nil {
+ return fmt.Errorf("creating config file: %w", err)
+ }
+ defer f.Close()
+ _, err = fmt.Fprintf(f, "EDITOR_STRING=%s\nDATA_DIR=%s\n", Conf.EditorString, Conf.DataDir)
+ return err
+}
diff --git a/cmd/eno/main.go b/cmd/eno/main.go
index 52aadf4..a1c113a 100644
--- a/cmd/eno/main.go
+++ b/cmd/eno/main.go
@@ -1,8 +1,16 @@
package main
-import "os"
+import (
+ "fmt"
+ "os"
+)
func main() {
+ if err := initConfig(); err != nil {
+ fmt.Fprintf(os.Stderr, "config error: %v\nrun 'eno init' to fix\n", err)
+ os.Exit(1)
+ }
+
if err := cobraCmd.Execute(); err != nil {
os.Exit(1)
}