@@ -6,11 +6,8 @@ import (
66 "strconv"
77 "strings"
88
9- log "github.com/sirupsen/logrus"
109 "github.com/spf13/viper"
1110 "github.com/urfave/cli/v2"
12-
13- "github.com/lukso-network/tools-lukso-cli/common/errors"
1411)
1512
1613const (
@@ -59,11 +56,18 @@ func parsePath(path string) (dir, fileName, extension string) {
5956}
6057
6158type Config struct {
62- path string
63- viper * viper.Viper
64- executionClient string `mapstructure:"execution"`
65- consensusClient string `mapstructure:"consensus"`
66- validatorClient string `mapstructure:"validator"`
59+ path string
60+ viper * viper.Viper
61+ configValues
62+ }
63+
64+ type configValues struct {
65+ UseClients struct {
66+ ExecutionClient string `mapstructure:"execution"`
67+ ConsensusClient string `mapstructure:"consensus"`
68+ ValidatorClient string `mapstructure:"validator"`
69+ } `mapstructure:"useclients"`
70+ Ipv4 string `mapstructure:"ipv4"`
6771}
6872
6973// NewConfig creates and initializes viper config instance - it doesn't load config, to load use c.Read().
@@ -83,7 +87,7 @@ func NewConfig(path string) *Config {
8387
8488// Create creates a new config that keeps track of selected dependencies and writes to it.
8589// By default, this file should be present in root of initialized lukso directory
86- func (c * Config ) Create (selectedExecution , selectedConsensus , selectedValidator string ) (err error ) {
90+ func (c * Config ) Create (selectedExecution , selectedConsensus , selectedValidator , ipv4 string ) (err error ) {
8791 _ , err = os .Create (c .path )
8892 if err != nil {
8993 return
@@ -92,6 +96,7 @@ func (c *Config) Create(selectedExecution, selectedConsensus, selectedValidator
9296 c .viper .Set ("useClients.execution" , selectedExecution )
9397 c .viper .Set ("useClients.consensus" , selectedConsensus )
9498 c .viper .Set ("useClients.validator" , selectedValidator )
99+ c .viper .Set ("ipv4" , ipv4 )
95100
96101 err = c .viper .WriteConfigAs (c .path )
97102
@@ -105,6 +110,10 @@ func (c *Config) Exists() bool {
105110}
106111
107112func (c * Config ) WriteExecution (selectedExecution string ) (err error ) {
113+ err = c .viper .ReadInConfig ()
114+ if err != nil {
115+ return
116+ }
108117 c .viper .Set ("useClients.execution" , selectedExecution )
109118
110119 err = c .viper .WriteConfigAs (c .path )
@@ -113,49 +122,72 @@ func (c *Config) WriteExecution(selectedExecution string) (err error) {
113122}
114123
115124func (c * Config ) WriteConsensus (selectedConsensus string ) (err error ) {
125+ err = c .viper .ReadInConfig ()
126+ if err != nil {
127+ return
128+ }
116129 c .viper .Set ("useClients.consensus" , selectedConsensus )
117130
118131 err = c .viper .WriteConfigAs (c .path )
119132
120133 return
121134}
122135
123- // Read reads from config file passed during config instance into c
124- func (c * Config ) Read () (err error ) {
136+ func (c * Config ) WriteValidator (selectedValidator string ) (err error ) {
125137 err = c .viper .ReadInConfig ()
126138 if err != nil {
127139 return
128140 }
141+ c .viper .Set ("useClients.validator" , selectedValidator )
142+
143+ err = c .viper .WriteConfigAs (c .path )
144+
145+ return
146+ }
129147
130- exec , execOk := c .viper .Get ("useClients.execution" ).(string )
131- cons , consOk := c .viper .Get ("useClients.consensus" ).(string )
132- val , valOk := c .viper .Get ("useClients.validator" ).(string )
148+ func (c * Config ) WriteIPv4 (ipv4 string ) (err error ) {
149+ err = c .viper .ReadInConfig ()
150+ if err != nil {
151+ return
152+ }
153+ c .viper .Set ("ipv4" , ipv4 )
133154
134- if ! execOk || ! consOk || ! valOk {
135- log .Error (errors .ErrOlderFolderDetected )
155+ err = c .viper .WriteConfigAs (c .path )
136156
137- os .Exit (1 )
157+ return
158+ }
138159
160+ // Read reads from config file passed during config instance into c
161+ func (c * Config ) Read () (err error ) {
162+ err = c .viper .ReadInConfig ()
163+ if err != nil {
139164 return
140165 }
141166
142- c .executionClient = exec
143- c .consensusClient = cons
144- c .validatorClient = val
167+ err = c .viper .Unmarshal (& c .configValues )
145168
146169 return
147170}
148171
149172func (c * Config ) Execution () string {
150- return c .executionClient
173+ return c .UseClients . ExecutionClient
151174}
152175
153176func (c * Config ) Consensus () string {
154- return c .consensusClient
177+ return c .UseClients . ConsensusClient
155178}
156179
157180func (c * Config ) Validator () string {
158- return c .validatorClient
181+ return c .UseClients .ValidatorClient
182+ }
183+
184+ func (c * Config ) IPv4 () string {
185+ ip := c .Ipv4
186+ if ip == "" {
187+ ip = "0.0.0.0"
188+ }
189+
190+ return ip
159191}
160192
161193func LoadLighthouseConfig (path string ) (args []string , err error ) {
0 commit comments