aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-19 13:09:56 +0530
committerishaan <contact@ishaan.cc>2026-07-19 13:09:56 +0530
commit8a5de371fccb89635bc1a624d8b1aca52aa18da4 (patch)
tree89f3b2bec4e2425823534d4db46e3aecc2a15d91 /internal
parent80b1c6deec723095f08de6aee289d4fb32ca9337 (diff)
better config (package)
Diffstat (limited to 'internal')
-rw-r--r--internal/config/config.go197
-rw-r--r--internal/utils/ssh_helpers.go38
-rw-r--r--internal/utils/utils.go34
3 files changed, 235 insertions, 34 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..f31c4ef
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,197 @@
+package config
+
+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 Init() 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 Init()
+}
+
+var configFieldKey = map[string]string{
+ "EditorString": "EDITOR_STRING",
+ "DataDir": "DATA_DIR",
+ "DefaultSSHPrivPath": "DEFAULT_SSH_PRIV",
+ "DefaultSSHPubPath": "DEFAULT_SSH_PUB",
+ "SSHInitialized": "SSH_INIT",
+}
+
+func Update(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 Init()
+}
diff --git a/internal/utils/ssh_helpers.go b/internal/utils/ssh_helpers.go
new file mode 100644
index 0000000..9ffcf0a
--- /dev/null
+++ b/internal/utils/ssh_helpers.go
@@ -0,0 +1,38 @@
+package utils
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+)
+
+func GetPubKeyFromService(username, service string) ([]byte, error) {
+ // TODO: Read custom services from config
+
+ services := map[string][2]string{
+ "github" : [2]string{"https://github.com/", ".keys"},
+ "gitlab" : [2]string{"https://gitlab.com/", ".keys"},
+ "codeberg": [2]string{"https://codeberg.org", ".keys"},
+ }
+
+ url := strings.TrimSuffix(services[service][0], "/") + "/" + username + services[service][1]
+
+ resp, err := http.Get(url)
+ if err != nil {
+ return nil, fmt.Errorf("failed to fetch key from %s: %w", service, err)
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("%s returned status %d", service, resp.StatusCode)
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response from %s: %w", service, err)
+ }
+
+ return body, nil
+}
+
diff --git a/internal/utils/utils.go b/internal/utils/utils.go
index a43b268..cea00a3 100644
--- a/internal/utils/utils.go
+++ b/internal/utils/utils.go
@@ -3,8 +3,6 @@ package utils
import (
"crypto/rand"
"fmt"
- "io"
- "net/http"
"os"
"os/signal"
"strconv"
@@ -102,35 +100,3 @@ func CheckFileAuthMode(notePath string) (crypt.AuthMode, error) {
return headers.Auth, nil
}
-
-func GetPubKeyFromService(username, service string) ([]byte, error) {
- // TODO: Read custom services from config
-
- var url string
-
- switch service {
- case "github":
- url = "https://github.com/" + username + ".keys"
- case "gitlab":
- url = "https://gitlab.com/" + username + ".keys"
- default:
- return nil, fmt.Errorf("unknown service: %s", service)
- }
-
- resp, err := http.Get(url)
- if err != nil {
- return nil, fmt.Errorf("failed to fetch key from %s: %w", service, err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("%s returned status %d", service, resp.StatusCode)
- }
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("failed to read response from %s: %w", service, err)
- }
-
- return body, nil
-}