From 0a38578ae6ad169c3696881cccfc87000e8ca4eb Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 5 Dec 2023 12:44:45 -0500 Subject: [PATCH] fix(ui): hide clone command while browse only --- cmd/soft/browse/browse.go | 7 +------ pkg/ui/common/common.go | 10 ++++++++++ pkg/ui/common/utils.go | 5 ----- pkg/ui/pages/repo/repo.go | 4 ++-- pkg/ui/pages/selection/item.go | 9 ++++++--- pkg/ui/pages/selection/selection.go | 2 +- 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cmd/soft/browse/browse.go b/cmd/soft/browse/browse.go index 43fa3e612036eaa0e36908cac09a9923cf51f366..ff1a1d06178a3c6775f3da7d6f1127c0bfa45c56 100644 --- a/cmd/soft/browse/browse.go +++ b/cmd/soft/browse/browse.go @@ -43,6 +43,7 @@ var Command = &cobra.Command{ output := termenv.DefaultOutput() ctx := cmd.Context() c := common.NewCommon(ctx, output, 0, 0) + c.HideCloneCmd = true comps := []common.TabComponent{ repo.NewReadme(c), repo.NewFiles(c), @@ -69,12 +70,6 @@ var Command = &cobra.Command{ }, } -func init() { - // HACK: This is a hack to hide the clone url - // TODO: Make this configurable - common.CloneCmd = func(publicURL, name string) string { return "" } -} - type state int const ( diff --git a/pkg/ui/common/common.go b/pkg/ui/common/common.go index 3a236215055e3f40d971a2c89393a99fd9aca03c..ceee1f58ef0cee0ae55bf61299a6286da003d66e 100644 --- a/pkg/ui/common/common.go +++ b/pkg/ui/common/common.go @@ -2,6 +2,7 @@ package common import ( "context" + "fmt" "github.com/charmbracelet/log" "github.com/charmbracelet/soft-serve/git" @@ -33,6 +34,7 @@ type Common struct { Zone *zone.Manager Output *termenv.Output Logger *log.Logger + HideCloneCmd bool } // NewCommon returns a new Common struct. @@ -95,3 +97,11 @@ func (c *Common) PublicKey() ssh.PublicKey { } return nil } + +// CloneCmd returns the clone command string. +func (c *Common) CloneCmd(publicURL, name string) string { + if c.HideCloneCmd { + return "" + } + return fmt.Sprintf("git clone %s", RepoURL(publicURL, name)) +} diff --git a/pkg/ui/common/utils.go b/pkg/ui/common/utils.go index 08f07ada1e7c32ce4e44b3d605b1a39e5f77036e..dac9ac2f07fb2cd49c2ad92ffe2b0ee03f7c491a 100644 --- a/pkg/ui/common/utils.go +++ b/pkg/ui/common/utils.go @@ -33,8 +33,3 @@ func RepoURL(publicURL, name string) string { return fmt.Sprintf("%s/%s", publicURL, name) } - -// CloneCmd returns the URL of the repository. -var CloneCmd = func(publicURL, name string) string { - return fmt.Sprintf("git clone %s", RepoURL(publicURL, name)) -} diff --git a/pkg/ui/pages/repo/repo.go b/pkg/ui/pages/repo/repo.go index cc4ba58d8fa6c0eeeb2242a522208860a726627c..fb5f0a603c587e2610a86c4a5558246dc011bb2f 100644 --- a/pkg/ui/pages/repo/repo.go +++ b/pkg/ui/pages/repo/repo.go @@ -174,7 +174,7 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } if r.selectedRepo != nil { urlID := fmt.Sprintf("%s-url", r.selectedRepo.Name()) - cmd := common.CloneCmd(r.common.Config().SSH.PublicURL, r.selectedRepo.Name()) + cmd := r.common.CloneCmd(r.common.Config().SSH.PublicURL, r.selectedRepo.Name()) if msg, ok := msg.(tea.MouseMsg); ok && r.common.Zone.Get(urlID).InBounds(msg) { cmds = append(cmds, copyCmd(cmd, "Command copied to clipboard")) } @@ -329,7 +329,7 @@ func (r *Repo) headerView() string { Align(lipgloss.Right) var url string if cfg := r.common.Config(); cfg != nil { - url = common.CloneCmd(cfg.SSH.PublicURL, r.selectedRepo.Name()) + url = r.common.CloneCmd(cfg.SSH.PublicURL, r.selectedRepo.Name()) } url = common.TruncateString(url, r.common.Width-lipgloss.Width(desc)-1) url = r.common.Zone.Mark( diff --git a/pkg/ui/pages/selection/item.go b/pkg/ui/pages/selection/item.go index f90f58d8ce005336248920dcda2e7a3ebf3a987c..4a6209dcf044acb177a135fb970285f6172a691b 100644 --- a/pkg/ui/pages/selection/item.go +++ b/pkg/ui/pages/selection/item.go @@ -11,7 +11,6 @@ import ( "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/charmbracelet/soft-serve/pkg/config" "github.com/charmbracelet/soft-serve/pkg/proto" "github.com/charmbracelet/soft-serve/pkg/ui/common" "github.com/dustin/go-humanize" @@ -54,16 +53,20 @@ type Item struct { } // New creates a new Item. -func NewItem(repo proto.Repository, cfg *config.Config) (Item, error) { +func NewItem(c common.Common, repo proto.Repository) (Item, error) { var lastUpdate *time.Time lu := repo.UpdatedAt() if !lu.IsZero() { lastUpdate = &lu } + var cmd string + if cfg := c.Config(); cfg != nil { + cmd = c.CloneCmd(cfg.SSH.PublicURL, repo.Name()) + } return Item{ repo: repo, lastUpdate: lastUpdate, - cmd: common.CloneCmd(cfg.SSH.PublicURL, repo.Name()), + cmd: cmd, }, nil } diff --git a/pkg/ui/pages/selection/selection.go b/pkg/ui/pages/selection/selection.go index 2638ff16a7e8cd353eb8c871c1b388a613f3c5e2..4b9b72a17ddd5ea0c37e2622f26f53e0b6b08567 100644 --- a/pkg/ui/pages/selection/selection.go +++ b/pkg/ui/pages/selection/selection.go @@ -215,7 +215,7 @@ func (s *Selection) Init() tea.Cmd { } al := be.AccessLevelByPublicKey(ctx, r.Name(), pk) if al >= access.ReadOnlyAccess { - item, err := NewItem(r, cfg) + item, err := NewItem(s.common, r) if err != nil { s.common.Logger.Debugf("ui: failed to create item for %s: %v", r.Name(), err) continue