Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add node simplified config #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cartesi/rollups-node

go 1.21.1
10 changes: 10 additions & 0 deletions internal/cmd/print_config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main

import "github.com/cartesi/rollups-node/internal/pkg/config"

func main() {
config.PrintDocumentation()
}
35 changes: 35 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// This package loads the node config from enviroment variables
package config

import (
"fmt"
"os"
)

// Prefix to all enviroment variables
const Prefix string = "CARTESI_"

// Definition of config variables.
// For each variable declared below, there should be a entry in meta.go.

var PrintConfig bool
var LogLevel string

// Prints the documentation for each variable in Stdout
func PrintDocumentation() {
genDocumentation(os.Stdout)
}

// Loads the config from the environment.
// If PrintConfig is true, prints the config to Stdout.
// This function calls os.Exit if there is an error.
func Load() {
err := loadFromEnv(os.Stdout)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
78 changes: 78 additions & 0 deletions internal/pkg/config/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package config

import (
"errors"
"fmt"
"io"
"os"
"slices"
)

// Describes a config variable.
// This is useful for automatically generating documentation.
type MetaConfig struct {
key string
description string
defaultValue string
parse func(string) error
}

func (c MetaConfig) getKey() string {
return Prefix + c.key
}

// Meta config for each config variable
var configs []MetaConfig = []MetaConfig{
{
"PRINT_CONFIG",
"If 'true' prints the config variables as they are parsed.\n" +
"Can be 'true' or 'false'.",
"true",
func(value string) error {
var err error
PrintConfig, err = parseBool(value)
return err
},
},
{
"LOG_LEVEL",
"Sets the log level of the node, can be 'trace', 'info', or 'warning'.",
"info",
func(value string) error {
valid := []string{"trace", "info", "warning"}
if !slices.Contains(valid, value) {
return errors.New("invalid value")
}
LogLevel = value
return nil
},
},
}

func loadFromEnv(writer io.Writer) error {
for _, c := range configs {
value, exists := os.LookupEnv(c.getKey())
if !exists {
value = c.defaultValue
}
err := c.parse(value)
if err != nil {
msg := "failed to parse %v=%v because of %v"
return errors.New(fmt.Sprintf(msg, c.getKey(), value, err))
}
if PrintConfig {
fmt.Fprintf(writer, "%v=%v\n", c.getKey(), value)
}
}
return nil
}

func genDocumentation(writer io.Writer) {
for _, c := range configs {
msg := "## %v\n%v\nDefault value: '%v'\n\n"
fmt.Fprintf(writer, msg, c.getKey(), c.description, c.defaultValue)
}
}
18 changes: 18 additions & 0 deletions internal/pkg/config/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package config

import "errors"

// Reads a boolean value from the string
func parseBool(value string) (bool, error) {
switch value {
case "true":
return true, nil
case "false":
return false, nil
default:
return false, errors.New("invalid value")
}
}