forked from parse-community/parse-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_helpers.go
115 lines (107 loc) · 2.74 KB
/
run_helpers.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
package main
import (
"fmt"
"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)
}
}
}
// runWithAppClient wraps a run function that should get an app
func runWithAppClient(e *env, f func(*env, *context) error) cobraRun {
return func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(e.Err, "please provide an app name\n\n")
cmd.Help()
e.Exit(1)
}
if len(args) > 1 {
fmt.Fprintf(
e.Err,
"unexpected arguments, only an app name is expected:%+v\n\n",
args,
)
cmd.Help()
e.Exit(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)
}
}
}
// 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)
}
}
}