aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/eno/cobra.go19
-rw-r--r--cmd/eno/cobra_new.go17
-rw-r--r--cmd/eno/cobra_ssh.go9
-rw-r--r--cmd/eno/config.go196
-rw-r--r--cmd/eno/main.go4
5 files changed, 27 insertions, 218 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index cf373d5..4f37bf1 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -7,6 +7,7 @@ import (
"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"
@@ -54,10 +55,10 @@ var encryptCmd = &cobra.Command{
}
if sshFlag {
- if Conf.DefaultSSHPubPath == "" {
+ if config.Conf.DefaultSSHPubPath == "" {
return fmt.Errorf("SSH not configured. Run 'eno ssh init' first")
}
- defaultPubKeyBytes, err := os.ReadFile(Conf.DefaultSSHPubPath)
+ defaultPubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath)
if err != nil {
return fmt.Errorf("failed to read default SSH public key: %v", err)
}
@@ -141,10 +142,10 @@ var decryptCmd = &cobra.Command{
}
if authMode == crypt.SSH {
- if Conf.DefaultSSHPrivPath == "" {
+ if config.Conf.DefaultSSHPrivPath == "" {
return fmt.Errorf("SSH not configured. Run 'eno ssh init' first")
}
- plaintext, err := crypt.DecryptBytesSSH(fileBytes, Conf.DefaultSSHPrivPath)
+ plaintext, err := crypt.DecryptBytesSSH(fileBytes, config.Conf.DefaultSSHPrivPath)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
@@ -189,13 +190,13 @@ var initCmd = &cobra.Command{
Short: "intialize Eno config",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
- err := handlerInit()
+ err := config.HandlerInit()
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
- cmd.Println("Config initialized at ", Conf.ConfPath)
+ cmd.Println("Config initialized at ", config.Conf.ConfPath)
},
}
@@ -221,7 +222,7 @@ var editCmd = &cobra.Command{
target += ".eno"
if _, err := os.Stat(target); os.IsNotExist(err) {
- alt := filepath.Join(Conf.DataDir, target)
+ alt := filepath.Join(config.Conf.DataDir, target)
if _, err2 := os.Stat(alt); err2 == nil {
target = alt
}
@@ -235,9 +236,9 @@ var editCmd = &cobra.Command{
switch authMode {
case crypt.SSH:
- err = crud.EditNoteSSH(target, Conf.DefaultSSHPrivPath, Conf.EditorString)
+ err = crud.EditNoteSSH(target, config.Conf.DefaultSSHPrivPath, config.Conf.EditorString)
case crypt.Pass:
- err = crud.EditNotePass(target, Conf.EditorString)
+ err = crud.EditNotePass(target, config.Conf.EditorString)
default:
cmd.PrintErrln("Error: unknown auth mode:", authMode)
os.Exit(1)
diff --git a/cmd/eno/cobra_new.go b/cmd/eno/cobra_new.go
index 19436fb..7d46546 100644
--- a/cmd/eno/cobra_new.go
+++ b/cmd/eno/cobra_new.go
@@ -6,6 +6,7 @@ import (
"strings"
"github.com/spf13/cobra"
+ "gitlab.com/whiteboardguy/eno/internal/config"
"gitlab.com/whiteboardguy/eno/internal/crud"
"gitlab.com/whiteboardguy/eno/internal/utils"
)
@@ -39,16 +40,16 @@ var newCmd = &cobra.Command{
if strings.ContainsRune(noteName, filepath.Separator) {
output = noteName + ".eno"
} else {
- output = filepath.Join(Conf.DataDir, noteName+".eno")
+ output = filepath.Join(config.Conf.DataDir, noteName+".eno")
}
}
- if _, err := os.Stat(Conf.DataDir); os.IsNotExist(err) {
- if err := os.MkdirAll(Conf.DataDir, 0700); err != nil {
+ if _, err := os.Stat(config.Conf.DataDir); os.IsNotExist(err) {
+ if err := os.MkdirAll(config.Conf.DataDir, 0700); err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
- cmd.Println("Created data directory:", Conf.DataDir)
+ cmd.Println("Created data directory:", config.Conf.DataDir)
}
if _, err := os.Stat(output); err == nil {
@@ -64,13 +65,13 @@ var newCmd = &cobra.Command{
}
if sshFlag && editFlag {
- pubKeyBytes, err := os.ReadFile(Conf.DefaultSSHPubPath)
+ pubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
- err = crud.NewNoteSSHEdit(output, pubKeyBytes, Conf.DefaultSSHPrivPath, Conf.EditorString)
+ err = crud.NewNoteSSHEdit(output, pubKeyBytes, config.Conf.DefaultSSHPrivPath, config.Conf.EditorString)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
@@ -79,7 +80,7 @@ var newCmd = &cobra.Command{
}
if sshFlag {
- pubKeyBytes, err := os.ReadFile(Conf.DefaultSSHPubPath)
+ pubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
@@ -94,7 +95,7 @@ var newCmd = &cobra.Command{
}
if editFlag {
- err = crud.NewNotePassEdit(output, Conf.EditorString)
+ err = crud.NewNotePassEdit(output, config.Conf.EditorString)
if err != nil {
cmd.PrintErrln("Error:", err)
os.Exit(1)
diff --git a/cmd/eno/cobra_ssh.go b/cmd/eno/cobra_ssh.go
index 084960a..f219cfb 100644
--- a/cmd/eno/cobra_ssh.go
+++ b/cmd/eno/cobra_ssh.go
@@ -9,6 +9,7 @@ import (
"strings"
"github.com/spf13/cobra"
+ "gitlab.com/whiteboardguy/eno/internal/config"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"gitlab.com/whiteboardguy/eno/internal/utils"
"golang.org/x/crypto/ssh"
@@ -38,7 +39,7 @@ var sshInitCmd = &cobra.Command{
Short: "setup ssh for Eno notes",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
- if Conf.SSHInitialized {
+ if config.Conf.SSHInitialized {
cmd.PrintErrln("SSH is already initialized")
ok, err := utils.AskUserYesNo("Reinitialize SSH?", false)
if err != nil {
@@ -150,17 +151,17 @@ var sshInitCmd = &cobra.Command{
os.Exit(1)
}
- if err := updateConfig("DefaultSSHPrivPath", privateKeyPath); err != nil {
+ if err := config.Update("DefaultSSHPrivPath", privateKeyPath); err != nil {
clear(privKeyBytes)
cmd.PrintErrln(err)
os.Exit(1)
}
- if err := updateConfig("DefaultSSHPubPath", publicKeyPath); err != nil {
+ if err := config.Update("DefaultSSHPubPath", publicKeyPath); err != nil {
clear(privKeyBytes)
cmd.PrintErrln(err)
os.Exit(1)
}
- if err := updateConfig("SSHInitialized", true); err != nil {
+ if err := config.Update("SSHInitialized", true); err != nil {
clear(privKeyBytes)
cmd.PrintErrln(err)
os.Exit(1)
diff --git a/cmd/eno/config.go b/cmd/eno/config.go
deleted file mode 100644
index 90bdf1b..0000000
--- a/cmd/eno/config.go
+++ /dev/null
@@ -1,196 +0,0 @@
-package main
-
-import (
- "bufio"
- "errors"
- "fmt"
- "os"
- "path/filepath"
- "strconv"
- "strings"
-)
-
-type Config struct {
- EditorString string
- DataDir string
- ConfPath string
- DefaultSSHPrivPath string
- DefaultSSHPubPath string
- SSHInitialized bool
-}
-var Conf Config
-
-func confPath() (string, error) {
- if d := os.Getenv("XDG_CONFIG_HOME"); d != "" {
- return filepath.Join(d, "eno", "eno.conf"), nil
- }
- home, err := os.UserHomeDir()
- if err != nil {
- return "", fmt.Errorf("home dir: %w", err)
- }
- return filepath.Join(home, ".config", "eno", "eno.conf"), nil
-}
-
-func defaultDataDir() (string, error) {
- home, err := os.UserHomeDir()
- if err != nil {
- return "", fmt.Errorf("home dir: %w", err)
- }
- return filepath.Join(home, ".EnoNotes"), nil
-}
-
-func initConfig() error {
- e := os.Getenv("EDITOR")
- if e == "" {
- e = "nvim"
- }
- Conf.EditorString = fmt.Sprintf("%s %%s", e)
-
- var err error
- Conf.DataDir, err = defaultDataDir()
- if err != nil {
- return fmt.Errorf("default data dir: %w", err)
- }
- Conf.ConfPath, err = confPath()
- if err != nil {
- return fmt.Errorf("config path: %w", err)
- }
- Conf.SSHInitialized = false
-
- f, err := os.Open(Conf.ConfPath)
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- 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
- }
- case "SSH_INIT":
- if v, err := strconv.ParseBool(strings.TrimSpace(val)); err == nil {
- Conf.SSHInitialized = v
- }
- case "DEFAULT_SSH_PRIV":
- if v := strings.TrimSpace(val); v != "" {
- Conf.DefaultSSHPrivPath = v
- }
- case "DEFAULT_SSH_PUB":
- if v := strings.TrimSpace(val); v != "" {
- Conf.DefaultSSHPubPath = 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.OpenFile(Conf.ConfPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
- if err != nil {
- return fmt.Errorf("creating config file: %w", err)
- }
- defer f.Close()
-
- defaultConfLines := []string{
- fmt.Sprintf("EDITOR_STRING=%s\n", Conf.EditorString),
-
- fmt.Sprintf("DATA_DIR=%s\n", Conf.DataDir),
-
- fmt.Sprintf("SSH_INIT=%t\n", Conf.SSHInitialized),
-
- "# Change 'DEFAULT_SSH_PRIV' to a private ssh key you want to use regularly to encrypt.",
- "# Change 'DEFAULT_SSH_PUB' to a private ssh key you want to use regularly to decrypt.",
- }
-
- _, err = fmt.Fprintf(f, "%s\n", strings.Join(defaultConfLines, "\n"))
- if err != nil {
- return err
- }
-
- return initConfig()
-}
-
-var configFieldKey = map[string]string{
- "EditorString": "EDITOR_STRING",
- "DataDir": "DATA_DIR",
- "DefaultSSHPrivPath": "DEFAULT_SSH_PRIV",
- "DefaultSSHPubPath": "DEFAULT_SSH_PUB",
- "SSHInitialized": "SSH_INIT",
-}
-
-func updateConfig(key string, val any) error {
- configKey, ok := configFieldKey[key]
- if !ok {
- return fmt.Errorf("unknown config field: %s", key)
- }
-
- var valStr string
- switch v := val.(type) {
- case bool:
- valStr = strconv.FormatBool(v)
- case string:
- valStr = v
- default:
- valStr = fmt.Sprintf("%v", v)
- }
-
- data, err := os.ReadFile(Conf.ConfPath)
- if err != nil {
- return fmt.Errorf("reading config: %w", err)
- }
-
- lines := strings.Split(string(data), "\n")
- newLine := fmt.Sprintf("%s=%s", configKey, valStr)
- found := false
-
- for i, line := range lines {
- trimmed := strings.TrimSpace(line)
- if trimmed == "" || strings.HasPrefix(trimmed, "#") {
- continue
- }
- k, _, ok := strings.Cut(trimmed, "=")
- if !ok {
- continue
- }
- if strings.TrimSpace(k) == configKey {
- lines[i] = newLine
- found = true
- break
- }
- }
-
- if !found {
- lines = append(lines, newLine)
- }
-
- err = os.WriteFile(Conf.ConfPath, []byte(strings.Join(lines, "\n")), 0600)
- if err != nil {
- return err
- }
-
- return initConfig()
-}
diff --git a/cmd/eno/main.go b/cmd/eno/main.go
index a1c113a..2f69b23 100644
--- a/cmd/eno/main.go
+++ b/cmd/eno/main.go
@@ -3,10 +3,12 @@ package main
import (
"fmt"
"os"
+
+ "gitlab.com/whiteboardguy/eno/internal/config"
)
func main() {
- if err := initConfig(); err != nil {
+ if err := config.Init(); err != nil {
fmt.Fprintf(os.Stderr, "config error: %v\nrun 'eno init' to fix\n", err)
os.Exit(1)
}