fix(ui): hide clone command while browse only

Ayman Bagabas created

Change summary

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(-)

Detailed changes

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 (

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))
+}

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))
-}

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(

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
 }
 

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