fix: server cli interface

Ayman Bagabas created

Change summary

server/cmd/cat.go    |  2 +-
server/cmd/cmd.go    | 19 +++++++++++++++++--
server/cmd/git.go    |  2 +-
server/cmd/list.go   |  2 +-
server/git.go        | 25 -------------------------
server/middleware.go |  4 ++--
server/session.go    | 39 +++------------------------------------
7 files changed, 25 insertions(+), 68 deletions(-)

Detailed changes

server/cmd/cat.go 🔗

@@ -43,7 +43,7 @@ func CatCommand() *cobra.Command {
 			var repo *config.Repo
 			repoExists := false
 			for _, rp := range ac.Source.AllRepos() {
-				if rp.Name() == rn {
+				if rp.Repo() == rn {
 					repoExists = true
 					repo = rp
 					break

server/cmd/cmd.go 🔗

@@ -8,6 +8,21 @@ import (
 	"github.com/spf13/cobra"
 )
 
+// ContextKey is a type that can be used as a key in a context.
+type ContextKey string
+
+// String returns the string representation of the ContextKey.
+func (c ContextKey) String() string {
+	return "soft-serve cli context key " + string(c)
+}
+
+var (
+	// ConfigCtxKey is the key for the config in the context.
+	ConfigCtxKey = ContextKey("config")
+	// SessionCtxKey is the key for the session in the context.
+	SessionCtxKey = ContextKey("session")
+)
+
 var (
 	// ErrUnauthorized is returned when the user is not authorized to perform action.
 	ErrUnauthorized = fmt.Errorf("Unauthorized")
@@ -64,7 +79,7 @@ func RootCommand() *cobra.Command {
 
 func fromContext(cmd *cobra.Command) (*appCfg.Config, ssh.Session) {
 	ctx := cmd.Context()
-	ac := ctx.Value("config").(*appCfg.Config)
-	s := ctx.Value("session").(ssh.Session)
+	ac := ctx.Value(ConfigCtxKey).(*appCfg.Config)
+	s := ctx.Value(SessionCtxKey).(ssh.Session)
 	return ac, s
 }

server/cmd/git.go 🔗

@@ -27,7 +27,7 @@ func GitCommand() *cobra.Command {
 			rn := args[0]
 			repoExists := false
 			for _, rp := range ac.Source.AllRepos() {
-				if rp.Name() == rn {
+				if rp.Repo() == rn {
 					repoExists = true
 					repo = rp
 					break

server/cmd/list.go 🔗

@@ -33,7 +33,7 @@ func ListCommand() *cobra.Command {
 			}
 			if path == "" || path == "." || path == "/" {
 				for _, r := range ac.Source.AllRepos() {
-					fmt.Fprintln(s, r.Name())
+					fmt.Fprintln(s, r.Repo())
 				}
 				return nil
 			}

server/git.go 🔗

@@ -1,25 +0,0 @@
-package server
-
-import (
-	"github.com/charmbracelet/soft-serve/config"
-	"github.com/charmbracelet/soft-serve/ui/git"
-)
-
-// source is a wrapper around config.RepoSource that implements git.GitRepoSource.
-type source struct {
-	*config.RepoSource
-}
-
-// GetRepo implements git.GitRepoSource.
-func (s *source) GetRepo(name string) (git.GitRepo, error) {
-	return s.RepoSource.GetRepo(name)
-}
-
-// AllRepos implements git.GitRepoSource.
-func (s *source) AllRepos() []git.GitRepo {
-	rs := make([]git.GitRepo, 0)
-	for _, r := range s.RepoSource.AllRepos() {
-		rs = append(rs, r)
-	}
-	return rs
-}

server/middleware.go 🔗

@@ -20,8 +20,8 @@ func softMiddleware(ac *appCfg.Config) wish.Middleware {
 					return
 				}
 				ctx := s.Context()
-				ctx = context.WithValue(ctx, "config", ac) //nolint:revive
-				ctx = context.WithValue(ctx, "session", s) //nolint:revive
+				ctx = context.WithValue(ctx, cmd.ConfigCtxKey, ac)
+				ctx = context.WithValue(ctx, cmd.SessionCtxKey, s)
 
 				use := "ssh"
 				port := ac.Port

server/session.go 🔗

@@ -8,51 +8,19 @@ import (
 	appCfg "github.com/charmbracelet/soft-serve/config"
 	"github.com/charmbracelet/soft-serve/ui"
 	"github.com/charmbracelet/soft-serve/ui/common"
-	"github.com/charmbracelet/soft-serve/ui/git"
 	"github.com/charmbracelet/soft-serve/ui/keymap"
 	"github.com/charmbracelet/soft-serve/ui/styles"
 	bm "github.com/charmbracelet/wish/bubbletea"
 	"github.com/gliderlabs/ssh"
 )
 
-type Session struct {
-	tea.Model
-	*tea.Program
-	session ssh.Session
-	Cfg     *appCfg.Config
-}
-
-func (s *Session) Config() *appCfg.Config {
-	return s.Cfg
-}
-
-func (s *Session) Send(msg tea.Msg) {
-	s.Program.Send(msg)
-}
-
-func (s *Session) PublicKey() ssh.PublicKey {
-	return s.session.PublicKey()
-}
-
-func (s *Session) Session() ssh.Session {
-	return s.session
-}
-
-func (s *Session) Source() git.GitRepoSource {
-	return &source{s.Cfg.Source}
-}
-
+// SessionHandler is the soft-serve bubbletea ssh session handler.
 func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
 	return func(s ssh.Session) *tea.Program {
 		pty, _, active := s.Pty()
 		if !active {
-			fmt.Println("not active")
 			return nil
 		}
-		sess := &Session{
-			session: s,
-			Cfg:     ac,
-		}
 		cmd := s.Command()
 		initialRepo := ""
 		if len(cmd) == 1 {
@@ -72,7 +40,8 @@ func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
 			Height: pty.Window.Height,
 		}
 		m := ui.New(
-			sess,
+			ac,
+			s,
 			c,
 			initialRepo,
 		)
@@ -83,8 +52,6 @@ func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
 			tea.WithoutCatchPanics(),
 			tea.WithMouseCellMotion(),
 		)
-		sess.Model = m
-		sess.Program = p
 		return p
 	}
 }