From cfe7e601ed13a688695b5e7e4826cce0430b2750 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 24 May 2022 09:58:58 -0400 Subject: [PATCH] fix: server cli interface --- 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(-) delete mode 100644 server/git.go diff --git a/server/cmd/cat.go b/server/cmd/cat.go index f8bfd9004b7a0ddd5150d61b0725bfa9297ebab9..75008918675b09849c19b662e83b0ccf6e205f34 100644 --- a/server/cmd/cat.go +++ b/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 diff --git a/server/cmd/cmd.go b/server/cmd/cmd.go index 6cd371be928c55dcc655f98b5f4c5963a84b3fef..684fe7ec65fb17559bceb05151208158b02bb134 100644 --- a/server/cmd/cmd.go +++ b/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 } diff --git a/server/cmd/git.go b/server/cmd/git.go index ca8095ac5082c9a0e83eb42b6c4d883082250b4f..d9a60070e6bdbf3ab3d1419d758bf93be75fc2c8 100644 --- a/server/cmd/git.go +++ b/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 diff --git a/server/cmd/list.go b/server/cmd/list.go index f07a2a68f3600357e0020ea6269a954714740220..e1b239b186f8d453fbbc810efa4b3c07d630f4b8 100644 --- a/server/cmd/list.go +++ b/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 } diff --git a/server/git.go b/server/git.go deleted file mode 100644 index f08e1e58affc049ab3cd3cc6bc579a7a7e00177c..0000000000000000000000000000000000000000 --- a/server/git.go +++ /dev/null @@ -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 -} diff --git a/server/middleware.go b/server/middleware.go index ce3d3df9c0af3c6c33f50732415dea0bb78b1c00..76b3312850661ebbc213220eee2f480cc267844c 100644 --- a/server/middleware.go +++ b/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 diff --git a/server/session.go b/server/session.go index b9021b114aaccffe3a6c3788edd49432f029a3a2..de6f2d4c3d33a0fb2feb01181c8b5d6d04a3dc12 100644 --- a/server/session.go +++ b/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 } }