aboutsummaryrefslogtreecommitdiff
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
parent80b1c6deec723095f08de6aee289d4fb32ca9337 (diff)
better config (package)
-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/main.go4
-rw-r--r--internal/config/config.go (renamed from cmd/eno/config.go)13
-rw-r--r--internal/utils/ssh_helpers.go38
-rw-r--r--internal/utils/utils.go34
7 files changed, 72 insertions, 62 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/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)
}
diff --git a/cmd/eno/config.go b/internal/config/config.go
index 90bdf1b..f31c4ef 100644
--- a/cmd/eno/config.go
+++ b/internal/config/config.go
@@ -1,4 +1,4 @@
-package main
+package config
import (
"bufio"
@@ -18,6 +18,7 @@ type Config struct {
DefaultSSHPubPath string
SSHInitialized bool
}
+
var Conf Config
func confPath() (string, error) {
@@ -39,7 +40,7 @@ func defaultDataDir() (string, error) {
return filepath.Join(home, ".EnoNotes"), nil
}
-func initConfig() error {
+func Init() error {
e := os.Getenv("EDITOR")
if e == "" {
e = "nvim"
@@ -102,7 +103,7 @@ func initConfig() error {
return scanner.Err()
}
-func handlerInit() error {
+func HandlerInit() error {
if _, err := os.Stat(Conf.ConfPath); err == nil {
return nil
}
@@ -131,7 +132,7 @@ func handlerInit() error {
return err
}
- return initConfig()
+ return Init()
}
var configFieldKey = map[string]string{
@@ -142,7 +143,7 @@ var configFieldKey = map[string]string{
"SSHInitialized": "SSH_INIT",
}
-func updateConfig(key string, val any) error {
+func Update(key string, val any) error {
configKey, ok := configFieldKey[key]
if !ok {
return fmt.Errorf("unknown config field: %s", key)
@@ -192,5 +193,5 @@ func updateConfig(key string, val any) error {
return err
}
- return initConfig()
+ 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
-}