Skip to content

Commit d70c988

Browse files
committed
Do not use init func
1 parent 5fc6940 commit d70c988

File tree

9 files changed

+73
-91
lines changed

9 files changed

+73
-91
lines changed

cmd/completion.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ __git-ghost_custom_func() {
5858
`
5959
)
6060

61-
func init() {
62-
RootCmd.AddCommand(completionCmd)
63-
}
64-
65-
var completionCmd = &cobra.Command{
66-
Use: "completion SHELL",
67-
Short: "output shell completion code for the specified shell (bash or zsh)",
68-
Long: `Write bash or zsh shell completion code to standard output.
61+
func NewCompletionCmd(rootCmd *cobra.Command) *cobra.Command {
62+
return &cobra.Command{
63+
Use: "completion SHELL",
64+
Short: "output shell completion code for the specified shell (bash or zsh)",
65+
Long: `Write bash or zsh shell completion code to standard output.
6966
7067
For bash, ensure you have bash completions installed and enabled.
7168
To access completions in your current shell, run
@@ -75,21 +72,22 @@ var completionCmd = &cobra.Command{
7572
For zsh, output to a file in a directory referenced by the $fpath shell
7673
variable.
7774
`,
78-
Args: cobra.ExactArgs(1),
79-
Run: func(cmd *cobra.Command, args []string) {
80-
shell := args[0]
81-
RootCmd.BashCompletionFunction = bashCompletionFunc
82-
availableCompletions := map[string]func(io.Writer) error{
83-
"bash": RootCmd.GenBashCompletion,
84-
"zsh": RootCmd.GenZshCompletion,
85-
}
86-
completion, ok := availableCompletions[shell]
87-
if !ok {
88-
fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell)
89-
os.Exit(1)
90-
}
91-
if err := completion(os.Stdout); err != nil {
92-
log.Fatal(err)
93-
}
94-
},
75+
Args: cobra.ExactArgs(1),
76+
Run: func(cmd *cobra.Command, args []string) {
77+
shell := args[0]
78+
rootCmd.BashCompletionFunction = bashCompletionFunc
79+
availableCompletions := map[string]func(io.Writer) error{
80+
"bash": rootCmd.GenBashCompletion,
81+
"zsh": rootCmd.GenZshCompletion,
82+
}
83+
completion, ok := availableCompletions[shell]
84+
if !ok {
85+
fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell)
86+
os.Exit(1)
87+
}
88+
if err := completion(os.Stdout); err != nil {
89+
log.Fatal(err)
90+
}
91+
},
92+
}
9593
}

cmd/delete.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28-
func init() {
29-
RootCmd.AddCommand(NewDeleteCommand())
30-
}
31-
3228
type deleteFlags struct {
3329
hashFrom string
3430
hashTo string

cmd/gc.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import (
2020
"github.com/spf13/cobra"
2121
)
2222

23-
func init() {
24-
RootCmd.AddCommand(gcCmd)
25-
}
26-
2723
var gcCmd = &cobra.Command{
2824
Use: "gc",
2925
Short: "gc ghost commits from remote repository.",

cmd/list.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import (
2727
"github.com/spf13/cobra"
2828
)
2929

30-
func init() {
31-
RootCmd.AddCommand(NewListCommand())
32-
}
33-
3430
var outputTypes = []string{"only-from", "only-to"}
3531
var regexpOutputPattern = regexp.MustCompile("^(|" + strings.Join(outputTypes, "|") + ")$")
3632

cmd/pull.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28-
func init() {
29-
RootCmd.AddCommand(NewPullCommand())
30-
}
31-
3228
type pullFlags struct {
3329
// forceApply bool
3430
}

cmd/push.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ type pushFlags struct {
3030
followSymlinks bool
3131
}
3232

33-
func init() {
34-
RootCmd.AddCommand(NewPushCommand())
35-
}
36-
3733
func NewPushCommand() *cobra.Command {
3834
var (
3935
flags pushFlags

cmd/root.go

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,57 @@ var (
5555
Revision string
5656
)
5757

58-
var RootCmd = &cobra.Command{
59-
Use: "git-ghost",
60-
Short: "git-ghost",
61-
SilenceErrors: false,
62-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
63-
if cmd.Use == "version" {
64-
return nil
65-
}
66-
err := validateEnvironment()
67-
if err != nil {
68-
return err
69-
}
70-
err = globalOpts.SetDefaults()
71-
if err != nil {
72-
return err
73-
}
74-
err = globalOpts.Validate()
75-
if err != nil {
76-
return err
77-
}
78-
switch globalOpts.verbose {
79-
case 0:
80-
log.SetLevel(log.ErrorLevel)
81-
case 1:
82-
log.SetLevel(log.InfoLevel)
83-
case 2:
84-
log.SetLevel(log.DebugLevel)
85-
case 3:
86-
log.SetLevel(log.TraceLevel)
87-
}
88-
return nil
89-
},
90-
}
91-
9258
var globalOpts globalFlags
9359

94-
func init() {
60+
func NewRootCmd() *cobra.Command {
9561
cobra.OnInitialize()
96-
RootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)")
97-
RootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)")
98-
RootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)")
99-
RootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)")
100-
RootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode")
101-
RootCmd.AddCommand(versionCmd)
62+
rootCmd := &cobra.Command{
63+
Use: "git-ghost",
64+
Short: "git-ghost",
65+
SilenceErrors: false,
66+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
67+
if cmd.Use == "version" {
68+
return nil
69+
}
70+
err := validateEnvironment()
71+
if err != nil {
72+
return err
73+
}
74+
err = globalOpts.SetDefaults()
75+
if err != nil {
76+
return err
77+
}
78+
err = globalOpts.Validate()
79+
if err != nil {
80+
return err
81+
}
82+
switch globalOpts.verbose {
83+
case 0:
84+
log.SetLevel(log.ErrorLevel)
85+
case 1:
86+
log.SetLevel(log.InfoLevel)
87+
case 2:
88+
log.SetLevel(log.DebugLevel)
89+
case 3:
90+
log.SetLevel(log.TraceLevel)
91+
}
92+
return nil
93+
},
94+
}
95+
rootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)")
96+
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)")
97+
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)")
98+
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)")
99+
rootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode")
100+
rootCmd.AddCommand(NewPushCommand())
101+
rootCmd.AddCommand(NewPullCommand())
102+
rootCmd.AddCommand(NewShowCommand())
103+
rootCmd.AddCommand(NewListCommand())
104+
rootCmd.AddCommand(NewDeleteCommand())
105+
rootCmd.AddCommand(gcCmd)
106+
rootCmd.AddCommand(versionCmd)
107+
rootCmd.AddCommand(NewCompletionCmd(rootCmd))
108+
return rootCmd
102109
}
103110

104111
var versionCmd = &cobra.Command{

cmd/show.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28-
func init() {
29-
RootCmd.AddCommand(NewShowCommand())
30-
}
31-
3228
func NewShowCommand() *cobra.Command {
3329
command := &cobra.Command{
3430
Use: "show [from-hash(default=HEAD)] [diff-hash]",

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func init() {
2828

2929
func main() {
3030
// RootCmd prints errors if exists
31-
if err := cmd.RootCmd.Execute(); err != nil {
31+
rootCmd := cmd.NewRootCmd()
32+
if err := rootCmd.Execute(); err != nil {
3233
os.Exit(-1)
3334
}
3435
}

0 commit comments

Comments
 (0)