@@ -5,27 +5,13 @@ import (
55 "os"
66
77 "github.com/github/gh-cli/context"
8- "github.com/spf13/cobra"
9- )
108
11- var (
12- currentRepo string
13- currentBranch string
9+ "github.com/spf13/cobra"
1410)
1511
1612func init () {
17- RootCmd .PersistentFlags ().StringVarP (& currentRepo , "repo" , "R" , "" , "current GitHub repository" )
18- RootCmd .PersistentFlags ().StringVarP (& currentBranch , "current-branch" , "B" , "" , "current git branch" )
19- }
20-
21- func initContext () {
22- ctx := context .InitDefaultContext ()
23- ctx .SetBranch (currentBranch )
24- repo := currentRepo
25- if repo == "" {
26- repo = os .Getenv ("GH_REPO" )
27- }
28- ctx .SetBaseRepo (repo )
13+ RootCmd .PersistentFlags ().StringP ("repo" , "R" , "" , "current GitHub repository" )
14+ RootCmd .PersistentFlags ().StringP ("current-branch" , "B" , "" , "current git branch" )
2915}
3016
3117// RootCmd is the entry point of command-line execution
@@ -34,10 +20,21 @@ var RootCmd = &cobra.Command{
3420 Short : "GitHub CLI" ,
3521 Long : `Do things with GitHub from your terminal` ,
3622 Args : cobra .MinimumNArgs (1 ),
37- PersistentPreRun : func (cmd * cobra.Command , args []string ) {
38- initContext ()
39- },
4023 Run : func (cmd * cobra.Command , args []string ) {
4124 fmt .Println ("root" )
4225 },
4326}
27+
28+ func contextForCommand (cmd * cobra.Command ) context.Context {
29+ ctx := context .New ()
30+ if repo := os .Getenv ("GH_REPO" ); repo != "" {
31+ ctx .SetBaseRepo (repo )
32+ }
33+ if repo , err := cmd .Flags ().GetString ("repo" ); err == nil {
34+ ctx .SetBaseRepo (repo )
35+ }
36+ if branch , err := cmd .Flags ().GetString ("current-branch" ); err == nil {
37+ ctx .SetBranch (branch )
38+ }
39+ return ctx
40+ }
0 commit comments