Skip to content

Commit 8370602

Browse files
committed
WIP eliminate package-level state in commands, context
1 parent 641de86 commit 8370602

File tree

4 files changed

+26
-45
lines changed

4 files changed

+26
-45
lines changed

command/pr.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strconv"
66

77
"github.com/github/gh-cli/api"
8-
"github.com/github/gh-cli/context"
98
"github.com/github/gh-cli/utils"
109
"github.com/spf13/cobra"
1110
)
@@ -38,6 +37,7 @@ work with pull requests.`,
3837
}
3938

4039
func prList(cmd *cobra.Command, args []string) error {
40+
ctx := contextForCommand(cmd)
4141
prPayload, err := api.PullRequests()
4242
if err != nil {
4343
return err
@@ -47,7 +47,7 @@ func prList(cmd *cobra.Command, args []string) error {
4747
if prPayload.CurrentPR != nil {
4848
printPrs(*prPayload.CurrentPR)
4949
} else {
50-
currentBranch, err := context.Current().Branch()
50+
currentBranch, err := ctx.Branch()
5151
if err != nil {
5252
return err
5353
}
@@ -76,7 +76,8 @@ func prList(cmd *cobra.Command, args []string) error {
7676
}
7777

7878
func prView(cmd *cobra.Command, args []string) error {
79-
baseRepo, err := context.Current().BaseRepo()
79+
ctx := contextForCommand(cmd)
80+
baseRepo, err := ctx.BaseRepo()
8081
if err != nil {
8182
return err
8283
}
@@ -94,7 +95,7 @@ func prView(cmd *cobra.Command, args []string) error {
9495
if err != nil {
9596
return err
9697
} else if prPayload.CurrentPR == nil {
97-
branch, err := context.Current().Branch()
98+
branch, err := ctx.Branch()
9899
if err != nil {
99100
return err
100101
}

command/root.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1612
func 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+
}

context/blank_context.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ import (
55
"strings"
66
)
77

8-
// InitBlankContext initializes a blank context for testing
9-
func InitBlankContext() Context {
10-
currentContext = &blankContext{
11-
authToken: "OTOKEN",
12-
authLogin: "monalisa",
13-
}
14-
return currentContext
8+
func NewBlank() Context {
9+
return &blankContext{}
1510
}
1611

1712
// A Context implementation that queries the filesystem

context/context.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,8 @@ type Context interface {
1919
SetBaseRepo(string)
2020
}
2121

22-
var currentContext Context
23-
24-
// Current returns the currently initialized Context instance
25-
func Current() Context {
26-
return currentContext
27-
}
28-
29-
// InitDefaultContext initializes the default filesystem context
30-
func InitDefaultContext() Context {
31-
ctx := &fsContext{}
32-
if currentContext == nil {
33-
currentContext = ctx
34-
}
35-
return ctx
22+
func New() Context {
23+
return &blankContext{}
3624
}
3725

3826
// A Context implementation that queries the filesystem

0 commit comments

Comments
 (0)