1- package command
1+ package credits
22
33import (
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