-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.go
50 lines (43 loc) · 1023 Bytes
/
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
package cli
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
)
func parseFile(filePath string, v protoreflect.ProtoMessage) error {
b, err := os.ReadFile(filePath)
if err != nil {
return err
}
switch filepath.Ext(filePath) {
case ".json":
if err := protojson.Unmarshal(b, v); err != nil {
return fmt.Errorf("invalid json: %w", err)
}
case ".yaml", ".yml":
if err := protojson.Unmarshal(b, v); err != nil {
return fmt.Errorf("invalid yaml: %w", err)
}
default:
return errors.New("unsupported file type")
}
return nil
}
func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
func makeMapFromString(commaSepStr string) map[string]string {
m := make(map[string]string)
keyValArray := strings.Split(commaSepStr, ",")
for _, s := range keyValArray {
arr := strings.Split(s, ":")
m[arr[0]] = arr[1]
}
return m
}