Skip to content

Commit 9359dca

Browse files
committed
isolate credits
1 parent bccc93a commit 9359dca

File tree

3 files changed

+122
-95
lines changed

3 files changed

+122
-95
lines changed

command/repo.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8-
func init() {
9-
repoCmd.AddCommand(repoCreditsCmd)
10-
repoCreditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits")
11-
}
12-
138
var repoCmd = &cobra.Command{
149
Use: "repo <command>",
1510
Short: "Create, clone, fork, and view repositories",
@@ -26,28 +21,3 @@ A repository can be supplied as an argument in any of the following formats:
2621
- "OWNER/REPO"
2722
- by URL, e.g. "https://github.com/OWNER/REPO"`},
2823
}
29-
30-
var repoCreditsCmd = &cobra.Command{
31-
Use: "credits [<repository>]",
32-
Short: "View credits for a repository",
33-
Example: heredoc.Doc(`
34-
# view credits for the current repository
35-
$ gh repo credits
36-
37-
# view credits for a specific repository
38-
$ gh repo credits cool/repo
39-
40-
# print a non-animated thank you
41-
$ gh repo credits -s
42-
43-
# pipe to just print the contributors, one per line
44-
$ gh repo credits | cat
45-
`),
46-
Args: cobra.MaximumNArgs(1),
47-
RunE: repoCredits,
48-
Hidden: true,
49-
}
50-
51-
func repoCredits(cmd *cobra.Command, args []string) error {
52-
return credits(cmd, args)
53-
}

command/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
2424
repoCloneCmd "github.com/cli/cli/pkg/cmd/repo/clone"
2525
repoCreateCmd "github.com/cli/cli/pkg/cmd/repo/create"
26+
creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
2627
repoForkCmd "github.com/cli/cli/pkg/cmd/repo/fork"
2728
repoViewCmd "github.com/cli/cli/pkg/cmd/repo/view"
2829
"github.com/cli/cli/pkg/cmdutil"
@@ -156,6 +157,9 @@ func init() {
156157
repoCmd.AddCommand(repoForkCmd.NewCmdFork(&repoResolvingCmdFactory, nil))
157158
repoCmd.AddCommand(repoCloneCmd.NewCmdClone(cmdFactory, nil))
158159
repoCmd.AddCommand(repoCreateCmd.NewCmdCreate(cmdFactory, nil))
160+
repoCmd.AddCommand(creditsCmd.NewCmdRepoCredits(&repoResolvingCmdFactory, nil))
161+
162+
RootCmd.AddCommand(creditsCmd.NewCmdCredits(cmdFactory, nil))
159163
}
160164

161165
// RootCmd is the entry point of command-line execution
Lines changed: 118 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,138 @@
1-
package command
1+
package credits
22

33
import (
44
"bytes"
55
"fmt"
66
"math"
77
"math/rand"
8+
"net/http"
89
"os"
910
"os/exec"
1011
"runtime"
1112
"strings"
1213
"time"
1314

1415
"github.com/MakeNowJust/heredoc"
15-
"github.com/spf13/cobra"
16-
"golang.org/x/crypto/ssh/terminal"
17-
16+
"github.com/cli/cli/api"
17+
"github.com/cli/cli/internal/ghrepo"
18+
"github.com/cli/cli/pkg/cmdutil"
19+
"github.com/cli/cli/pkg/iostreams"
1820
"github.com/cli/cli/utils"
21+
"github.com/spf13/cobra"
1922
)
2023

21-
var thankYou = `
22-
_ _
23-
| | | |
24-
_|_ | | __, _ _ | | __
25-
| |/ \ / | / |/ | |/_) | | / \_| |
26-
|_/| |_/\_/|_/ | |_/| \_/ \_/|/\__/ \_/|_/
27-
/|
28-
\|
29-
_
30-
o | | |
31-
__ __ _ _ _|_ ,_ | | _|_ __ ,_ , |
32-
/ / \_/ |/ | | / | | |/ \_| | | / \_/ | / \_|
33-
\___/\__/ | |_/|_/ |_/|_/\_/ \_/|_/|_/\__/ |_/ \/ o
24+
type CreditsOptions struct {
25+
HttpClient func() (*http.Client, error)
26+
BaseRepo func() (ghrepo.Interface, error)
27+
IO *iostreams.IOStreams
3428

29+
Repository string
30+
Static bool
31+
}
3532

