Merge pull request #998 from MichaelMure/fix/996/create-env-once

fix(commands): create env.Env once for all Cobra commands
This commit is contained in:
Michael Muré 2023-01-18 08:21:08 +01:00 committed by GitHub
commit 2484867089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 83 additions and 142 deletions

View File

@ -7,9 +7,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func NewBridgeCommand() *cobra.Command {
env := execenv.NewEnv()
func NewBridgeCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "bridge",
Short: "List bridges to other bug trackers",
@ -20,11 +18,11 @@ func NewBridgeCommand() *cobra.Command {
Args: cobra.NoArgs,
}
cmd.AddCommand(newBridgeAuthCommand())
cmd.AddCommand(newBridgeNewCommand())
cmd.AddCommand(newBridgePullCommand())
cmd.AddCommand(newBridgePushCommand())
cmd.AddCommand(newBridgeRm())
cmd.AddCommand(newBridgeAuthCommand(env))
cmd.AddCommand(newBridgeNewCommand(env))
cmd.AddCommand(newBridgePullCommand(env))
cmd.AddCommand(newBridgePushCommand(env))
cmd.AddCommand(newBridgeRm(env))
return cmd
}

View File

@ -12,9 +12,7 @@ import (
"github.com/MichaelMure/git-bug/util/colors"
)
func newBridgeAuthCommand() *cobra.Command {
env := execenv.NewEnv()
func newBridgeAuthCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "List all known bridge authentication credentials",
@ -25,9 +23,9 @@ func newBridgeAuthCommand() *cobra.Command {
Args: cobra.NoArgs,
}
cmd.AddCommand(newBridgeAuthAddTokenCommand())
cmd.AddCommand(newBridgeAuthRm())
cmd.AddCommand(newBridgeAuthShow())
cmd.AddCommand(newBridgeAuthAddTokenCommand(env))
cmd.AddCommand(newBridgeAuthRm(env))
cmd.AddCommand(newBridgeAuthShow(env))
return cmd
}

View File

