/
root.go
66 lines (59 loc) · 1.75 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cmd
import (
"fmt"
"io"
"os"
"runtime"
"github.com/exercism/cli/api"
"github.com/exercism/cli/cli"
"github.com/exercism/cli/config"
"github.com/exercism/cli/debug"
"github.com/spf13/cobra"
)
var (
// BinaryName is the name of the app.
// By default this is exercism, but people
// are free to name this however they want.
// The usage examples and help strings should reflect
// the actual name of the binary.
BinaryName string
// Out is used to write to information.
Out io.Writer
// Err is used to write errors.
Err io.Writer
// In is used to provide mocked test input (i.e. for prompts).
In io.Reader
)
// RootCmd represents the base command when called without any subcommands.
var RootCmd = &cobra.Command{
Use: BinaryName,
Short: "A friendly command-line interface to Exercism.",
Long: `A command-line interface for the v2 redesign of Exercism.
Download exercises and submit your solutions.`,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if verbose, _ := cmd.Flags().GetBool("verbose"); verbose {
debug.Verbose = verbose
}
if timeout, _ := cmd.Flags().GetInt("timeout"); timeout > 0 {
cli.TimeoutInSeconds = timeout
api.TimeoutInSeconds = timeout
}
},
}
// Execute adds all child commands to the root command.
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(-1)
}
}
func init() {
BinaryName = os.Args[0]
config.SetDefaultDirName(BinaryName)
Out = os.Stdout
Err = os.Stderr
In = os.Stdin
api.UserAgent = fmt.Sprintf("github.com/exercism/cli v%s (%s/%s)", Version, runtime.GOOS, runtime.GOARCH)
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
RootCmd.PersistentFlags().IntP("timeout", "", 0, "override the default HTTP timeout (seconds)")
}