move the command init to a separate file
This commit is contained in:
parent
093e6d49a2
commit
e8bf1bd033
@ -16,7 +16,6 @@ import (
|
|||||||
"github.com/github/git-lfs/config"
|
"github.com/github/git-lfs/config"
|
||||||
"github.com/github/git-lfs/errors"
|
"github.com/github/git-lfs/errors"
|
||||||
"github.com/github/git-lfs/git"
|
"github.com/github/git-lfs/git"
|
||||||
"github.com/github/git-lfs/httputil"
|
|
||||||
"github.com/github/git-lfs/lfs"
|
"github.com/github/git-lfs/lfs"
|
||||||
"github.com/github/git-lfs/localstorage"
|
"github.com/github/git-lfs/localstorage"
|
||||||
"github.com/github/git-lfs/tools"
|
"github.com/github/git-lfs/tools"
|
||||||
@ -38,6 +37,8 @@ var (
|
|||||||
OutputWriter = io.MultiWriter(os.Stdout, ErrorBuffer)
|
OutputWriter = io.MultiWriter(os.Stdout, ErrorBuffer)
|
||||||
ManPages = make(map[string]string, 20)
|
ManPages = make(map[string]string, 20)
|
||||||
cfg *config.Configuration
|
cfg *config.Configuration
|
||||||
|
|
||||||
|
// Run uses this to initialize the git-lfs command
|
||||||
commandFuncs []func() *cobra.Command
|
commandFuncs []func() *cobra.Command
|
||||||
commandMu sync.Mutex
|
commandMu sync.Mutex
|
||||||
|
|
||||||
@ -45,32 +46,6 @@ var (
|
|||||||
excludeArg string
|
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 {
|
func NewCommand(name string, runFn func(*cobra.Command, []string)) *cobra.Command {
|
||||||
return &cobra.Command{Use: name, Run: runFn, PreRun: resolveLocalStorage}
|
return &cobra.Command{Use: name, Run: runFn, PreRun: resolveLocalStorage}
|
||||||
}
|
}
|
||||||
@ -298,30 +273,6 @@ func determineIncludeExcludePaths(config *config.Configuration, includeArg, excl
|
|||||||
return
|
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 <command>'
|
|
||||||
func help(cmd *cobra.Command, args []string) {
|
|
||||||
if len(args) == 0 {
|
|
||||||
printHelp("git-lfs")
|
|
||||||
} else {
|
|
||||||
printHelp(args[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// usage is used for 'git-lfs <command> --help' or when invoked manually
|
|
||||||
func usage(cmd *cobra.Command) error {
|
|
||||||
printHelp(cmd.Name())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// isCommandEnabled returns whether the environment variable GITLFS<CMD>ENABLED
|
// isCommandEnabled returns whether the environment variable GITLFS<CMD>ENABLED
|
||||||
// is "truthy" according to config.Os.Bool (see
|
// is "truthy" according to config.Os.Bool (see
|
||||||
// github.com/github/git-lfs/config#Configuration.Env.Os), returning false
|
// github.com/github/git-lfs/config#Configuration.Env.Os), returning false
|
||||||
|
58
commands/run.go
Normal file
58
commands/run.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user