diff --git a/cmd/download.go b/cmd/download.go index 3bc502f..5e87ea1 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -33,7 +33,7 @@ var downloadCmd = cobra.Command{ makeshift download -xr `, 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, "path", "MAKESHIFT_PATH") }, @@ -158,10 +158,9 @@ var downloadProfileCmd = &cobra.Command{ `, Args: cobra.ExactArgs(1), Short: "Download a profile", - PreRun: func(cmd *cobra.Command, args []string) { - setenv(cmd, "host", "MAKESHIFT_HOST") - setenv(cmd, "path", "MAKESHIFT_PATH") - }, + // PreRun: func(cmd *cobra.Command, args []string) { + // setenv(cmd, "host", "MAKESHIFT_HOST") + // }, Run: func(cmd *cobra.Command, args []string) { var ( host, _ = cmd.Flags().GetString("host") @@ -173,8 +172,14 @@ var downloadProfileCmd = &cobra.Command{ query string err error ) + + log.Debug(). + Str("host", host). + Str("output", outputPath). + Send() + for _, profileID := range args { - query = fmt.Sprintf("/profile/{%s}", profileID) + query = fmt.Sprintf("/profile/%s", profileID) res, body, err = c.MakeRequest(client.HTTPEnvelope{ Path: query, Method: http.MethodGet, @@ -212,10 +217,9 @@ var downloadPluginCmd = &cobra.Command{ `, Args: cobra.ExactArgs(1), Short: "Download a plugin", - PreRun: func(cmd *cobra.Command, args []string) { - setenv(cmd, "host", "MAKESHIFT_HOST") - setenv(cmd, "path", "MAKESHIFT_PATH") - }, + // PreRun: func(cmd *cobra.Command, args []string) { + // setenv(cmd, "host", "MAKESHIFT_HOST") + // }, Run: func(cmd *cobra.Command, args []string) { var ( host, _ = cmd.Flags().GetString("host") @@ -229,7 +233,7 @@ var downloadPluginCmd = &cobra.Command{ ) for _, pluginName := range args { - query = fmt.Sprintf("/profile/%s?", pluginName) + query = fmt.Sprintf("/profile/%s", pluginName) res, body, err = c.MakeRequest(client.HTTPEnvelope{ Path: query, Method: http.MethodGet, @@ -260,9 +264,9 @@ var downloadPluginCmd = &cobra.Command{ } 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("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("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") diff --git a/cmd/root.go b/cmd/root.go index 375f4bc..b068dcd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,7 +18,7 @@ var ( // plugins []string // timeout int // logFile string - logLevel logger.LogLevel = logger.INFO + loglevel logger.LogLevel = logger.INFO ) var rootCmd = cobra.Command{ @@ -32,7 +32,7 @@ var rootCmd = cobra.Command{ // initialize the logger logFile, _ = cmd.Flags().GetString("log-file") - err = logger.InitWithLogLevel(logLevel, logFile) + err = logger.InitWithLogLevel(loglevel, logFile) if err != nil { log.Error().Err(err).Msg("failed to initialize logger") os.Exit(1) @@ -67,8 +67,12 @@ func Execute() { } func init() { + cobra.OnInitialize( + initLogger, + // initializeConfig, + ) // 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)") } @@ -81,3 +85,13 @@ func setenv(cmd *cobra.Command, varname string, envvar string) { 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) + } +}