@ -24,8 +24,7 @@ type bridgeAuthAddTokenOptions struct {
user string
}
func newBridgeAuthAddTokenCommand() *cobra.Command {
env := execenv.NewEnv()
func newBridgeAuthAddTokenCommand(env *execenv.Env) *cobra.Command {
options := bridgeAuthAddTokenOptions{}
cmd := &cobra.Command{

View File

@ -8,9 +8,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBridgeAuthRm() *cobra.Command {
env := execenv.NewEnv()
func newBridgeAuthRm(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "rm BRIDGE_ID",
Short: "Remove a credential",

View File

@ -13,9 +13,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBridgeAuthShow() *cobra.Command {
env := execenv.NewEnv()
func newBridgeAuthShow(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "Display an authentication credential",

View File

@ -26,8 +26,7 @@ type bridgeNewOptions struct {
nonInteractive bool
}
func newBridgeNewCommand() *cobra.Command {
env := execenv.NewEnv()
func newBridgeNewCommand(env *execenv.Env) *cobra.Command {
options := bridgeNewOptions{}
cmd := &cobra.Command{

View File

@ -23,8 +23,7 @@ type bridgePullOptions struct {
noResume bool
}
func newBridgePullCommand() *cobra.Command {
env := execenv.NewEnv()
func newBridgePullCommand(env *execenv.Env) *cobra.Command {
options := bridgePullOptions{}
cmd := &cobra.Command{

View File

@ -15,9 +15,7 @@ import (
"github.com/MichaelMure/git-bug/util/interrupt"
)
func newBridgePushCommand() *cobra.Command {
env := execenv.NewEnv()
func newBridgePushCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "push [NAME]",
Short: "Push updates to remote bug tracker",

View File

@ -8,9 +8,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBridgeRm() *cobra.Command {
env := execenv.NewEnv()
func newBridgeRm(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "rm NAME",
Short: "Delete a configured bridge",

View File

@ -34,8 +34,7 @@ type bugOptions struct {
outputFormat string
}
func NewBugCommand() *cobra.Command {
env := execenv.NewEnv()
func NewBugCommand(env *execenv.Env) *cobra.Command {
options := bugOptions{}
cmd := &cobra.Command{
@ -107,16 +106,16 @@ git bug status:open --by creation "foo bar" baz
child.GroupID = groupID
}
addCmdWithGroup(newBugDeselectCommand(), selectGroup)
addCmdWithGroup(newBugSelectCommand(), selectGroup)
addCmdWithGroup(newBugDeselectCommand(env), selectGroup)
addCmdWithGroup(newBugSelectCommand(env), selectGroup)
cmd.AddCommand(newBugCommentCommand())
cmd.AddCommand(newBugLabelCommand())
cmd.AddCommand(newBugNewCommand())
cmd.AddCommand(newBugRmCommand())
cmd.AddCommand(newBugShowCommand())
cmd.AddCommand(newBugStatusCommand())
cmd.AddCommand(newBugTitleCommand())
cmd.AddCommand(newBugCommentCommand(env))
cmd.AddCommand(newBugLabelCommand(env))
cmd.AddCommand(newBugNewCommand(env))
cmd.AddCommand(newBugRmCommand(env))
cmd.AddCommand(newBugShowCommand(env))
cmd.AddCommand(newBugStatusCommand(env))
cmd.AddCommand(newBugTitleCommand(env))
return cmd
}

View File

@ -8,9 +8,7 @@ import (
"github.com/MichaelMure/git-bug/util/colors"
)
func newBugCommentCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugCommentCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "comment [BUG_ID]",
Short: "List a bug's comments",
@ -21,8 +19,8 @@ func newBugCommentCommand() *cobra.Command {
ValidArgsFunction: BugCompletion(env),
}
cmd.AddCommand(newBugCommentNewCommand())
cmd.AddCommand(newBugCommentEditCommand())
cmd.AddCommand(newBugCommentNewCommand(env))
cmd.AddCommand(newBugCommentEditCommand(env))
return cmd
}

View File

@ -14,8 +14,7 @@ type bugCommentNewOptions struct {
nonInteractive bool
}
func newBugCommentNewCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugCommentNewCommand(env *execenv.Env) *cobra.Command {
options := bugCommentNewOptions{}
cmd := &cobra.Command{

View File

@ -13,8 +13,7 @@ type bugCommentEditOptions struct {
nonInteractive bool
}
func newBugCommentEditCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugCommentEditCommand(env *execenv.Env) *cobra.Command {
options := bugCommentEditOptions{}
cmd := &cobra.Command{

View File

@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/MichaelMure/git-bug/commands/bug/testenv"
"github.com/MichaelMure/git-bug/commands/cmdtest"
"github.com/MichaelMure/git-bug/commands/execenv"
)

View File

@ -8,9 +8,7 @@ import (
"github.com/MichaelMure/git-bug/entities/bug"
)
func newBugDeselectCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugDeselectCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "deselect",
Short: "Clear the implicitly selected bug",

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBugLabelCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugLabelCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "label [BUG_ID]",
Short: "Display labels of a bug",
@ -19,8 +17,8 @@ func newBugLabelCommand() *cobra.Command {
ValidArgsFunction: BugCompletion(env),
}
cmd.AddCommand(newBugLabelNewCommand())
cmd.AddCommand(newBugLabelRmCommand())
cmd.AddCommand(newBugLabelNewCommand(env))
cmd.AddCommand(newBugLabelRmCommand(env))
return cmd
}

View File

@ -7,9 +7,7 @@ import (
"github.com/MichaelMure/git-bug/util/text"
)
func newBugLabelNewCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugLabelNewCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "new [BUG_ID] LABEL...",
Short: "Add a label to a bug",

View File

@ -7,9 +7,7 @@ import (
"github.com/MichaelMure/git-bug/util/text"
)
func newBugLabelRmCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugLabelRmCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "rm [BUG_ID] LABEL...",
Short: "Remove a label from a bug",

View File

@ -15,8 +15,7 @@ type bugNewOptions struct {
nonInteractive bool
}
func newBugNewCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugNewCommand(env *execenv.Env) *cobra.Command {
options := bugNewOptions{}
cmd := &cobra.Command{

View File

@ -8,9 +8,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBugRmCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugRmCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "rm BUG_ID",
Short: "Remove an existing bug",

View File

@ -15,9 +15,7 @@ func ResolveSelected(repo *cache.RepoCache, args []string) (*cache.BugCache, []s
return _select.Resolve[*cache.BugCache](repo, bug.Typename, bug.Namespace, repo.Bugs(), args)
}
func newBugSelectCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugSelectCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "select BUG_ID",
Short: "Select a bug for implicit use in future commands",

View File

@ -19,8 +19,7 @@ type bugShowOptions struct {
format string
}
func newBugShowCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugShowCommand(env *execenv.Env) *cobra.Command {
options := bugShowOptions{}
cmd := &cobra.Command{

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBugStatusCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugStatusCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "status [BUG_ID]",
Short: "Display the status of a bug",
@ -19,8 +17,8 @@ func newBugStatusCommand() *cobra.Command {
ValidArgsFunction: BugCompletion(env),
}
cmd.AddCommand(newBugStatusCloseCommand())
cmd.AddCommand(newBugStatusOpenCommand())
cmd.AddCommand(newBugStatusCloseCommand(env))
cmd.AddCommand(newBugStatusOpenCommand(env))
return cmd
}

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBugStatusCloseCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugStatusCloseCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "close [BUG_ID]",
Short: "Mark a bug as closed",

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBugStatusOpenCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugStatusOpenCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "open [BUG_ID]",
Short: "Mark a bug as open",

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newBugTitleCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugTitleCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "title [BUG_ID]",
Short: "Display the title of a bug",
@ -19,7 +17,7 @@ func newBugTitleCommand() *cobra.Command {
ValidArgsFunction: BugCompletion(env),
}
cmd.AddCommand(newBugTitleEditCommand())
cmd.AddCommand(newBugTitleEditCommand(env))
return cmd
}

View File

@ -13,8 +13,7 @@ type bugTitleEditOptions struct {
nonInteractive bool
}
func newBugTitleEditCommand() *cobra.Command {
env := execenv.NewEnv()
func newBugTitleEditCommand(env *execenv.Env) *cobra.Command {
options := bugTitleEditOptions{}
cmd := &cobra.Command{

View File

@ -12,8 +12,7 @@ type commandOptions struct {
desc bool
}
func newCommandsCommand() *cobra.Command {
env := execenv.NewEnv()
func newCommandsCommand(env *execenv.Env) *cobra.Command {
options := commandOptions{}
cmd := &cobra.Command{

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newLabelCommand() *cobra.Command {
env := execenv.NewEnv()
func newLabelCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "label",
Short: "List valid labels",

View File

@ -10,9 +10,7 @@ import (
"github.com/MichaelMure/git-bug/entity"
)
func newPullCommand() *cobra.Command {
env := execenv.NewEnv()
func newPullCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "pull [REMOTE]",
Short: "Pull updates from a git remote",

View File

@ -9,9 +9,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newPushCommand() *cobra.Command {
env := execenv.NewEnv()
func newPushCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "push [REMOTE]",
Short: "Push updates to a git remote",

View File

@ -7,10 +7,10 @@ import (
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/commands/bridge"
"github.com/MichaelMure/git-bug/commands/bug"
bridgecmd "github.com/MichaelMure/git-bug/commands/bridge"
bugcmd "github.com/MichaelMure/git-bug/commands/bug"
"github.com/MichaelMure/git-bug/commands/execenv"
"github.com/MichaelMure/git-bug/commands/user"
usercmd "github.com/MichaelMure/git-bug/commands/user"
)
// These variables are initialized externally during the build. See the Makefile.
@ -68,20 +68,22 @@ the same git remote you are already using to collaborate with other people.
child.GroupID = groupID
}
addCmdWithGroup(bugcmd.NewBugCommand(), entityGroup)
addCmdWithGroup(usercmd.NewUserCommand(), entityGroup)
addCmdWithGroup(newLabelCommand(), entityGroup)
env := execenv.NewEnv()
addCmdWithGroup(newTermUICommand(), uiGroup)
addCmdWithGroup(newWebUICommand(), uiGroup)
addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
addCmdWithGroup(newLabelCommand(env), entityGroup)
addCmdWithGroup(newPullCommand(), remoteGroup)
addCmdWithGroup(newPushCommand(), remoteGroup)
addCmdWithGroup(bridgecmd.NewBridgeCommand(), remoteGroup)
addCmdWithGroup(newTermUICommand(env), uiGroup)
addCmdWithGroup(newWebUICommand(env), uiGroup)
cmd.AddCommand(newCommandsCommand())
cmd.AddCommand(newVersionCommand())
cmd.AddCommand(newWipeCommand())
addCmdWithGroup(newPullCommand(env), remoteGroup)
addCmdWithGroup(newPushCommand(env), remoteGroup)
addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup)
cmd.AddCommand(newCommandsCommand(env))
cmd.AddCommand(newVersionCommand(env))
cmd.AddCommand(newWipeCommand(env))
return cmd
}

View File

@ -7,9 +7,7 @@ import (
"github.com/MichaelMure/git-bug/termui"
)
func newTermUICommand() *cobra.Command {
env := execenv.NewEnv()
func newTermUICommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "termui",
Aliases: []string{"tui"},

View File

@ -16,8 +16,7 @@ type userOptions struct {
format string
}
func NewUserCommand() *cobra.Command {
env := execenv.NewEnv()
func NewUserCommand(env *execenv.Env) *cobra.Command {
options := userOptions{}
cmd := &cobra.Command{
@ -29,9 +28,9 @@ func NewUserCommand() *cobra.Command {
}),
}
cmd.AddCommand(newUserNewCommand())
cmd.AddCommand(newUserShowCommand())
cmd.AddCommand(newUserAdoptCommand())
cmd.AddCommand(newUserNewCommand(env))
cmd.AddCommand(newUserShowCommand(env))
cmd.AddCommand(newUserAdoptCommand(env))
flags := cmd.Flags()
flags.SortFlags = false

View File

@ -7,9 +7,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newUserAdoptCommand() *cobra.Command {
env := execenv.NewEnv()
func newUserAdoptCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "adopt USER_ID",
Short: "Adopt an existing identity as your own",

View File

@ -14,9 +14,7 @@ type userNewOptions struct {
nonInteractive bool
}
func newUserNewCommand() *cobra.Command {
env := execenv.NewEnv()
func newUserNewCommand(env *execenv.Env) *cobra.Command {
options := userNewOptions{}
cmd := &cobra.Command{
Use: "new",

View File

@ -16,8 +16,7 @@ type userShowOptions struct {
fields string
}
func newUserShowCommand() *cobra.Command {
env := execenv.NewEnv()
func newUserShowCommand(env *execenv.Env) *cobra.Command {
options := userShowOptions{}
cmd := &cobra.Command{

View File

@ -14,8 +14,7 @@ type versionOptions struct {
all bool
}
func newVersionCommand() *cobra.Command {
env := execenv.NewEnv()
func newVersionCommand(env *execenv.Env) *cobra.Command {
options := versionOptions{}
cmd := &cobra.Command{

View File

@ -42,8 +42,7 @@ type webUIOptions struct {
query string
}
func newWebUICommand() *cobra.Command {
env := execenv.NewEnv()
func newWebUICommand(env *execenv.Env) *cobra.Command {
options := webUIOptions{}
cmd := &cobra.Command{

View File

@ -6,9 +6,7 @@ import (
"github.com/MichaelMure/git-bug/commands/execenv"
)
func newWipeCommand() *cobra.Command {
env := execenv.NewEnv()
func newWipeCommand(env *execenv.Env) *cobra.Command {
cmd := &cobra.Command{
Use: "wipe",
Short: "Wipe git-bug from the git repository",

2
go.mod
View File

@ -114,7 +114,7 @@ require (
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/term v0.4.0 // indirect
golang.org/x/term v0.4.0
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/tools v0.4.0 // indirect
golang.org/x/vuln v0.0.0-20220908155419-5537ad2271a7