031d796826c9a46e383ec8553c9ed3288cde8569

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

Rename Stats interface to Callbacks

Diff

  1diff --git a/config/config.go b/config/config.go
  2index 160e71120b1b4607e1567431cdf6fdc5a93a1555..897bd014d532823ee9f431f21a806aaec53b3320 100644
  3--- a/config/config.go
  4+++ b/config/config.go
  5@@ -3,10 +3,16 @@ package config
  6 import (
  7 	"log"
  8 
  9-	"github.com/charmbracelet/soft/stats"
 10 	"github.com/meowgorithm/babyenv"
 11 )
 12 
 13+// Callbacks provides an interface that can be used to run callbacks on different events.
 14+type Callbacks interface {
 15+	Tui(action string)
 16+	Push(repo string)
 17+	Fetch(repo string)
 18+}
 19+
 20 // Config is the configuration for the soft-serve.
 21 type Config struct {
 22 	Host            string `env:"SOFT_SERVE_HOST" default:""`
 23@@ -14,7 +20,7 @@ type Config struct {
 24 	KeyPath         string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
 25 	RepoPath        string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
 26 	InitialAdminKey string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" default:""`
 27-	Stats           stats.Stats
 28+	Callbacks       Callbacks
 29 }
 30 
 31 // DefaultConfig returns a Config with the values populated with the defaults
 32@@ -25,10 +31,10 @@ func DefaultConfig() *Config {
 33 	if err != nil {
 34 		log.Fatalln(err)
 35 	}
 36-	return scfg.WithStats(stats.NewStats())
 37+	return scfg.WithCallbacks(nil)
 38 }
 39 
 40-func (cfg *Config) WithStats(s stats.Stats) *Config {
 41-	cfg.Stats = s
 42+func (cfg *Config) WithCallbacks(c Callbacks) *Config {
 43+	cfg.Callbacks = c
 44 	return cfg
 45 }
 46diff --git a/internal/config/git.go b/internal/config/git.go
 47index 845c2fb8ac724e89c4e018b8b59dfb493e069f4f..6832455b434e9b4d9f3be3a9c0194ccb5e7b05c5 100644
 48--- a/internal/config/git.go
 49+++ b/internal/config/git.go
 50@@ -13,11 +13,15 @@ func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
 51 	if err != nil {
 52 		log.Printf("error reloading after push: %s", err)
 53 	}
 54-	cfg.Cfg.Stats.Push(repo)
 55+	if cfg.Cfg.Callbacks != nil {
 56+		cfg.Cfg.Callbacks.Push(repo)
 57+	}
 58 }
 59 
 60 func (cfg *Config) Fetch(repo string, pk ssh.PublicKey) {
 61-	cfg.Cfg.Stats.Fetch(repo)
 62+	if cfg.Cfg.Callbacks != nil {
 63+		cfg.Cfg.Callbacks.Fetch(repo)
 64+	}
 65 }
 66 
 67 func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
 68diff --git a/internal/tui/session.go b/internal/tui/session.go
 69index 9f3cb3951a50858c77effd85fc2f9d4049e18a92..9f2c48600f8c4699380c5938ea327ef95e6418d7 100644
 70--- a/internal/tui/session.go
 71+++ b/internal/tui/session.go
 72@@ -27,7 +27,9 @@ func SessionHandler(cfg *config.Config) func(ssh.Session) (tea.Model, []tea.Prog
 73 		}
 74 		scfg.Width = pty.Window.Width
 75 		scfg.Height = pty.Window.Height
 76-		cfg.Cfg.Stats.Tui("view")
 77+		if cfg.Cfg.Callbacks != nil {
 78+			cfg.Cfg.Callbacks.Tui("view")
 79+		}
 80 		return NewBubble(cfg, scfg), []tea.ProgramOption{tea.WithAltScreen()}
 81 	}
 82 }
 83diff --git a/stats/stats.go b/stats/stats.go
 84deleted file mode 100644
 85index b9eb7173603a0db1194f00a509361921a9a3e595..0000000000000000000000000000000000000000
 86--- a/stats/stats.go
 87+++ /dev/null
 88@@ -1,28 +0,0 @@
 89-package stats
 90-
 91-import "log"
 92-
 93-// Stats provides an interface that can be used to collect metrics about the server.
 94-type Stats interface {
 95-	Tui(action string)
 96-	Push(repo string)
 97-	Fetch(repo string)
 98-}
 99-
100-type stats struct{}
101-
102-func (s *stats) Tui(action string) {
103-	log.Printf("TUI: %s", action)
104-}
105-
106-func (s *stats) Push(repo string) {
107-	log.Printf("git push: %s", repo)
108-}
109-
110-func (s *stats) Fetch(repo string) {
111-	log.Printf("git fetch: %s", repo)
112-}
113-
114-func NewStats() Stats {
115-	return &stats{}
116-}