@@ -2,12 +2,11 @@ package cmd
22
33import (
44 "bytes"
5+ "crypto/rand"
56 "fmt"
6- "math/rand"
77 "os"
88 "path"
99 "strings"
10- "time"
1110
1211 "github.com/rss3-network/node-automated-deployer/pkg/compose"
1312 "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
7170}
7271
7372func randomString (n int ) string {
74- r := rand .New (rand .NewSource (time .Now ().UnixNano ()))
7573 const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
74+
7675 b := make ([]byte , n )
76+ if _ , err := rand .Read (b ); err != nil {
77+ panic (err )
78+ }
79+
7780 for i := range b {
78- b [i ] = letterBytes [r . Intn ( len (letterBytes ) )]
81+ b [i ] = letterBytes [int ( b [ i ]) % len (letterBytes )]
7982 }
83+
8084 return string (b )
8185}
8286
@@ -103,16 +107,20 @@ func patchConfigFileWithAccessToken(file string, accessToken string) error {
103107 if err != nil {
104108 return fmt .Errorf ("patch config file with generated access token, find discovery node, %w" , err )
105109 }
110+
106111 if discoveryNode == nil {
107112 return fmt .Errorf ("patch config file with generated access token, discovery node not found" )
108113 }
114+
109115 serverNode , err := findYamlNode ("server" , discoveryNode )
110116 if err != nil {
111117 return fmt .Errorf ("patch config file with generated access token, find server node, %w" , err )
112118 }
119+
113120 if serverNode == nil {
114121 return fmt .Errorf ("patch config file with generated access token, server node not found" )
115122 }
123+
116124 accessTokenNode , err := findYamlNode ("access_token" , serverNode )
117125 if err != nil {
118126 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 {
150158 if err != nil {
151159 return fmt .Errorf ("patch config file with generated access token, encode config file, %w" , err )
152160 }
161+
153162 f .Close ()
154163
155164 return nil
@@ -165,21 +174,23 @@ func discoverConfigFile(file string) (string, error) {
165174 _ , err = os .Stat (path .Join ("config" , file ))
166175 if err == nil {
167176 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
173177 }
174- } else {
178+
179+ if os .IsNotExist (err ) {
180+ return "" , fmt .Errorf ("config file %s not found" , file )
181+ }
182+
175183 return "" , err
176184 }
185+
186+ return "" , err
177187}
178188
179189func findYamlNode (fieldName string , parent * yaml.Node ) (* yaml.Node , error ) {
180190 if parent == nil {
181191 return nil , fmt .Errorf ("find yaml node with field %s, parent node is nil" , fieldName )
182192 }
193+
183194 for i , node := range parent .Content {
184195 if node .Value == fieldName {
185196 return parent .Content [i + 1 ], nil
0 commit comments