forked from parse-community/parse-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunners.go
More file actions
136 lines (126 loc) · 3.26 KB
/
runners.go
File metadata and controls
136 lines (126 loc) · 3.26 KB
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
package parsecli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
)
type cobraRun func(cmd *cobra.Command, args []string)
// RunNoArgs wraps a run function that shouldn't get any arguments.
func RunNoArgs(e *Env, f func(*Env) error) cobraRun {
return func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
fmt.Fprintf(e.Err, "unexpected arguments:%+v\n\n", args)
cmd.Help()
e.Exit(1)
}
if err := f(e); err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
}
}
// RunWithArgs wraps a run function that can access arguments to cobraCmd
func RunWithArgs(e *Env, f func(*Env, []string) error) cobraRun {
return func(cmd *cobra.Command, args []string) {
if err := f(e, args); err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
}
}
// RunWithClient wraps a run function that should get an app, when the default is
// picked from the config in the current working directory.
func RunWithClient(e *Env, f func(*Env, *Context) error) cobraRun {
return func(cmd *cobra.Command, args []string) {
app := DefaultKey
if len(args) > 1 {
fmt.Fprintf(
e.Err,
"unexpected arguments, only an optional app name is expected:%+v\n\n",
args,
)
cmd.Help()
e.Exit(1)
}
if len(args) == 1 {
app = args[0]
}
cl, err := newContext(e, app)
if err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
if err := f(e, cl); err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
}
}
// RunWithClientConfirm wraps a run function that takes an app name
// or asks for confirmation to use the default app name instead
func RunWithClientConfirm(e *Env, f func(*Env, *Context) error) cobraRun {
return func(cmd *cobra.Command, args []string) {
app := DefaultKey
if len(args) > 1 {
fmt.Fprintf(
e.Err,
"unexpected arguments, only an optional app name is expected:%+v\n\n",
args,
)
cmd.Help()
e.Exit(1)
}
if len(args) == 1 {
app = args[0]
}
cl, err := newContext(e, app)
if err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
if app == DefaultKey {
fmt.Fprintf(
e.Out,
`Did not provide app name as an argument to the command.
Please enter an app name to execute this command on
or press ENTER to use the default app %q: `,
cl.Config.GetDefaultApp(),
)
var appName string
fmt.Fscanf(e.In, "%s\n", &appName)
appName = strings.TrimSpace(appName)
if appName != "" {
cl, err = newContext(e, appName)
if err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
}
}
if err := f(e, cl); err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
}
}
// RunWithArgsClient wraps a run function that should get an app, whee the default is
// picked from the config in the current working directory. It also passes args to the
// runner function
func RunWithArgsClient(e *Env, f func(*Env, *Context, []string) error) cobraRun {
return func(cmd *cobra.Command, args []string) {
app := DefaultKey
if len(args) > 1 {
app = args[0]
args = args[1:]
}
cl, err := newContext(e, app)
if err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
if err := f(e, cl, args); err != nil {
fmt.Fprintln(e.Err, ErrorString(e, err))
e.Exit(1)
}
}
}