-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
210 lines (201 loc) · 5.46 KB
/
main.go
File metadata and controls
210 lines (201 loc) · 5.46 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/foliagecp/sdk/clients/go/db"
"github.com/foliagecp/sdk/statefun/system"
"github.com/urfave/cli/v2"
)
var (
NatsURL string = system.GetEnvMustProceed("NATS_URL", "nats://nats:foliage@nats:4222")
NatsHubDomain string = system.GetEnvMustProceed("NATS_HUB_DOMAIN", "hub")
NatsRequestTimeoutSec int = system.GetEnvMustProceed("NATS_REQUEST_TIMEOUT_SEC", 60)
FoliageCLIDir string = system.GetEnvMustProceed("FOLIAGE_CLI_DIR", "~/.foliage-cli")
dbClient db.DBSyncClient
)
func main() {
dbc, err := db.NewDBSyncClient(NatsURL, NatsRequestTimeoutSec, NatsHubDomain)
if err != nil {
log.Fatalln(err)
}
dbClient = dbc
if s, err := expandFileName(FoliageCLIDir); err != nil {
log.Panicln(err)
} else {
FoliageCLIDir = s
}
app := &cli.App{
Name: "foliage-cli",
Usage: "Foliage command line interface",
Commands: []*cli.Command{
{
Name: "gwalk",
Usage: "traverse the graph",
Subcommands: []*cli.Command{
{
Name: "to",
Usage: "walk to specified vertex id",
ArgsUsage: "<id>",
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() != 1 {
return fmt.Errorf("wrong argument amount")
}
if err := gWalkTo(cCtx.Args().First()); err != nil {
return err
}
return nil
},
},
{
Name: "routes",
Usage: "find all existing routes",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "forward_depth",
Aliases: []string{"fd"},
Value: 1,
Usage: "Forward depth",
},
&cli.IntFlag{
Name: "backward_depth",
Aliases: []string{"bd"},
Value: 0,
Usage: "Backward depth",
},
&cli.IntFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: 0,
Usage: "Level of details (0 - just links, 1 - links types, 2 - link types and tags)",
},
},
Action: func(cCtx *cli.Context) error {
return gWalkRoutes(cCtx.Uint("fd"), cCtx.Uint("bd"), cCtx.Int("v"))
},
},
{
Name: "inspect",
Usage: "show detailed info about current location",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "pretty_print",
Aliases: []string{"p"},
Value: false,
Usage: "JSON pretty print",
},
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Value: false,
Usage: "Print all available data",
},
},
Action: func(cCtx *cli.Context) error {
return gWalkInspect(cCtx.Bool("p"), cCtx.Bool("a"))
},
},
{
Name: "query",
Usage: "JPGQL query from current gwalk id",
ArgsUsage: "<JPGQL query>",
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() != 1 {
return fmt.Errorf("wrong argument amount")
}
return gWalkQuery(cCtx.Args().First())
},
},
{
Name: "export",
Usage: "Receive graph in different formats",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Value: "graphml",
Usage: "Use one from the following: dot, graphml, graphml_json2xml. Default: dot",
},
&cli.IntFlag{
Name: "depth",
Aliases: []string{"d"},
Value: -1,
Usage: "Graph depth",
},
&cli.BoolFlag{
Name: "raw",
Aliases: []string{"r"},
Value: false,
Usage: "Raw data only",
},
&cli.StringSliceFlag{
Name: "exclude-vertex",
Aliases: []string{"X"},
Usage: "Exclude vertex body fields (repeatable). Example: -X __meta -X spec.secret",
},
&cli.StringSliceFlag{
Name: "exclude-edge",
Aliases: []string{"x"},
Usage: "Exclude edge body fields (repeatable). Example: -x __meta -x config.token",
},
},
Action: func(cCtx *cli.Context) error {
return gWalkPrintGraph(
cCtx.String("format"),
cCtx.Int("depth"),
cCtx.Bool("raw"),
cCtx.StringSlice("exclude-vertex"),
cCtx.StringSlice("exclude-edge"),
)
},
},
{
Name: "import",
Usage: "Upload graph from different formats",
ArgsUsage: "[filename]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Value: "graphml",
Usage: "Use one from the following: graphml. Default: graphml",
},
&cli.BoolFlag{
Name: "stdin",
Aliases: []string{"s"},
Value: false,
Usage: "Raw data from stdin (\"filename\" argument will be ignored)",
},
},
Action: func(cCtx *cli.Context) error {
var input io.Reader
if cCtx.Bool("s") {
input = os.Stdin
} else {
if cCtx.NArg() != 1 {
return fmt.Errorf("wrong argument amount")
}
f, err := os.Open(cCtx.Args().First())
if err != nil {
return err
}
input = f
defer f.Close()
}
data, err := io.ReadAll(input)
if err != nil {
return err
}
return gWalkImportGraph(cCtx.String("f"), string(data))
},
},
},
},
},
}
if err = app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
// --------------------------------------------------------------------------------------