Refactor bootstraping procedure

This commit is contained in:
Denis Arh
2020-08-24 15:38:42 +02:00
parent 1147830663
commit f4e6d2ae5a
53 changed files with 718 additions and 1462 deletions
+18 -42
View File
@@ -9,30 +9,30 @@ import (
)
var (
rootCommand = cobra.Command{
rootCommand = &cobra.Command{
Use: "corteza-server",
Aliases: []string{"corteza", "server"},
TraverseChildren: true,
}
serveApiCommand = cobra.Command{
serveApiCommand = &cobra.Command{
Use: "serve-api",
Aliases: []string{"serve"},
Short: "Start HTTP server with REST API",
}
upgradeCommand = cobra.Command{
upgradeCommand = &cobra.Command{
Use: "upgrade",
Short: "Upgrade tasks",
}
provisionCommand = cobra.Command{
provisionCommand = &cobra.Command{
Use: "provision",
Short: "Provision tasks",
}
versionCommand = cobra.Command{
versionCommand = &cobra.Command{
Use: "version",
Short: "Version",
}
@@ -49,39 +49,24 @@ var (
//
// Callback is called when not executed with help subcommand
func RootCommand(ppRunEfn func() error) *cobra.Command {
// Make a copy
var (
cmd = rootCommand
silent, debug bool
)
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) (err error) {
rootCommand.PersistentPreRunE = func(cmd *cobra.Command, args []string) (err error) {
if light[cmd.Name()] {
// Do not run hooks on parts on help command
return nil
}
if debug {
logger.DefaultLevel.SetLevel(zap.DebugLevel)
} else if silent {
logger.DefaultLevel.SetLevel(zap.FatalLevel)
if ppRunEfn != nil {
return ppRunEfn()
}
return ppRunEfn()
return nil
}
cmd.Flags().BoolVarP(&silent, "silent", "s", false, "No output")
cmd.Flags().BoolVarP(&debug, "debug", "d", false, "Debug")
return &cmd
return rootCommand
}
func ServeCommand(runEfn func() error) *cobra.Command {
// Make a copy
var cmd = serveApiCommand
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
serveApiCommand.RunE = func(cmd *cobra.Command, args []string) (err error) {
if _, set := os.LookupEnv("LOG_LEVEL"); !set {
// If LOG_LEVEL is not explicitly set, let's
// set it to INFO so that it
@@ -91,38 +76,29 @@ func ServeCommand(runEfn func() error) *cobra.Command {
return runEfn()
}
return &cmd
return serveApiCommand
}
func UpgradeCommand(dbfn func() error) *cobra.Command {
// Make a copies
var dbCmd = upgradeCommand
dbCmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
upgradeCommand.RunE = func(cmd *cobra.Command, args []string) (err error) {
return dbfn()
}
return &dbCmd
return upgradeCommand
}
func ProvisionCommand(cffn func() error) *cobra.Command {
// Make a copies
var cfCmd = provisionCommand
cfCmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
provisionCommand.RunE = func(cmd *cobra.Command, args []string) (err error) {
return cffn()
}
return &cfCmd
return provisionCommand
}
func VersionCommand() *cobra.Command {
// Make a copies
var cfCmd = versionCommand
cfCmd.Run = func(cmd *cobra.Command, args []string) {
versionCommand.Run = func(cmd *cobra.Command, args []string) {
cmd.Println(version.Version)
}
return &cfCmd
return versionCommand
}