/
main.go
141 lines (129 loc) · 2.8 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/fission/fission-workflows/pkg/version"
"github.com/fission/fission/fission/plugin"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// This is a prototype of the CLI (and will be integrated into the Fission CLI eventually).
func main() {
app := cli.NewApp()
app.Author = "Erwin van Eyk"
app.Email = "erwin@platform9.com"
app.Version = version.Version
app.EnableBashCompletion = true
app.Usage = "Inspect, manage, and debug workflow executions"
app.Description = app.Usage
app.HideVersion = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url, u",
EnvVar: "FISSION_URL",
Usage: "URL to the Fission apiserver",
},
cli.StringFlag{
Name: "path-prefix",
EnvVar: "FISSION_PATH_PREFIX",
Value: "/proxy/workflows-apiserver",
Usage: "The path to prepend each of the commands",
},
cli.IntFlag{
Name: "verbosity",
Value: 1,
Usage: "CLI verbosity (0 is quiet, 1 is the default, 2 is verbose.)",
EnvVar: "FISSION_VERBOSITY",
},
cli.BoolFlag{
Hidden: true,
Name: "plugin",
Usage: "Show Fission plugin info",
},
}
app.Commands = []cli.Command{
cmdInvoke,
cmdConfig,
cmdStatus,
cmdParse,
cmdWorkflow,
cmdInvocation,
cmdValidate,
cmdVersion,
}
app.Action = func(ctx *cli.Context) error {
if ctx.GlobalBool("plugin") {
bs, err := json.Marshal(plugin.Metadata{
Name: "workflows",
Version: version.Version,
Aliases: []string{"wf"},
Usage: ctx.App.Usage,
})
if err != nil {
panic(err)
}
fmt.Println(string(bs))
return nil
}
cli.ShowAppHelp(ctx)
return nil
}
app.Run(os.Args)
}
func table(writer io.Writer, headings []string, rows [][]string) {
w := tabwriter.NewWriter(writer, 0, 0, 5, ' ', 0)
if headings != nil {
fmt.Fprintln(w, strings.Join(headings, "\t")+"\t")
}
for _, row := range rows {
fmt.Fprintln(w, strings.Join(row, "\t")+"\t")
}
err := w.Flush()
if err != nil {
panic(err)
}
}
func fail(msg ...interface{}) {
for _, line := range msg {
fmt.Fprintln(os.Stderr, line)
}
os.Exit(1)
}
type Context struct {
*cli.Context
}
func (c Context) Deadline() (deadline time.Time, ok bool) {
return
}
func (c Context) Done() <-chan struct{} {
return nil
}
func (c Context) Err() error {
return nil
}
func (c Context) Value(key interface{}) interface{} {
if s, ok := key.(string); ok {
return c.Generic(s)
}
return nil
}
func commandContext(fn func(c Context) error) func(c *cli.Context) error {
return func(c *cli.Context) error {
switch c.GlobalInt("verbosity") {
case 0:
logrus.SetLevel(logrus.ErrorLevel)
case 1:
logrus.SetLevel(logrus.InfoLevel)
default:
fallthrough
case 2:
logrus.SetLevel(logrus.DebugLevel)
}
return fn(Context{c})
}
}