tui: make app config take server config

Ayman Bagabas created

Change summary

internal/config/config.go              | 29 ++++++++++++++-------------
internal/config/git.go                 | 10 +-------
internal/tui/bubbles/commits/bubble.go |  2 
internal/tui/session.go                |  4 --
4 files changed, 19 insertions(+), 26 deletions(-)

Detailed changes

internal/config/config.go 🔗

@@ -9,8 +9,8 @@ import (
 	"os"
 	"path/filepath"
 
+	"github.com/charmbracelet/soft/config"
 	"github.com/charmbracelet/soft/internal/git"
-	"github.com/charmbracelet/soft/stats"
 	gg "github.com/go-git/go-git/v5"
 	"github.com/go-git/go-git/v5/plumbing/object"
 )
@@ -24,7 +24,7 @@ type Config struct {
 	Users        []User `yaml:"users"`
 	Repos        []Repo `yaml:"repos"`
 	Source       *git.RepoSource
-	Stats        stats.Stats
+	Cfg          *config.Config
 }
 
 type User struct {
@@ -41,19 +41,20 @@ type Repo struct {
 	Private bool   `yaml:"private"`
 }
 
-func (cfg *Config) WithStats(s stats.Stats) *Config {
-	cfg.Stats = s
-	return cfg
-}
-
-func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, error) {
+func NewConfig(cfg *config.Config) (*Config, error) {
 	var anonAccess string
 	var yamlUsers string
 	var displayHost string
-	cfg := &Config{}
-	cfg.Host = host
-	cfg.Port = port
-	cfg.Source = rs
+	host := cfg.Host
+	port := cfg.Port
+	pk := cfg.InitialAdminKey
+	rs := cfg.RepoSource
+	c := &Config{
+		Cfg: cfg,
+	}
+	c.Host = cfg.Host
+	c.Port = port
+	c.Source = rs
 	if pk == "" {
 		anonAccess = "read-write"
 	} else {
@@ -75,11 +76,11 @@ func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, e
 		yamlUsers = defaultUserConfig
 	}
 	yaml := fmt.Sprintf("%s%s%s", yamlConfig, yamlUsers, exampleUserConfig)
-	err := cfg.createDefaultConfigRepo(yaml)
+	err := c.createDefaultConfigRepo(yaml)
 	if err != nil {
 		return nil, err
 	}
-	return cfg, nil
+	return c, nil
 }
 
 func (cfg *Config) reload() error {

internal/config/git.go 🔗

@@ -9,21 +9,15 @@ import (
 )
 
 func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
-	log.Printf("git push: %s", repo)
 	err := cfg.reload()
 	if err != nil {
 		log.Printf("error reloading after push: %s", err)
 	}
-	if cfg.Stats != nil {
-		cfg.Stats.Push()
-	}
+	cfg.Cfg.Stats.Push(repo)
 }
 
 func (cfg *Config) Fetch(repo string, pk ssh.PublicKey) {
-	log.Printf("git fetch: %s", repo)
-	if cfg.Stats != nil {
-		cfg.Stats.Fetch()
-	}
+	cfg.Cfg.Stats.Fetch(repo)
 }
 
 func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {

internal/tui/bubbles/commits/bubble.go 🔗

@@ -1,11 +1,11 @@
 package commits
 
 import (
-	"soft-serve/server/git"
 	"strings"
 
 	"github.com/charmbracelet/bubbles/viewport"
 	tea "github.com/charmbracelet/bubbletea"
+	"github.com/charmbracelet/soft/internal/git"
 	"github.com/dustin/go-humanize"
 )
 

internal/tui/session.go 🔗

@@ -27,9 +27,7 @@ func SessionHandler(cfg *config.Config) func(ssh.Session) (tea.Model, []tea.Prog
 		}
 		scfg.Width = pty.Window.Width
 		scfg.Height = pty.Window.Height
-		if cfg.Stats != nil {
-			cfg.Stats.Tui()
-		}
+		cfg.Cfg.Stats.Tui("view")
 		return NewBubble(cfg, scfg), []tea.ProgramOption{tea.WithAltScreen()}
 	}
 }