-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfile_utils.go
115 lines (101 loc) · 2.51 KB
/
file_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
package interchaintest
import (
"fmt"
"github.com/icon-project/ibc-integration/test/chains"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"os"
"path/filepath"
)
var ibcConfigPath = filepath.Join(os.Getenv(chains.BASE_PATH), "ibc-config")
func CleanBackupConfig() {
files, err := filepath.Glob(filepath.Join(ibcConfigPath, "*.json"))
if err != nil {
fmt.Println("Error deleting file:", err)
return
}
for _, file := range files {
err := os.Remove(file)
if err != nil {
fmt.Println("Error deleting file:", err)
}
}
}
// for saving data in particular format
func BackupConfig(chain chains.Chain) error {
config, err := chain.BackupConfig()
if err != nil {
return err
}
fileName := fmt.Sprintf("%s/%s.json", ibcConfigPath, chain.(ibc.Chain).Config().ChainID)
dirPath := filepath.Dir(fileName)
err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return err
}
file, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating file:", err)
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println("Error closing file:", err)
}
}(file)
_, err = file.Write(config)
return err
}
func GetLocalFileContent(fileName string) ([]byte, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("%s file not found : %w", fileName, err)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println("Error closing file:", err)
}
}(file)
fileInfo, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("unable to get file: %w", err)
}
fileSize := fileInfo.Size()
// Read the file content into a buffer
buffer := make([]byte, fileSize)
_, err = file.Read(buffer)
if err != nil {
return nil, fmt.Errorf("unable to get content: %w", err)
}
return buffer, nil
}
func RestoreConfig(chain chains.Chain) error {
fileName := fmt.Sprintf("%s/%s.json", ibcConfigPath, chain.(ibc.Chain).Config().ChainID)
file, err := os.Open(fileName)
if err != nil {
fmt.Println("Error opening file:", err)
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println("Error closing file:", err)
}
}(file)
fileInfo, err := file.Stat()
if err != nil {
fmt.Println("Error getting file info:", err)
return err
}
fileSize := fileInfo.Size()
// Read the file content into a buffer
buffer := make([]byte, fileSize)
_, err = file.Read(buffer)
if err != nil {
fmt.Println("Error reading file:", err)
return err
}
err = chain.RestoreConfig(buffer)
return err
}