36-
`
33+
func NewCmdCredits(f *cmdutil.Factory, runF func(*CreditsOptions) error) *cobra.Command {
34+
opts := &CreditsOptions{
35+
HttpClient: f.HttpClient,
36+
IO: f.IOStreams,
37+
BaseRepo: f.BaseRepo,
38+
Repository: "cli/cli",
39+
}
3740

38-
func init() {
39-
RootCmd.AddCommand(creditsCmd)
41+
cmd := &cobra.Command{
42+
Use: "credits",
43+
Short: "View credits for this tool",
44+
Long: `View animated credits for gh, the tool you are currently using :)`,
45+
Example: heredoc.Doc(`
46+
# see a credits animation for this project
47+
$ gh credits
48+
49+
# display a non-animated thank you
50+
$ gh credits -s
51+
52+
# just print the contributors, one per line
53+
$ gh credits | cat
54+
`),
55+
Args: cobra.ExactArgs(0),
56+
RunE: func(cmd *cobra.Command, args []string) error {
57+
if runF != nil {
58+
return runF(opts)
59+
}
4060

41-
creditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits")
42-
}
61+
return creditsRun(opts)
62+
},
63+
Hidden: true,
64+
}
65+
66+
cmd.Flags().BoolVarP(&opts.Static, "static", "s", false, "Print a static version of the credits")
4367

44-
var creditsCmd = &cobra.Command{
45-
Use: "credits",
46-
Short: "View credits for this tool",
47-
Long: `View animated credits for gh, the tool you are currently using :)`,
48-
Example: heredoc.Doc(`
49-
# see a credits animation for this project
50-
$ gh credits
51-
52-
# display a non-animated thank you
53-
$ gh credits -s
54-
55-
# just print the contributors, one per line
56-
$ gh credits | cat
57-
`),
58-
Args: cobra.ExactArgs(0),
59-
RunE: ghCredits,
60-
Hidden: true,
68+
return cmd
6169
}
6270

63-
func ghCredits(cmd *cobra.Command, _ []string) error {
64-
args := []string{"cli/cli"}
65-
return credits(cmd, args)
71+
func NewCmdRepoCredits(f *cmdutil.Factory, runF func(*CreditsOptions) error) *cobra.Command {
72+
opts := &CreditsOptions{
73+
HttpClient: f.HttpClient,
74+
BaseRepo: f.BaseRepo,
75+
IO: f.IOStreams,
76+
}
77+
78+
cmd := &cobra.Command{
79+
Use: "credits [<repository>]",
80+
Short: "View credits for a repository",
81+
Example: heredoc.Doc(`
82+
# view credits for the current repository
83+
$ gh repo credits
84+
85+
# view credits for a specific repository
86+
$ gh repo credits cool/repo
87+
88+
# print a non-animated thank you
89+
$ gh repo credits -s
90+
91+
# pipe to just print the contributors, one per line
92+
$ gh repo credits | cat
93+
`),
94+
Args: cobra.MaximumNArgs(1),
95+
RunE: func(cmd *cobra.Command, args []string) error {
96+
if len(args) > 0 {
97+
opts.Repository = args[0]
98+
}
99+
100+
if runF != nil {
101+
return runF(opts)
102+
}
103+
104+
return creditsRun(opts)
105+
},
106+
Hidden: true,
107+
}
108+
109+
cmd.Flags().BoolVarP(&opts.Static, "static", "s", false, "Print a static version of the credits")
110+
111+
return cmd
66112
}
67113

68-
func credits(cmd *cobra.Command, args []string) error {
114+
func creditsRun(opts *CreditsOptions) error {
69115
isWindows := runtime.GOOS == "windows"
70-
ctx := contextForCommand(cmd)
71-
client, err := apiClientForContext(ctx)
116+
httpClient, err := opts.HttpClient()
72117
if err != nil {
73118
return err
74119
}
75120

121+
client := api.NewClientFromHTTP(httpClient)
122+
76123
var owner string
77124
var repo string
78125

79-
if len(args) == 0 {
80-
baseRepo, err := determineBaseRepo(client, cmd, ctx)
126+
if opts.Repository == "" {
127+
baseRepo, err := opts.BaseRepo()
81128
if err != nil {
82129
return err
83130
}
84131

85132
owner = baseRepo.RepoOwner()
86133
repo = baseRepo.RepoName()
87134
} else {
88-
parts := strings.SplitN(args[0], "/", 2)
135+
parts := strings.SplitN(opts.Repository, "/", 2)
89136
owner = parts[0]
90137
repo = parts[1]
91138
}
@@ -105,27 +152,15 @@ func credits(cmd *cobra.Command, args []string) error {
105152
return err
106153
}
107154

108-
out := cmd.OutOrStdout()
109-
isTTY := false
110-
outFile, isFile := out.(*os.File)
111-
if isFile {
112-
isTTY = utils.IsTerminal(outFile)
113-
if isTTY {
114-
// FIXME: duplicates colorableOut
115-
out = utils.NewColorable(outFile)
116-
}
117-
}
155+
isTTY := opts.IO.IsStdoutTTY()
118156

119-
static, err := cmd.Flags().GetBool("static")
120-
if err != nil {
121-
return err
122-
}
157+
static := opts.Static || isWindows
123158

124-
static = static || isWindows
159+
out := opts.IO.Out
125160

126161
if isTTY && static {
127162
fmt.Fprintln(out, "THANK YOU CONTRIBUTORS!!! <3")
128-
fmt.Println()
163+
fmt.Fprintln(out, "")
129164
}
130165

131166
logins := []string{}
@@ -153,7 +188,8 @@ func credits(cmd *cobra.Command, args []string) error {
153188
lines = append(lines, logins...)
154189
lines = append(lines, "( <3 press ctrl-c to quit <3 )")
155190

156-
termWidth, termHeight, err := terminal.GetSize(int(outFile.Fd()))
191+
termWidth, termHeight, err := utils.TerminalSize(out)
192+
//termWidth, termHeight, err := terminal.GetSize(int(outFile.Fd()))
157193
if err != nil {
158194
return err
159195
}
@@ -262,3 +298,20 @@ func clear() {
262298
cmd.Stdout = os.Stdout
263299
_ = cmd.Run()
264300
}
301+
302+
var thankYou = `
303+
_ _
304+
| | | |
305+
_|_ | | __, _ _ | | __
306+
| |/ \ / | / |/ | |/_) | | / \_| |
307+
|_/| |_/\_/|_/ | |_/| \_/ \_/|/\__/ \_/|_/
308+
/|
309+
\|
310+
_
311+
o | | |
312+
__ __ _ _ _|_ ,_ | | _|_ __ ,_ , |
313+
/ / \_/ |/ | | / | | |/ \_| | | / \_/ | / \_|
314+
\___/\__/ | |_/|_/ |_/|_/\_/ \_/|_/|_/\__/ |_/ \/ o
315+
316+
317+
`

0 commit comments

Comments
 (0)