Skip to content

Commit

Permalink
Improved config reading and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
macwis committed Nov 23, 2023
1 parent 1eac8a9 commit 1a540fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions services/sidecar/cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"github.com/sirupsen/logrus"
"github.com/specularL2/specular/services/sidecar/internal/service/di"
"github.com/specularL2/specular/services/sidecar/rollup/services"
"log"
"os"
)

func main() {
app, _, err := di.SetupApplication()
if err != nil {
log.Fatalf("failed to setup application #{err}")
logrus.Fatalf("failed to setup application: %s", err)
os.Exit(1)
}

app.GetCli().Flags = services.CLIFlags()
Expand Down
3 changes: 3 additions & 0 deletions services/sidecar/config/default.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
---
SERVICE_NAME: "sidecar"
USAGE_DESC: "launch a validator and/or disseminator"
SERVICE_VERSION: "unknown"
LOG_LEVEL: "DEBUG"
38 changes: 24 additions & 14 deletions services/sidecar/internal/service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ import (

const (
defaultLogLevel = logrus.InfoLevel
serviceName = "sidecar"
)

type Config struct {
Name string "sidecar"
Usage string "launch a validator and/or disseminator"
// Action: startServices,

ServiceName string `mapstructure:"SERVICE_NAME"`
ServiceVersion string `mapstructure:"SERVICE_VERSION"`
LogLevel string `mapstructure:"LOG_LEVEL"`

UsageDesc string `mapstructure:"USAGE_DESC"`
LogLevel string `mapstructure:"LOG_LEVEL"`
}

func (c *Config) GetLogLevel(defaultLevel logrus.Level) logrus.Level {
Expand All @@ -32,11 +29,24 @@ func (c *Config) GetLogLevel(defaultLevel logrus.Level) logrus.Level {
return level
}

func (c *Config) IsValid() error {
if len(c.ServiceName) == 0 {
return errors.New("invalid config: SERVICE_NAME cannot be empty")
}

if len(c.UsageDesc) == 0 {
return errors.New("invalid config: USAGE_DESC cannot be empty")
}

return nil
}

func LoadConfig(log *logrus.Logger, configObject *Config, fileNames ...string) (*viper.Viper, error) {
mainConfig := viper.New()
fileNames = append([]string{"default.yaml", "config/default.yaml"}, fileNames...)
fileNames = append([]string{"config/default.yaml"}, fileNames...)

for _, fileName := range fileNames {
log.Infof("Loading config from: %s", fileName)
viperConfig := viper.New()
viperConfig.SetConfigFile(fileName)

Expand Down Expand Up @@ -74,19 +84,19 @@ func newConfig(configFiles []string) (*Config, error) {
return nil, err
}

//err = cfg.IsValid()
//if err != nil {
// return nil, err
//}
err = cfg.IsValid()
if err != nil {
return nil, err
}

return &cfg, nil
}

func NewConfig() (*Config, error) {
configFiles := []string{
"default.yaml",
"config/default.yaml",
"/config/config.yaml",
// "default.yaml",
// "config/default.yaml",
// "/config/config.yaml",
// ".env",
}

Expand Down
2 changes: 1 addition & 1 deletion services/sidecar/internal/service/config/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func NewLogger(cfg *Config) *logrus.Logger {
log := logrus.New()
log.SetLevel(level)
log.SetOutput(os.Stdout)
log.WithField("name", serviceName).Info("service is starting")
log.WithField("name", cfg.ServiceName).Info("service is starting")

return log
}
10 changes: 5 additions & 5 deletions services/sidecar/internal/sidecar/api/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

func NewCli(cfg *config.Config) (*cli.App, error) {
cli := &cli.App{
Name: "sidecar",
Usage: "launch a validator and/or disseminator",
cliApp := &cli.App{
Name: cfg.ServiceName,
Usage: cfg.UsageDesc,
}
cli.Flags = services.CLIFlags()
return cli, nil
cliApp.Flags = services.CLIFlags()
return cliApp, nil
}

0 comments on commit 1a540fd

Please sign in to comment.