-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
121 lines (107 loc) · 2.26 KB
/
utils.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
package tester
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/shivuslr41/grpc-tester/jq"
)
// file to save extracted data
const file = "variables.json"
// variables store replacable/extarcted data
var variables = make(map[string]any)
// tlsFlag sets grpcurl tls configuration
func (l *Lister) tlsFlag() string {
if l.TLS {
return ""
}
return "--plaintext"
}
// protoFlag sets grpcurl proto files and path configuration
func (l *Lister) protoFlag() string {
if l.ProtoPath != "" {
return fmt.Sprintf(
"--import-path %s --proto %s",
l.ProtoPath,
l.ProtoFile,
)
}
return ""
}
// replaceGconf replaces file configs from global -G configs if provided
func (r *Runner) replaceGconf() {
if GConf.Server != "" {
r.Server = GConf.Server
}
if GConf.Endpoint != "" {
r.Endpoint = GConf.Endpoint
}
if GConf.ProtoPath != "" {
r.ProtoPath = GConf.ProtoPath
}
if GConf.ProtoFile != "" {
r.ProtoFile = GConf.ProtoFile
}
r.StreamPayload = GConf.StreamPayload
r.TLS = GConf.TLS
}
// removeEmptyStrings removes empty vals from slice
func removeEmptyStrings(s []string) []string {
var ss []string
for i := range s {
if s[i] != "" {
ss = append(ss, s[i])
}
}
return ss
}
// readStdErr reads stderr from pipe
func readStdErr(rc io.ReadCloser) error {
b, err := io.ReadAll(rc)
if err != nil {
return err
}
if len(b) != 0 {
return fmt.Errorf("%s", string(b))
}
return nil
}
// printErrAndExit prints error and exits
func printErrAndExit(err error) {
fmt.Print(err)
os.Exit(1)
}
// format JSON string into "jq" format
func (t *T) format(b []byte) error {
str, err := jq.Format(string(b))
if err != nil {
return err
}
return json.Unmarshal([]byte(str), &t.Response)
}
// load extracted data from variables.json file to variables map
func load() error {
b, err := os.ReadFile(file)
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
return nil
}
return err
}
return json.Unmarshal(b, &variables)
}
// save extracted result data to variables.json file from variables map
func save() error {
b, err := json.MarshalIndent(variables, "", " ")
if err != nil {
return err
}
return os.WriteFile(file, b, 0644)
}
// print debug logs
func print(out ...any) {
if Debug {
fmt.Println(out...)
}
}