-
Notifications
You must be signed in to change notification settings - Fork 178
/
node_info.go
51 lines (43 loc) · 1.07 KB
/
node_info.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
package ping
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/onflow/flow-go/model/flow"
)
func readExtraNodeInfoJSON(jsonFileName string) (map[flow.Identifier]string, error) {
// read the file
byteValue, err := openAndReadFile(jsonFileName)
if err != nil {
return nil, err
}
// unmarshal json data
result, err := unmarshalNodeInfoFromJSONData(byteValue)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal %s: %w", jsonFileName, err)
}
return result, nil
}
func openAndReadFile(fileName string) ([]byte, error) {
//open
jsonFile, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("failed to open %s: %w", fileName, err)
}
defer jsonFile.Close()
// read
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", fileName, err)
}
return byteValue, nil
}
func unmarshalNodeInfoFromJSONData(jsonData []byte) (map[flow.Identifier]string, error) {
var result map[flow.Identifier]string
err := json.Unmarshal(jsonData, &result)
if err != nil {
return nil, err
}
return result, nil
}