Skip to content

Commit

Permalink
Add stats interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 12, 2021
1 parent 00195a6 commit 2a62d6a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"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"
)
Expand All @@ -21,6 +22,7 @@ type Config struct {
Users []User `yaml:"users"`
Repos []Repo `yaml:"repos"`
Source *git.RepoSource
Stats stats.Stats
}

type User struct {
Expand All @@ -37,6 +39,11 @@ 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) {
var anonAccess string
var yamlUsers string
Expand Down
11 changes: 7 additions & 4 deletions internal/config/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
if err != nil {
log.Printf("error reloading after push: %s", err)
}
if cfg.Stats != nil {
cfg.Stats.Push()
}
}

func (cfg *Config) Fetch(repo string, pk ssh.PublicKey) {
log.Printf("git fetch: %s", repo)
if cfg.Stats != nil {
cfg.Stats.Fetch()
}
}

func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
Expand All @@ -28,10 +34,7 @@ func (cfg *Config) PasswordHandler(ctx ssh.Context, password string) bool {
}

func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool {
if cfg.accessForKey("", pk) == gm.NoAccess {
return false
}
return true
return cfg.accessForKey("", pk) == gm.NoAccess
}

func (cfg *Config) accessForKey(repo string, pk ssh.PublicKey) gm.AccessLevel {
Expand Down
3 changes: 3 additions & 0 deletions internal/tui/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ 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()
}
return NewBubble(cfg, scfg), []tea.ProgramOption{tea.WithAltScreen()}
}
}
10 changes: 5 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type Config struct {
KeyPath string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
RepoPath string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
InitialAdminKey string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" default:""`
Cfg *config.Config
Stats stats.Stats
cfg *config.Config
}

// DefaultConfig returns a Config with the values populated with the defaults
Expand All @@ -50,15 +50,15 @@ func NewServer(scfg *Config) *ssh.Server {
if err != nil {
log.Fatalln(err)
}
scfg.Cfg = cfg
if scfg.Stats != nil {
cfg = cfg.WithStats(scfg.Stats)
}
scfg.cfg = cfg
mw := []wish.Middleware{
bm.Middleware(tui.SessionHandler(cfg)),
gm.Middleware(scfg.RepoPath, cfg),
lm.Middleware(),
}
if scfg.Stats != nil {
mw = append(mw, stats.Middleware(scfg.Stats))
}
s, err := wish.NewServer(
ssh.PublicKeyAuth(cfg.PublicKeyHandler),
ssh.PasswordAuth(cfg.PasswordHandler),
Expand Down
8 changes: 8 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package stats

// Stats provides an interface that can be used to collect metrics about the server.
type Stats interface {
Tui()
Push()
Fetch()
}

0 comments on commit 2a62d6a

Please sign in to comment.