-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
113 lines (90 loc) · 1.97 KB
/
cli.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cli
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
)
var (
buildCommit string
buildDate string
)
// CLI represents a command-line interface. This class is
// not threadsafe.
type CLI struct {
Name string
rootCmd *cobra.Command
version string
rpcURL string
config string
logfile string
host string
user string
database string
password string
}
// NewCLI returns an initialized CLI
func NewCLI() *CLI {
version := "v0.2.0"
if buildCommit != "" {
version = fmt.Sprintf("%v-%v", version, buildCommit)
}
if buildDate != "" {
version = fmt.Sprintf("%v-%v", version, buildDate)
}
cli := &CLI{
Name: "NewChainDump",
rootCmd: nil,
version: version,
rpcURL: "",
config: "",
}
cli.buildRootCmd()
return cli
}
// Execute parses the command line and processes it.
func (cli *CLI) Execute() {
cli.rootCmd.Execute()
}
// setup turns up the CLI environment, and gets called by Cobra before
// a command is executed.
func (cli *CLI) setup(cmd *cobra.Command, args []string) {
err := setupConfig(cli)
if err != nil {
fmt.Println(err)
fmt.Fprint(os.Stderr, cmd.UsageString())
os.Exit(1)
}
}
func (cli *CLI) help(cmd *cobra.Command, args []string) {
fmt.Fprint(os.Stderr, cmd.UsageString())
os.Exit(-1)
}
// TestCommand test command
func (cli *CLI) TestCommand(command string) string {
//cli.testing = true
result := cli.Run(strings.Fields(command)...)
// cli.testing = false
return result
}
// Run executes CLI with the given arguments. Used for testing. Not thread safe.
func (cli *CLI) Run(args ...string) string {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
cli.rootCmd.SetArgs(args)
cli.rootCmd.Execute()
cli.buildRootCmd()
w.Close()
os.Stdout = oldStdout
var stdOut bytes.Buffer
io.Copy(&stdOut, r)
return stdOut.String()
}
// Embeddable returns a CLI that you can embed into your own Go programs. This
// is not thread-safe.
func (cli *CLI) Embeddable() *CLI {
return cli
}