config confirmation

This commit is contained in:
2025-08-01 00:56:05 +03:00
parent 08e96aa32a
commit 5cdfb2a543
9 changed files with 131 additions and 81 deletions

View File

@@ -14,7 +14,7 @@ import (
)
type AppContract interface {
InitialHooks(fn ...func(cs *corestate.CoreState, x *AppX))
InitialHooks(fn ...func(ctx context.Context, cs *corestate.CoreState, x *AppX))
Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX) error)
Fallback(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX))
@@ -22,7 +22,7 @@ type AppContract interface {
}
type App struct {
initHooks []func(cs *corestate.CoreState, x *AppX)
initHooks []func(ctx context.Context, cs *corestate.CoreState, x *AppX)
runHook func(ctx context.Context, cs *corestate.CoreState, x *AppX) error
fallback func(ctx context.Context, cs *corestate.CoreState, x *AppX)
@@ -47,7 +47,7 @@ func New() AppContract {
}
}
func (a *App) InitialHooks(fn ...func(cs *corestate.CoreState, x *AppX)) {
func (a *App) InitialHooks(fn ...func(ctx context.Context, cs *corestate.CoreState, x *AppX)) {
a.initHooks = append(a.initHooks, fn...)
}
@@ -58,13 +58,13 @@ func (a *App) Fallback(fn func(ctx context.Context, cs *corestate.CoreState, x *
func (a *App) Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX) error) {
a.runHook = fn
for _, hook := range a.initHooks {
hook(a.Corestate, a.AppX)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer stop()
for _, hook := range a.initHooks {
hook(ctx, a.Corestate, a.AppX)
}
defer func() {
if r := recover(); r != nil {
a.AppX.Log.Printf("PANIC recovered: %v", r)
@@ -90,5 +90,6 @@ func (a *App) CallFallback(ctx context.Context) {
if a.fallback != nil {
a.fallback(ctx, a.Corestate, a.AppX)
}
os.Exit(3)
})
}

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"reflect"
"time"
"github.com/akyaiy/GoSally-mvp/internal/colors"
)
func (c *Compositor) Print(v any) {
@@ -29,9 +31,11 @@ func (c *Compositor) printConfig(v any, prefix string) {
}
}
coloredFieldName := colors.SetBrightCyan(fieldName)
if field.Kind() == reflect.Ptr {
if field.IsNil() {
fmt.Printf("%s%s: <nil>\n", prefix, fieldName)
fmt.Printf("%s%s: %s\n", prefix, coloredFieldName, colors.SetBrightRed("<nil>"))
continue
}
field = field.Elem()
@@ -40,19 +44,29 @@ func (c *Compositor) printConfig(v any, prefix string) {
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())
fmt.Printf("%s%s: %s\n",
prefix,
coloredFieldName,
colors.SetBrightYellow(duration.String()))
} else {
fmt.Printf("%s%s:\n", prefix, fieldName)
fmt.Printf("%s%s:\n", prefix, coloredFieldName)
c.printConfig(field.Addr().Interface(), prefix+" ")
}
} else if field.Kind() == reflect.Slice {
fmt.Printf("%s%s: %v\n", prefix, fieldName, field.Interface())
fmt.Printf("%s%s: %s\n",
prefix,
coloredFieldName,
colors.SetBrightYellow(fmt.Sprintf("%v", field.Interface())))
} else {
value := field.Interface()
valueStr := fmt.Sprintf("%v", value)
if field.Kind() == reflect.String {
value = fmt.Sprintf("\"%s\"", value)
valueStr = fmt.Sprintf("\"%s\"", value)
}
fmt.Printf("%s%s: %v\n", prefix, fieldName, value)
fmt.Printf("%s%s: %s\n",
prefix,
coloredFieldName,
colors.SetBrightYellow(valueStr))
}
}
}

View File

@@ -1,24 +0,0 @@
package logs
import "fmt"
func SetBlack(s string) string { return fmt.Sprintf("\033[30m%s\033[0m", s) }
func SetRed(s string) string { return fmt.Sprintf("\033[31m%s\033[0m", s) }
func SetGreen(s string) string { return fmt.Sprintf("\033[32m%s\033[0m", s) }
func SetYellow(s string) string { return fmt.Sprintf("\033[33m%s\033[0m", s) }
func SetBlue(s string) string { return fmt.Sprintf("\033[34m%s\033[0m", s) }
func SetMagenta(s string) string { return fmt.Sprintf("\033[35m%s\033[0m", s) }
func SetCyan(s string) string { return fmt.Sprintf("\033[36m%s\033[0m", s) }
func SetWhite(s string) string { return fmt.Sprintf("\033[37m%s\033[0m", s) }
func SetBrightBlack(s string) string { return fmt.Sprintf("\033[90m%s\033[0m", s) }
func SetBrightRed(s string) string { return fmt.Sprintf("\033[91m%s\033[0m", s) }
func SetBrightGreen(s string) string { return fmt.Sprintf("\033[92m%s\033[0m", s) }
func SetBrightYellow(s string) string { return fmt.Sprintf("\033[93m%s\033[0m", s) }
func SetBrightBlue(s string) string { return fmt.Sprintf("\033[94m%s\033[0m", s) }
func SetBrightMagenta(s string) string { return fmt.Sprintf("\033[95m%s\033[0m", s) }
func SetBrightCyan(s string) string { return fmt.Sprintf("\033[96m%s\033[0m", s) }
func SetBrightWhite(s string) string { return fmt.Sprintf("\033[97m%s\033[0m", s) }
func PrintError() string { return SetRed("Error") }
func PrintWarn() string { return SetYellow("Warning") }