summaryrefslogtreecommitdiff
path: root/internal/client
diff options
context:
space:
mode:
Diffstat (limited to 'internal/client')
-rw-r--r--internal/client/client.go7
-rw-r--r--internal/client/config/config.go71
2 files changed, 78 insertions, 0 deletions
diff --git a/internal/client/client.go b/internal/client/client.go
index a60c1e8..df7573f 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -6,6 +6,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
+ "github.com/kyren223/eko/internal/client/config"
"github.com/kyren223/eko/internal/client/ui"
"github.com/kyren223/eko/internal/client/ui/auth"
"github.com/kyren223/eko/pkg/assert"
@@ -22,6 +23,12 @@ func (c BubbleTeaCloser) Close() error {
func Run() {
log.Println("client started")
+
+ err := config.Load()
+ if err != nil {
+ log.Fatalf("Config file at '%v' was unable to load successfully\n%v\n", config.ConfigFile, err)
+ }
+
program := tea.NewProgram(initialModel(), tea.WithAltScreen())
assert.AddFlush(BubbleTeaCloser{program})
diff --git a/internal/client/config/config.go b/internal/client/config/config.go
new file mode 100644
index 0000000..2353747
--- /dev/null
+++ b/internal/client/config/config.go
@@ -0,0 +1,71 @@
+package config
+
+import (
+ "encoding/json"
+ "errors"
+ "os"
+ "path/filepath"
+)
+
+type Config struct {
+ PrivateKeyPath string
+}
+
+func Default() Config {
+ return Config{
+ PrivateKeyPath: "",
+ }
+}
+
+var (
+ config = Config{}
+ Dir string
+ ConfigFile string
+)
+
+func Load() error {
+ userConfigDir, err := os.UserConfigDir()
+ if err != nil {
+ return err
+ }
+
+ Dir = filepath.Join(userConfigDir, "eko")
+ err = os.MkdirAll(Dir, 0o755)
+ if err != nil {
+ return err
+ }
+
+ ConfigFile = filepath.Join(Dir, "config.json")
+ contents, err := os.ReadFile(ConfigFile)
+ if errors.Is(err, os.ErrNotExist) {
+ config = Default()
+ return write()
+ }
+ if err != nil {
+ return err
+ }
+
+ err = json.Unmarshal(contents, &config)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func write() error {
+ b, err := json.MarshalIndent(config, "", " ")
+ if err != nil {
+ return err
+ }
+ return os.WriteFile(ConfigFile, b, 0o644)
+}
+
+func Read() Config {
+ return config
+}
+
+func Use(f func(config *Config)) error {
+ f(&config)
+ return write()
+}