From e8bf1bd033ef305ab78788e0bb2ba4c696005dcb Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Thu, 1 Sep 2016 09:21:48 -0600 Subject: [PATCH] move the command init to a separate file --- commands/commands.go | 53 ++-------------------------------------- commands/run.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 51 deletions(-) create mode 100644 commands/run.go diff --git a/commands/commands.go b/commands/commands.go index 1abc106b..452b973d 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -16,7 +16,6 @@ import ( "github.com/github/git-lfs/config" "github.com/github/git-lfs/errors" "github.com/github/git-lfs/git" - "github.com/github/git-lfs/httputil" "github.com/github/git-lfs/lfs" "github.com/github/git-lfs/localstorage" "github.com/github/git-lfs/tools" @@ -38,6 +37,8 @@ var ( OutputWriter = io.MultiWriter(os.Stdout, ErrorBuffer) ManPages = make(map[string]string, 20) cfg *config.Configuration + + // Run uses this to initialize the git-lfs command commandFuncs []func() *cobra.Command commandMu sync.Mutex @@ -45,32 +46,6 @@ var ( excludeArg string ) -func Run() { - cfg = config.Config - - root := &cobra.Command{ - Use: "git-lfs", - Run: func(cmd *cobra.Command, args []string) { - versionCommand(cmd, args) - cmd.Usage() - }, - } - - // Set up help/usage funcs based on manpage text - root.SetHelpFunc(help) - root.SetHelpTemplate("{{.UsageString}}") - root.SetUsageFunc(usage) - - for _, f := range commandFuncs { - if cmd := f(); cmd != nil { - root.AddCommand(cmd) - } - } - - root.Execute() - httputil.LogHttpStats(cfg) -} - func NewCommand(name string, runFn func(*cobra.Command, []string)) *cobra.Command { return &cobra.Command{Use: name, Run: runFn, PreRun: resolveLocalStorage} } @@ -298,30 +273,6 @@ func determineIncludeExcludePaths(config *config.Configuration, includeArg, excl return } -func printHelp(commandName string) { - if txt, ok := ManPages[commandName]; ok { - fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(txt)) - } else { - fmt.Fprintf(os.Stderr, "Sorry, no usage text found for %q\n", commandName) - } -} - -// help is used for 'git-lfs help ' -func help(cmd *cobra.Command, args []string) { - if len(args) == 0 { - printHelp("git-lfs") - } else { - printHelp(args[0]) - } - -} - -// usage is used for 'git-lfs --help' or when invoked manually -func usage(cmd *cobra.Command) error { - printHelp(cmd.Name()) - return nil -} - // isCommandEnabled returns whether the environment variable GITLFSENABLED // is "truthy" according to config.Os.Bool (see // github.com/github/git-lfs/config#Configuration.Env.Os), returning false diff --git a/commands/run.go b/commands/run.go new file mode 100644 index 00000000..3643575b --- /dev/null +++ b/commands/run.go @@ -0,0 +1,58 @@ +package commands + +import ( + "fmt" + "os" + "strings" + + "github.com/github/git-lfs/config" + "github.com/github/git-lfs/httputil" + "github.com/spf13/cobra" +) + +func Run() { + cfg = config.Config + root := NewCommand("git-lfs", gitlfsCommand) + root.PreRun = nil + + // Set up help/usage funcs based on manpage text + root.SetHelpTemplate("{{.UsageString}}") + root.SetHelpFunc(helpCommand) + root.SetUsageFunc(usageCommand) + + for _, f := range commandFuncs { + if cmd := f(); cmd != nil { + root.AddCommand(cmd) + } + } + + root.Execute() + httputil.LogHttpStats(cfg) +} + +func gitlfsCommand(cmd *cobra.Command, args []string) { + versionCommand(cmd, args) + cmd.Usage() +} + +func helpCommand(cmd *cobra.Command, args []string) { + if len(args) == 0 { + printHelp("git-lfs") + } else { + printHelp(args[0]) + } + +} + +func usageCommand(cmd *cobra.Command) error { + printHelp(cmd.Name()) + return nil +} + +func printHelp(commandName string) { + if txt, ok := ManPages[commandName]; ok { + fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(txt)) + } else { + fmt.Fprintf(os.Stderr, "Sorry, no usage text found for %q\n", commandName) + } +}