Skip to content

Commit

Permalink
fix(ui): hide clone command while browse only
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 5, 2023
1 parent 812d840 commit 0a38578
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
7 changes: 1 addition & 6 deletions cmd/soft/browse/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 (
Expand Down
10 changes: 10 additions & 0 deletions pkg/ui/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"context"
"fmt"

"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/git"
Expand Down Expand Up @@ -33,6 +34,7 @@ type Common struct {
Zone *zone.Manager
Output *termenv.Output
Logger *log.Logger
HideCloneCmd bool
}

// NewCommon returns a new Common struct.
Expand Down Expand Up @@ -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))
}
5 changes: 0 additions & 5 deletions pkg/ui/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions pkg/ui/pages/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions pkg/ui/pages/selection/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/pages/selection/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a38578

Please sign in to comment.