-
Notifications
You must be signed in to change notification settings - Fork 17
/
tools.go
130 lines (114 loc) · 2.42 KB
/
tools.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
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
"k8s.io/klog/v2"
)
func isDirectory(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
return false, err
}
return info.IsDir(), nil
}
// exists checks whether a file or directory exists.
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
// file does not exist
return false, nil
}
// other error
return false, err
}
// isFile checks whether a path is a file.
func isFile(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
return false, err
}
return !info.IsDir(), nil
}
// isJSONFile checks whether a path is a JSON file.
func isJSONFile(filepath string) bool {
return filepath[len(filepath)-5:] == ".json"
}
// isYAMLFile checks whether a path is a YAML file.
func isYAMLFile(filepath string) bool {
return filepath[len(filepath)-5:] == ".yaml" || filepath[len(filepath)-4:] == ".yml"
}
// loadFromJSON loads a JSON file into dst (which must be a pointer).
func loadFromJSON(configFilepath string, dst any) error {
file, err := os.Open(configFilepath)
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
defer file.Close()
return fasterJson.NewDecoder(file).Decode(dst)
}
// loadFromYAML loads a YAML file into dst (which must be a pointer).
func loadFromYAML(configFilepath string, dst any) error {
file, err := os.Open(configFilepath)
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
defer file.Close()
return yaml.NewDecoder(file).Decode(dst)
}
type timer struct {
reqID string
start time.Time
prev time.Time
}
func newTimer(reqID string) *timer {
now := time.Now()
return &timer{
reqID: reqID,
start: now,
prev: now,
}
}
func (t *timer) time(name string) {
klog.V(4).Infof("[%s]: %q: %s (overall %s)", t.reqID, name, time.Since(t.prev), time.Since(t.start))
t.prev = time.Now()
}
// pub enum RewardType {
// Fee,
// Rent,
// Staking,
// Voting,
// }
func rewardTypeToString(typ int) string {
switch typ {
case 1:
return "Fee"
case 2:
return "Rent"
case 3:
return "Staking"
case 4:
return "Voting"
default:
return "Unknown"
}
}
func rewardTypeStringToInt(typ string) int {
switch typ {
case "Fee":
return 1
case "Rent":
return 2
case "Staking":
return 3
case "Voting":
return 4
default:
return 0
}
}
const CodeNotFound = -32009