diff --git a/cmd/root.go b/cmd/root.go index 843ced6..cec01c4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,3 +1,5 @@ +// The cmd package is the main package where all the main hooks and methods are called. +// GoSally uses spf13/cobra to organize all the calls. package cmd import ( @@ -22,6 +24,8 @@ scripts in a given directory. For more information, visit: https://gosally.oblat }, } +// Execute prepares global log, loads cmdline args +// and executes rootCmd.Execute() func Execute() { log.SetOutput(os.Stdout) log.SetPrefix(colors.SetBrightBlack(fmt.Sprintf("(%s) ", corestate.StageNotReady))) diff --git a/cmd/run.go b/cmd/run.go index 36e2b86..6f096c6 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -11,6 +11,7 @@ var runCmd = &cobra.Command{ Short: "Run node normally", Long: ` "run" starts the node with settings depending on the configuration file`, + // hooks.Run essentially the heart of the program Run: hooks.Run, } diff --git a/hooks/initial.go b/hooks/initial.go index 65b4314..c49a8e6 100644 --- a/hooks/initial.go +++ b/hooks/initial.go @@ -28,6 +28,7 @@ import ( "gopkg.in/ini.v1" ) +// The config composer needs to be in the global scope var Compositor *config.Compositor = config.NewCompositor() func InitGlobalLoggerHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { @@ -65,7 +66,8 @@ func InitConfigLoadHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) } } -func InitUUUDHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { +// The hook reads or prepares a persistent uuid for the node +func InitUUIDHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { uuid32, err := corestate.GetNodeUUID(filepath.Join(cs.MetaDir, "uuid")) if errors.Is(err, fs.ErrNotExist) { if err := corestate.SetNodeUUID(filepath.Join(cs.NodePath, cs.MetaDir, cs.UUID32DirName)); err != nil { @@ -83,6 +85,8 @@ func InitUUUDHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { corestate.NODE_UUID = uuid32 } +// The hook is responsible for checking the initialization stage +// and restarting in some cases func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { if *x.Config.Env.ParentStagePID != os.Getpid() { // still pre-init stage @@ -134,6 +138,8 @@ func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { } // post-init stage +// The hook creates a run.lock file, which contains information +// about the process and the node, in the runtime directory. func InitRunlockHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { NodeApp.Fallback(func(ctx context.Context, cs *corestate.CoreState, x *app.AppX) { x.Log.Println("Cleaning up...") @@ -186,6 +192,8 @@ func InitRunlockHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { } } +// The hook reads the configuration and replaces special expressions +// (%tmp% and so on) in string fields with the required data. func InitConfigReplHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { if !slices.Contains(*x.Config.Conf.DisableWarnings, "--WNonStdTmpDir") && os.TempDir() != "/tmp" { x.Log.Printf("%s: %s", colors.PrintWarn(), "Non-standard value specified for temporary directory") @@ -210,6 +218,8 @@ func InitConfigReplHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) } } +// The hook is responsible for outputting the +// final config and asking for confirmation. func InitConfigPrintHook(ctx context.Context, cs *corestate.CoreState, x *app.AppX) { if *x.Config.Conf.Node.ShowConfig { fmt.Printf("Configuration from %s:\n", x.Config.CMDLine.Run.ConfigPath) @@ -240,6 +250,8 @@ func InitSLogHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { *x.SLog = *newSlog } +// The method goes through the entire config structure through +// reflection and replaces string fields with the required ones. func processConfig(conf any, replacements map[string]any) error { val := reflect.ValueOf(conf) if val.Kind() == reflect.Ptr { diff --git a/hooks/run.go b/hooks/run.go index ed0c36e..7c00500 100644 --- a/hooks/run.go +++ b/hooks/run.go @@ -33,7 +33,7 @@ var NodeApp = app.New() func Run(cmd *cobra.Command, args []string) { NodeApp.InitialHooks( InitGlobalLoggerHook, InitCorestateHook, InitConfigLoadHook, - InitUUUDHook, InitRuntimeHook, InitRunlockHook, + InitUUIDHook, InitRuntimeHook, InitRunlockHook, InitConfigReplHook, InitConfigPrintHook, InitSLogHook, ) diff --git a/main.go b/main.go index bd37215..f1e6c1e 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// Package main used only for calling cmd.Execute() package main import (