forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
157 lines (129 loc) · 3.51 KB
/
run.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cli
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
cmds "github.com/ipfs/go-ipfs-cmds"
)
// ExitError is the error used when a specific exit code needs to be returned.
type ExitError int
func (e ExitError) Error() string {
return fmt.Sprintf("exit code %d", int(e))
}
// Closer is a helper interface to check if the env supports closing
type Closer interface {
Close()
}
func Run(ctx context.Context, root *cmds.Command,
cmdline []string, stdin, stdout, stderr *os.File,
buildEnv cmds.MakeEnvironment, makeExecutor cmds.MakeExecutor) error {
printErr := func(err error) {
fmt.Fprintf(stderr, "Error: %s\n", err)
}
req, errParse := Parse(ctx, cmdline[1:], stdin, root)
// Handle the timeout up front.
var cancel func()
if timeoutStr, ok := req.Options[cmds.TimeoutOpt]; ok {
timeout, err := time.ParseDuration(timeoutStr.(string))
if err != nil {
printErr(err)
return err
}
req.Context, cancel = context.WithTimeout(req.Context, timeout)
} else {
req.Context, cancel = context.WithCancel(req.Context)
}
defer cancel()
// this is a message to tell the user how to get the help text
printMetaHelp := func(w io.Writer) {
cmdPath := strings.Join(req.Path, " ")
fmt.Fprintf(w, "Use '%s %s --help' for information about this command\n", cmdline[0], cmdPath)
}
printHelp := func(long bool, w io.Writer) {
helpFunc := ShortHelp
if long {
helpFunc = LongHelp
}
var path []string
if req != nil {
path = req.Path
}
if err := helpFunc(cmdline[0], root, path, w); err != nil {
// This should not happen
panic(err)
}
}
// BEFORE handling the parse error, if we have enough information
// AND the user requested help, print it out and exit
err := HandleHelp(cmdline[0], req, stdout)
if err == nil {
return nil
} else if err != ErrNoHelpRequested {
return err
}
// no help requested, continue.
// ok now handle parse error (which means cli input was wrong,
// e.g. incorrect number of args, or nonexistent subcommand)
if errParse != nil {
printErr(errParse)
// this was a user error, print help
if req != nil && req.Command != nil {
fmt.Fprintln(stderr) // i need some space
printHelp(false, stderr)
}
return errParse
}
// here we handle the cases where
// - commands with no Run func are invoked directly.
// - the main command is invoked.
if req == nil || req.Command == nil || req.Command.Run == nil {
printHelp(false, stdout)
return nil
}
cmd := req.Command
env, err := buildEnv(req.Context, req)
if err != nil {
printErr(err)
return err
}
if c, ok := env.(Closer); ok {
defer c.Close()
}
exctr, err := makeExecutor(req, env)
if err != nil {
printErr(err)
return err
}
encTypeStr, _ := req.Options[cmds.EncLong].(string)
encType := cmds.EncodingType(encTypeStr)
// use JSON if text was requested but the command doesn't have a text-encoder
if _, ok := cmd.Encoders[encType]; encType == cmds.Text && !ok {
req.Options[cmds.EncLong] = cmds.JSON
}
re, err := NewResponseEmitter(stdout, stderr, req)
if err != nil {
printErr(err)
return err
}
// Execute the command.
err = exctr.Execute(req, re, env)
// If we get an error here, don't bother reading the status from the
// response emitter. It may not even be closed.
if err != nil {
printErr(err)
if kiterr, ok := err.(*cmds.Error); ok {
err = *kiterr
}
if kiterr, ok := err.(cmds.Error); ok && kiterr.Code == cmds.ErrClient {
printMetaHelp(stderr)
}
return err
}
if code := re.Status(); code != 0 {
return ExitError(code)
}
return nil
}