-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (57 loc) · 1.28 KB
/
main.go
File metadata and controls
71 lines (57 loc) · 1.28 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
package main
import (
"encoding/json"
"intelirest-cli/parser"
"intelirest-cli/runtime"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var rootCmd = &cobra.Command{
Use: "rest-cli",
Short: "RFC-2616 compliant request file runner for CLI's",
Args: cobra.MinimumNArgs(1),
RunE: execute,
}
func main() {
f := rootCmd.Flags()
f.StringP("environment", "e", "", "specify environment to run")
f.IntP("maxconns", "M", 4, "maximum number of connections for the client")
f.BoolP("verbose", "v", false, "enable verbose output")
if err := viper.BindPFlags(f); err != nil {
panic(err)
}
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
func execute(_ *cobra.Command, args []string) error {
envName := viper.GetString("environment")
env, err := runtime.ReadEnvironment(envName)
if err != nil {
return err
}
p, err := parser.New(args[0], env)
if err != nil {
return err
}
requests, err := p.Parse()
if err != nil {
return err
}
client := runtime.New(viper.GetInt("maxconns"))
if viper.GetBool("verbose") {
client.SetVerbose()
}
responses, err := client.Do(requests)
if err != nil {
return err
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(responses); err != nil {
return err
}
return nil
}