@@ -2,12 +2,11 @@ package cmd
2
2
3
3
import (
4
4
"bytes"
5
+ "crypto/rand"
5
6
"fmt"
6
- "math/rand"
7
7
"os"
8
8
"path"
9
9
"strings"
10
- "time"
11
10
12
11
"github.com/rss3-network/node-automated-deployer/pkg/compose"
13
12
"github.com/rss3-network/node/config"
@@ -71,12 +70,17 @@ Then, with a single command, you create and start all the services from your con
71
70
}
72
71
73
72
func randomString (n int ) string {
74
- r := rand .New (rand .NewSource (time .Now ().UnixNano ()))
75
73
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
74
+
76
75
b := make ([]byte , n )
76
+ if _ , err := rand .Read (b ); err != nil {
77
+ panic (err )
78
+ }
79
+
77
80
for i := range b {
78
- b [i ] = letterBytes [r . Intn ( len (letterBytes ) )]
81
+ b [i ] = letterBytes [int ( b [ i ]) % len (letterBytes )]
79
82
}
83
+
80
84
return string (b )
81
85
}
82
86
@@ -103,16 +107,20 @@ func patchConfigFileWithAccessToken(file string, accessToken string) error {
103
107
if err != nil {
104
108
return fmt .Errorf ("patch config file with generated access token, find discovery node, %w" , err )
105
109
}
110
+
106
111
if discoveryNode == nil {
107
112
return fmt .Errorf ("patch config file with generated access token, discovery node not found" )
108
113
}
114
+
109
115
serverNode , err := findYamlNode ("server" , discoveryNode )
110
116
if err != nil {
111
117
return fmt .Errorf ("patch config file with generated access token, find server node, %w" , err )
112
118
}
119
+
113
120
if serverNode == nil {
114
121
return fmt .Errorf ("patch config file with generated access token, server node not found" )
115
122
}
123
+
116
124
accessTokenNode , err := findYamlNode ("access_token" , serverNode )
117
125
if err != nil {
118
126
return fmt .Errorf ("patch config file with generated access token, find access_token node, %w" , err )
@@ -150,6 +158,7 @@ func patchConfigFileWithAccessToken(file string, accessToken string) error {
150
158
if err != nil {
151
159
return fmt .Errorf ("patch config file with generated access token, encode config file, %w" , err )
152
160
}
161
+
153
162
f .Close ()
154
163
155
164
return nil
@@ -165,21 +174,23 @@ func discoverConfigFile(file string) (string, error) {
165
174
_ , err = os .Stat (path .Join ("config" , file ))
166
175
if err == nil {
167
176
return path .Join ("config" , file ), nil
168
- } else {
169
- if os .IsNotExist (err ) {
170
- return "" , fmt .Errorf ("config file %s not found" , file )
171
- }
172
- return "" , err
173
177
}
174
- } else {
178
+
179
+ if os .IsNotExist (err ) {
180
+ return "" , fmt .Errorf ("config file %s not found" , file )
181
+ }
182
+
175
183
return "" , err
176
184
}
185
+
186
+ return "" , err
177
187
}
178
188
179
189
func findYamlNode (fieldName string , parent * yaml.Node ) (* yaml.Node , error ) {
180
190
if parent == nil {
181
191
return nil , fmt .Errorf ("find yaml node with field %s, parent node is nil" , fieldName )
182
192
}
193
+
183
194
for i , node := range parent .Content {
184
195
if node .Value == fieldName {
185
196
return parent .Content [i + 1 ], nil
0 commit comments