fix: issue with logging not initializing correctly

This commit is contained in:
David Allen 2025-08-29 16:02:36 -06:00
parent e5c1b59bc1
commit 3244a66f8e
Signed by: towk
GPG key ID: 0430CDBE22619155
2 changed files with 34 additions and 16 deletions

View file

@ -33,7 +33,7 @@ var downloadCmd = cobra.Command{
makeshift download -xr makeshift download -xr
`, `,
Short: "Download and modify files with plugins", Short: "Download and modify files with plugins",
PreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
setenv(cmd, "host", "MAKESHIFT_HOST") setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH") setenv(cmd, "path", "MAKESHIFT_PATH")
}, },
@ -158,10 +158,9 @@ var downloadProfileCmd = &cobra.Command{
`, `,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Short: "Download a profile", Short: "Download a profile",
PreRun: func(cmd *cobra.Command, args []string) { // PreRun: func(cmd *cobra.Command, args []string) {
setenv(cmd, "host", "MAKESHIFT_HOST") // setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH") // },
},
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var ( var (
host, _ = cmd.Flags().GetString("host") host, _ = cmd.Flags().GetString("host")
@ -173,8 +172,14 @@ var downloadProfileCmd = &cobra.Command{
query string query string
err error err error
) )
log.Debug().
Str("host", host).
Str("output", outputPath).
Send()
for _, profileID := range args { for _, profileID := range args {
query = fmt.Sprintf("/profile/{%s}", profileID) query = fmt.Sprintf("/profile/%s", profileID)
res, body, err = c.MakeRequest(client.HTTPEnvelope{ res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query, Path: query,
Method: http.MethodGet, Method: http.MethodGet,
@ -212,10 +217,9 @@ var downloadPluginCmd = &cobra.Command{
`, `,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Short: "Download a plugin", Short: "Download a plugin",
PreRun: func(cmd *cobra.Command, args []string) { // PreRun: func(cmd *cobra.Command, args []string) {
setenv(cmd, "host", "MAKESHIFT_HOST") // setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH") // },
},
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var ( var (
host, _ = cmd.Flags().GetString("host") host, _ = cmd.Flags().GetString("host")
@ -229,7 +233,7 @@ var downloadPluginCmd = &cobra.Command{
) )
for _, pluginName := range args { for _, pluginName := range args {
query = fmt.Sprintf("/profile/%s?", pluginName) query = fmt.Sprintf("/profile/%s", pluginName)
res, body, err = c.MakeRequest(client.HTTPEnvelope{ res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query, Path: query,
Method: http.MethodGet, Method: http.MethodGet,
@ -260,9 +264,9 @@ var downloadPluginCmd = &cobra.Command{
} }
func init() { func init() {
downloadCmd.Flags().String("host", "http://localhost:5050", "Set the makeshift remote host (can be set with MAKESHIFT_HOST)") downloadCmd.PersistentFlags().String("host", "http://localhost:5050", "Set the makeshift remote host (can be set with MAKESHIFT_HOST)")
downloadCmd.PersistentFlags().StringP("output", "o", "", "Set the output path to write files")
downloadCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)") downloadCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
downloadCmd.Flags().StringP("output", "o", "", "Set the output path to write files")
downloadCmd.Flags().StringSlice("profiles", []string{}, "Set the profile(s) to use to populate data store") downloadCmd.Flags().StringSlice("profiles", []string{}, "Set the profile(s) to use to populate data store")
downloadCmd.Flags().StringSlice("plugins", []string{}, "Set the plugin(s) to run before downloading files") downloadCmd.Flags().StringSlice("plugins", []string{}, "Set the plugin(s) to run before downloading files")
downloadCmd.Flags().BoolP("extract", "x", false, "Set whether to extract archive locally after downloading") downloadCmd.Flags().BoolP("extract", "x", false, "Set whether to extract archive locally after downloading")

View file

@ -18,7 +18,7 @@ var (
// plugins []string // plugins []string
// timeout int // timeout int
// logFile string // logFile string
logLevel logger.LogLevel = logger.INFO loglevel logger.LogLevel = logger.INFO
) )
var rootCmd = cobra.Command{ var rootCmd = cobra.Command{
@ -32,7 +32,7 @@ var rootCmd = cobra.Command{
// initialize the logger // initialize the logger
logFile, _ = cmd.Flags().GetString("log-file") logFile, _ = cmd.Flags().GetString("log-file")
err = logger.InitWithLogLevel(logLevel, logFile) err = logger.InitWithLogLevel(loglevel, logFile)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to initialize logger") log.Error().Err(err).Msg("failed to initialize logger")
os.Exit(1) os.Exit(1)
@ -67,8 +67,12 @@ func Execute() {
} }
func init() { func init() {
cobra.OnInitialize(
initLogger,
// initializeConfig,
)
// initialize the config a single time // initialize the config a single time
rootCmd.PersistentFlags().VarP(&logLevel, "log-level", "l", "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)") rootCmd.PersistentFlags().String("log-file", "", "Set the log file path (can be set with MAKESHIFT_LOG_FILE)")
} }
@ -81,3 +85,13 @@ func setenv(cmd *cobra.Command, varname string, envvar string) {
cmd.Flags().Set(varname, val) cmd.Flags().Set(varname, val)
} }
} }
func initLogger() {
// initialize the logger
logfile, _ := rootCmd.PersistentFlags().GetString("log-file")
err := logger.InitWithLogLevel(loglevel, logfile)
if err != nil {
log.Error().Err(err).Msg("failed to initialize logger")
os.Exit(1)
}
}