Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
oops
  • Loading branch information
vilmibm committed Aug 25, 2020
commit 09de5cd096e1b399fd7036fb831aff774a757b2f
90 changes: 58 additions & 32 deletions pkg/cmd/repo/garden.go → pkg/cmd/repo/garden/garden.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package command
package garden

import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
)

type Geometry struct {
Expand Down Expand Up @@ -80,54 +83,77 @@ func (p *Player) move(direction Direction) {
}
}

func init() {
repoCmd.AddCommand(repoGardenCmd)
}
type GardenOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)

var repoGardenCmd = &cobra.Command{
Use: "garden",
Short: "Wander around a repository as a garden",
Long: "Use WASD or vi keys to move and q to quit.",
RunE: repoGarden,
RepoArg string
}

func repoGarden(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
client, err := apiClientForContext(ctx)
if err != nil {
return err
func NewCmdGarden(f *cmdutil.Factory, runF func(*GardenOptions) error) *cobra.Command {
opts := GardenOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
BaseRepo: f.BaseRepo,
}

out := colorableOut(cmd)

isTTY := false
outFile, isFile := out.(*os.File)
if isFile {
isTTY = utils.IsTerminal(outFile)
if isTTY {
// FIXME: duplicates colorableOut
out = utils.NewColorable(outFile)
}
cmd := &cobra.Command{
Use: "garden [<repository>]",
Short: "Explore a git repository as a garden",
Long: "Use WASD or vi keys to move. q to quit.",
RunE: func(c *cobra.Command, args []string) error {
if len(args) > 0 {
opts.RepoArg = args[0]
}
if runF != nil {
return runF(&opts)
}
return gardenRun(&opts)
},
}
}

func gardenRun(opts *GardenOptions) error {
out := opts.IO.Out

if !isTTY {
if opts.IO.IsStdoutTTY() {
return errors.New("must be connected to a terminal")
}

var baseRepo ghrepo.Interface
if len(args) > 0 {
baseRepo, err = ghrepo.FromFullName(args[0])
var toView ghrepo.Interface
apiClient := api.NewClientFromHTTP(httpClient)
if opts.RepoArg == "" {
var err error
toView, err = opts.BaseRepo()
if err != nil {
return err
}
} else {
baseRepo, err = determineBaseRepo(client, cmd, ctx)
var err error
viewURL := opts.RepoArg
if !strings.Contains(viewURL, "/") {
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
viewURL = currentUser + "/" + viewURL
}
toView, err = ghrepo.FromFullName(viewURL)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
}

repo, err := api.GitHubRepo(apiClient, toView)
if err != nil {
return err
}

seed := computeSeed(ghrepo.FullName(baseRepo))
seed := computeSeed(ghrepo.FullName(repo))
rand.Seed(seed)

termWidth, termHeight, err := terminal.GetSize(int(outFile.Fd()))
termWidth, termHeight, err := utils.TerminalSize(out)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
repoCreateCmd "github.com/cli/cli/pkg/cmd/repo/create"
creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
repoForkCmd "github.com/cli/cli/pkg/cmd/repo/fork"
gardenCmd "github.com/cli/cli/pkg/cmd/repo/garden"
repoViewCmd "github.com/cli/cli/pkg/cmd/repo/view"
"github.com/cli/cli/pkg/cmdutil"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,6 +37,7 @@ func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(repoCloneCmd.NewCmdClone(f, nil))
cmd.AddCommand(repoCreateCmd.NewCmdCreate(f, nil))
cmd.AddCommand(creditsCmd.NewCmdRepoCredits(f, nil))
cmd.AddCommand(gardenCmd.NewCmdGarden(f, nil))

return cmd
}