diff --git a/cmd/layer/list/list.go b/cmd/layer/list/list.go index 6389e96..a7b0d75 100644 --- a/cmd/layer/list/list.go +++ b/cmd/layer/list/list.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" "github.com/ublue-os/bext/pkg/fileio" + "github.com/ublue-os/bext/pkg/logging" ) var ListCmd = &cobra.Command{ @@ -53,6 +54,10 @@ func listCmd(cmd *cobra.Command, args []string) error { return err } + if !*fLogOnly { + slog.SetDefault(logging.NewMuteLogger()) + } + if *fCheck { if len(args) < 1 { return internal.NewPositionalError("LAYER") diff --git a/cmd/root.go b/cmd/root.go index c98ec77..f2ae1e1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "log/slog" "os" "path" @@ -11,65 +10,66 @@ import ( "github.com/ublue-os/bext/cmd/layer" "github.com/ublue-os/bext/cmd/mount" "github.com/ublue-os/bext/internal" + "github.com/ublue-os/bext/pkg/logging" appLogging "github.com/ublue-os/bext/pkg/logging" ) var RootCmd = &cobra.Command{ - Use: "bext", - Short: "Manager for Systemd system extensions", - Long: `Manage your systemd system extensions from your CLI, managing their cache, multiple versions, and building.`, - SilenceUsage: true, + Use: "bext", + Short: "Manager for Systemd system extensions", + Long: `Manage your systemd system extensions from your CLI, managing their cache, multiple versions, and building.`, + PersistentPreRunE: initLogging, + SilenceUsage: true, } +var ( + fLogFile string + fLogLevel string + fNoLogging bool +) + func Execute() { - err := RootCmd.Execute() - if err != nil { + if err := RootCmd.Execute(); err != nil { + slog.Debug("Application exited with error", slog.String("errormsg", err.Error()), slog.Int("exitcode", 1)) os.Exit(1) } } -var ( - fLogFile *string - fLogLevel *string - fNoLogging *bool -) - -func init() { - fLogFile = RootCmd.PersistentFlags().String("log-file", "", "File where user facing logs will be written to") - fLogLevel = RootCmd.PersistentFlags().String("log-level", "info", "File where user facing logs will be written to") - fNoLogging = RootCmd.PersistentFlags().Bool("quiet", false, "Do not log anything to anywhere") - internal.Config.NoProgress = RootCmd.PersistentFlags().Bool("no-progress", false, "Do not use progress bars whenever they would be") - - RootCmd.AddCommand(layer.LayerCmd) - RootCmd.AddCommand(mount.MountCmd) - RootCmd.AddCommand(AddToPathCmd) - - if *fNoLogging { - slog.SetDefault(appLogging.NewMuteLogger()) - return - } +func initLogging(cmd *cobra.Command, args []string) error { var logWriter *os.File = os.Stdout - if *fLogFile != "" { - abs, err := filepath.Abs(path.Clean(*fLogFile)) + if fLogFile != "-" { + abs, err := filepath.Abs(path.Clean(fLogFile)) if err != nil { - os.Exit(1) + return err } - logWriter, err = os.Create(abs) + logWriter, err = os.OpenFile(abs, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755) if err != nil { - fmt.Println("Could not open log file") - os.Exit(1) + return err } - defer logWriter.Close() } - logLevel, err := appLogging.StrToLogLevel(*fLogLevel) + logLevel, err := appLogging.StrToLogLevel(fLogLevel) if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err.Error()) - os.Exit(1) + return err } - main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, *fLogFile != "")) + main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, fLogFile != "-")) + + if fNoLogging { + slog.SetDefault(logging.NewMuteLogger()) + } else { + slog.SetDefault(main_app_logger) + } + return nil +} - slog.SetDefault(main_app_logger) +func init() { + RootCmd.PersistentFlags().StringVar(&fLogFile, "log-file", "-", "File where user-facing logs will be written to") + RootCmd.PersistentFlags().StringVar(&fLogLevel, "log-level", "info", "Log level for user-facing logs") + RootCmd.PersistentFlags().BoolVar(&fNoLogging, "quiet", false, "Do not log anything to anywhere") + internal.Config.NoProgress = RootCmd.PersistentFlags().Bool("no-progress", false, "Do not use progress bars whenever they would be") + RootCmd.AddCommand(layer.LayerCmd) + RootCmd.AddCommand(mount.MountCmd) + RootCmd.AddCommand(AddToPathCmd) }