add optional config preview with confirmation

This commit is contained in:
2025-08-01 00:08:38 +03:00
parent 3b8390a0c8
commit 08e96aa32a
5 changed files with 88 additions and 4 deletions

View File

@@ -45,6 +45,7 @@ func (c *Compositor) LoadConf(path string) error {
// defaults
v.SetDefault("node.name", "noname")
v.SetDefault("node.mode", "dev")
v.SetDefault("node.show_config", "false")
v.SetDefault("node.com_dir", "./com/")
v.SetDefault("http_server.address", "0.0.0.0")
v.SetDefault("http_server.port", "8080")

View File

@@ -27,9 +27,10 @@ type Conf struct {
}
type Node struct {
Mode *string `mapstructure:"mode"`
Name *string `mapstructure:"name"`
ComDir *string `mapstructure:"com_dir"`
Mode *string `mapstructure:"mode"`
Name *string `mapstructure:"name"`
ShowConfig *bool `mapstructure:"show_config"`
ComDir *string `mapstructure:"com_dir"`
}
type HTTPServer struct {

View File

@@ -0,0 +1,58 @@
package config
import (
"fmt"
"reflect"
"time"
)
func (c *Compositor) Print(v any) {
c.printConfig(v, " ")
}
func (c *Compositor) printConfig(v any, prefix string) {
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := typ.Field(i)
fieldName := fieldType.Name
if tag, ok := fieldType.Tag.Lookup("mapstructure"); ok {
if tag != "" {
fieldName = tag
}
}
if field.Kind() == reflect.Ptr {
if field.IsNil() {
fmt.Printf("%s%s: <nil>\n", prefix, fieldName)
continue
}
field = field.Elem()
}
if field.Kind() == reflect.Struct {
if field.Type() == reflect.TypeOf(time.Duration(0)) {
duration := field.Interface().(time.Duration)
fmt.Printf("%s%s: %s\n", prefix, fieldName, duration.String())
} else {
fmt.Printf("%s%s:\n", prefix, fieldName)
c.printConfig(field.Addr().Interface(), prefix+" ")
}
} else if field.Kind() == reflect.Slice {
fmt.Printf("%s%s: %v\n", prefix, fieldName, field.Interface())
} else {
value := field.Interface()
if field.Kind() == reflect.String {
value = fmt.Sprintf("\"%s\"", value)
}
fmt.Printf("%s%s: %v\n", prefix, fieldName, value)
}
}
}