/
requests.go
57 lines (49 loc) · 1.29 KB
/
requests.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
package cmd
import (
"fmt"
termtables "github.com/brettski/go-termtables"
"github.com/urfave/cli/v2"
"github.com/michaeldbianchi/yahr/core"
)
func printRequestList(cCtx *cli.Context, requests []*core.RequestConfig) {
table := termtables.CreateTable()
table.AddHeaders("Group", "Name", "Method", "Endpoint")
for _, req := range requests {
table.AddRow(req.GroupName, req.Name, req.Method, req.EndpointUrl())
}
// fmt.Println(table.Render())
fmt.Fprintf(cCtx.App.Writer, table.Render())
}
var RequestCmd = &cli.Command{
Name: "requests",
Aliases: []string{"req"},
Usage: "A set of commands to work with requests",
Subcommands: []*cli.Command{
requestListCmd,
},
}
var requestListCmd = &cli.Command{
Name: "list",
Aliases: []string{"l"},
Usage: "List all requests, optionally limited to group of requests",
ArgsUsage: "[GROUP]",
Action: func(cCtx *cli.Context) error {
var requests []*core.RequestConfig
var err error
if cCtx.NArg() < 1 {
requests = core.FetchRequestConfigs()
} else {
group := cCtx.Args().First()
requests = core.FetchRequestConfigsByGroup(group)
}
if err != nil {
return err
}
if len(requests) < 1 {
fmt.Fprintln(cCtx.App.Writer, "No requests found")
return nil
}
printRequestList(cCtx, requests)
return nil
},
}