feat: updated implementations for cmds
This commit is contained in:
parent
7a96bfd6c7
commit
59a5225b28
7 changed files with 331 additions and 102 deletions
90
cmd/root.go
90
cmd/root.go
|
|
@ -3,36 +3,58 @@ package cmd
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
logger "git.towk2.me/towk/makeshift/pkg/log"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
host string
|
||||
path string
|
||||
outputPath string
|
||||
rootPath string
|
||||
logLevel string
|
||||
profile string
|
||||
plugins []string
|
||||
timeout int
|
||||
// host string
|
||||
// path string
|
||||
// outputPath string
|
||||
// rootPath string
|
||||
// profile string
|
||||
// plugins []string
|
||||
// timeout int
|
||||
// logFile string
|
||||
logLevel logger.LogLevel = logger.INFO
|
||||
)
|
||||
|
||||
var rootCmd = cobra.Command{
|
||||
Use: "configurator",
|
||||
Short: "Extensible configuration builder to download files",
|
||||
Use: "makeshift",
|
||||
Short: "Extensible file cobbler",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
// set the logging level
|
||||
level, err := strToLogLevel(logLevel)
|
||||
var (
|
||||
logFile string
|
||||
err error
|
||||
)
|
||||
|
||||
// initialize the logger
|
||||
logFile, _ = cmd.Flags().GetString("log-file")
|
||||
err = logger.InitWithLogLevel(logLevel, logFile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to convert log level argument")
|
||||
log.Error().Err(err).Msg("failed to initialize logger")
|
||||
os.Exit(1)
|
||||
}
|
||||
zerolog.SetGlobalLevel(level)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// try and set flags using env vars
|
||||
setenv(cmd, "log-file", "MAKESHIFT_LOG_FILE")
|
||||
if len(args) == 0 {
|
||||
err := cmd.Help()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to print help")
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
},
|
||||
PostRun: func(cmd *cobra.Command, args []string) {
|
||||
log.Debug().Msg("closing log file")
|
||||
err := logger.LogFile.Close()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to close log file")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -46,34 +68,16 @@ func Execute() {
|
|||
|
||||
func init() {
|
||||
// initialize the config a single time
|
||||
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "Set the log level output")
|
||||
rootCmd.PersistentFlags().VarP(&logLevel, "log-level", "l", "Set the log level output")
|
||||
rootCmd.PersistentFlags().String("log-file", "", "Set the log file path (can be set with MAKESHIFT_LOG_FILE)")
|
||||
}
|
||||
|
||||
func strToLogLevel(ll string) (zerolog.Level, error) {
|
||||
levels := []string{"debug", "info", "warn", "disabled"}
|
||||
if index := slices.Index(levels, ll); index >= 0 {
|
||||
// handle special case to map index == 3 to zerolog.Disabled == 7
|
||||
switch index {
|
||||
case 3:
|
||||
return zerolog.Disabled, nil
|
||||
}
|
||||
return zerolog.Level(index), nil
|
||||
func setenv(cmd *cobra.Command, varname string, envvar string) {
|
||||
if cmd.Flags().Changed(varname) {
|
||||
return
|
||||
}
|
||||
return -100, fmt.Errorf(
|
||||
"invalid log level (options: %s)", strings.Join(levels, ", "),
|
||||
) // use 'info' by default
|
||||
}
|
||||
|
||||
func setenv(v *string, key string) {
|
||||
t := os.Getenv(key)
|
||||
if t != "" {
|
||||
*v = t
|
||||
val := os.Getenv(envvar)
|
||||
if val != "" {
|
||||
cmd.Flags().Set(varname, val)
|
||||
}
|
||||
}
|
||||
|
||||
// func setenv(cmd *cobra.Command, varname string, envvar string) {
|
||||
// v := os.Getenv(envvar)
|
||||
// if v != "" {
|
||||
// cmd.Flags().Set(varname, v)
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue