Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit bdc5c27

Browse files
committed
Stop using global variables
1 parent 23e3ac5 commit bdc5c27

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

cmd/root.go

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,69 @@ package cmd
33
import (
44
"context"
55

6-
"github.com/google/go-github/github"
76
"github.com/hellofresh/github-cli/pkg/config"
8-
"github.com/hellofresh/github-cli/pkg/formatter"
9-
"github.com/pkg/errors"
10-
log "github.com/sirupsen/logrus"
7+
"github.com/hellofresh/github-cli/pkg/github"
8+
"github.com/hellofresh/github-cli/pkg/log"
9+
"github.com/sirupsen/logrus"
1110
"github.com/spf13/cobra"
12-
"golang.org/x/oauth2"
1311
)
1412

15-
var (
16-
cfgFile string
17-
globalConfig *config.Spec
18-
githubClient *github.Client
19-
token string
20-
verbose bool
13+
type (
14+
// RootOptions represents the ahoy global options
15+
RootOptions struct {
16+
configFile string
17+
token string
18+
org string
19+
verbose bool
20+
}
21+
)
2122

22-
// RootCmd is our main command
23-
RootCmd = &cobra.Command{
23+
// NewRootCmd creates the root command
24+
func NewRootCmd() *cobra.Command {
25+
opts := RootOptions{}
26+
27+
cmd := cobra.Command{
2428
Use: "github-cli [--config] [--token]",
2529
Short: "HF Github is a cli tool to manage your github repositories",
26-
Long: `A simple CLI tool to help you manage your github repositories.
27-
Complete documentation is available at http://github.com/hellofresh/github-cli`,
30+
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
31+
if opts.verbose {
32+
log.WithContext(context.Background()).SetLevel(logrus.DebugLevel)
33+
}
34+
},
2835
}
29-
)
3036

31-
func init() {
32-
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.github.toml)")
33-
RootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "optional, github token for authentication (default in $HOME/.github.toml)")
34-
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Make the operation more talkative")
37+
cmd.PersistentFlags().StringVarP(&opts.configFile, "config", "c", "", "config file (default is $HOME/.github.toml)")
38+
cmd.PersistentFlags().StringVarP(&opts.token, "token", "t", "", "optional, github token for authentication (default in $HOME/.github.toml)")
39+
cmd.PersistentFlags().BoolVarP(&opts.verbose, "verbose", "v", false, "Make the operation more talkative")
40+
cmd.PersistentFlags().StringVarP(&opts.org, "organization", "o", "", "Github's organization")
3541

36-
// Aggregates Root commands
37-
RootCmd.AddCommand(NewRepoCmd())
38-
RootCmd.AddCommand(NewHiringCmd())
39-
RootCmd.AddCommand(NewVersionCmd())
40-
41-
log.SetFormatter(&formatter.CliFormatter{})
42-
}
43-
44-
func setupConnection(cmd *cobra.Command, args []string) error {
45-
var err error
42+
ctx := log.NewContext(context.Background())
43+
ctx, err := config.NewContext(ctx, opts.configFile)
44+
if err != nil {
45+
log.WithContext(ctx).WithError(err).Error("Could not load configuration file")
46+
}
4647

47-
if verbose {
48-
log.SetLevel(log.DebugLevel)
48+
cfg := config.WithContext(ctx)
49+
if opts.token != "" {
50+
cfg.Github.Token = opts.token
51+
cfg.GithubTestOrg.Token = opts.token
4952
}
5053

51-
globalConfig, err = config.Load(cfgFile)
52-
if err != nil {
53-
return errors.Wrap(err, "Could not load the configurations")
54+
if opts.org != "" {
55+
cfg.Github.Organization = opts.org
56+
cfg.GithubTestOrg.Organization = opts.org
5457
}
58+
ctx = config.OverrideConfig(ctx, cfg)
5559

56-
if token != "" {
57-
globalConfig.Github.Token = token
58-
globalConfig.GithubTestOrg.Token = token
60+
ctx, err = github.NewContext(ctx, cfg.Github.Token)
61+
if err != nil {
62+
log.WithContext(ctx).WithError(err).Fatal("Could not create the kube client")
5963
}
6064

61-
ts := oauth2.StaticTokenSource(
62-
&oauth2.Token{AccessToken: globalConfig.Github.Token},
63-
)
64-
tc := oauth2.NewClient(context.Background(), ts)
65-
githubClient = github.NewClient(tc)
65+
// Aggregates Root commands
66+
cmd.AddCommand(NewRepoCmd(ctx))
67+
cmd.AddCommand(NewHiringCmd(ctx))
68+
cmd.AddCommand(NewVersionCmd(ctx))
6669

67-
return nil
70+
return &cmd
6871
}

0 commit comments

Comments
 (0)