mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 08:52:24 +00:00
commit to merge branches
This commit is contained in:
@@ -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
|
package cmd
|
||||||
|
|
||||||
import (
|
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() {
|
func Execute() {
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
log.SetPrefix(colors.SetBrightBlack(fmt.Sprintf("(%s) ", corestate.StageNotReady)))
|
log.SetPrefix(colors.SetBrightBlack(fmt.Sprintf("(%s) ", corestate.StageNotReady)))
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ var runCmd = &cobra.Command{
|
|||||||
Short: "Run node normally",
|
Short: "Run node normally",
|
||||||
Long: `
|
Long: `
|
||||||
"run" starts the node with settings depending on the configuration file`,
|
"run" starts the node with settings depending on the configuration file`,
|
||||||
|
// hooks.Run essentially the heart of the program
|
||||||
Run: hooks.Run,
|
Run: hooks.Run,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import (
|
|||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// The config composer needs to be in the global scope
|
||||||
var Compositor *config.Compositor = config.NewCompositor()
|
var Compositor *config.Compositor = config.NewCompositor()
|
||||||
|
|
||||||
func InitGlobalLoggerHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
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"))
|
uuid32, err := corestate.GetNodeUUID(filepath.Join(cs.MetaDir, "uuid"))
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
if err := corestate.SetNodeUUID(filepath.Join(cs.NodePath, cs.MetaDir, cs.UUID32DirName)); err != nil {
|
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
|
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) {
|
func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
||||||
if *x.Config.Env.ParentStagePID != os.Getpid() {
|
if *x.Config.Env.ParentStagePID != os.Getpid() {
|
||||||
// still pre-init stage
|
// still pre-init stage
|
||||||
@@ -134,6 +138,8 @@ func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// post-init stage
|
// 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) {
|
func InitRunlockHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
||||||
NodeApp.Fallback(func(ctx 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...")
|
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) {
|
func InitConfigReplHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
||||||
if !slices.Contains(*x.Config.Conf.DisableWarnings, "--WNonStdTmpDir") && os.TempDir() != "/tmp" {
|
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")
|
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) {
|
func InitConfigPrintHook(ctx context.Context, cs *corestate.CoreState, x *app.AppX) {
|
||||||
if *x.Config.Conf.Node.ShowConfig {
|
if *x.Config.Conf.Node.ShowConfig {
|
||||||
fmt.Printf("Configuration from %s:\n", x.Config.CMDLine.Run.ConfigPath)
|
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
|
*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 {
|
func processConfig(conf any, replacements map[string]any) error {
|
||||||
val := reflect.ValueOf(conf)
|
val := reflect.ValueOf(conf)
|
||||||
if val.Kind() == reflect.Ptr {
|
if val.Kind() == reflect.Ptr {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ var NodeApp = app.New()
|
|||||||
func Run(cmd *cobra.Command, args []string) {
|
func Run(cmd *cobra.Command, args []string) {
|
||||||
NodeApp.InitialHooks(
|
NodeApp.InitialHooks(
|
||||||
InitGlobalLoggerHook, InitCorestateHook, InitConfigLoadHook,
|
InitGlobalLoggerHook, InitCorestateHook, InitConfigLoadHook,
|
||||||
InitUUUDHook, InitRuntimeHook, InitRunlockHook,
|
InitUUIDHook, InitRuntimeHook, InitRunlockHook,
|
||||||
InitConfigReplHook, InitConfigPrintHook, InitSLogHook,
|
InitConfigReplHook, InitConfigPrintHook, InitSLogHook,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user