From 050db499d18c61d90a68096027ac0d5aaa842f57 Mon Sep 17 00:00:00 2001 From: ishaan Date: Sun, 12 Jul 2026 16:41:00 +0530 Subject: add init/new/edit subcommands, config, mlock/cleared sensitive info --- cmd/eno/config.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 cmd/eno/config.go (limited to 'cmd/eno/config.go') 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 +} -- cgit v1.3