cmd

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2024 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "ficsit",
	Short: "cli mod manager for satisfactory",
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		viper.SetConfigName("config")
		viper.AddConfigPath(".")
		viper.SetEnvPrefix("ficsit")
		viper.AutomaticEnv()

		_ = viper.ReadInConfig()

		handlers := make([]slog.Handler, 0)
		if viper.GetBool("pretty") {
			pterm.EnableStyling()
		} else {
			pterm.DisableStyling()
		}

		const (
			ansiReset         = "\033[0m"
			ansiBold          = "\033[1m"
			ansiWhite         = "\033[38m"
			ansiBrightMagenta = "\033[95m"
		)

		level := slog.LevelInfo
		if err := (&level).UnmarshalText([]byte(viper.GetString("log"))); err != nil {
			return fmt.Errorf("failed parsing level: %w", err)
		}

		if !viper.GetBool("quiet") {
			handlers = append(handlers, tint.NewHandler(os.Stdout, &tint.Options{
				Level:      level,
				AddSource:  true,
				TimeFormat: time.RFC3339Nano,
				ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
					if attr.Key == slog.LevelKey {
						level := attr.Value.Any().(slog.Level)
						if level == slog.LevelDebug {
							attr.Value = slog.StringValue(ansiBrightMagenta + "DBG" + ansiReset)
						}
					} else if attr.Key == slog.MessageKey {
						attr.Value = slog.StringValue(ansiBold + ansiWhite + fmt.Sprint(attr.Value.Any()) + ansiReset)
					}
					return attr
				},
			}))
		}

		if viper.GetString("log-file") != "" {
			logFile, err := os.OpenFile(viper.GetString("log-file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o777)
			if err != nil {
				return fmt.Errorf("failed to open log file: %w", err)
			}

			handlers = append(handlers, slog.NewJSONHandler(logFile, &slog.HandlerOptions{}))
		}

		slog.SetDefault(slog.New(
			slogmulti.Fanout(handlers...),
		))

		return nil
	},
}

Functions

func Execute

func Execute(version string, commit string)

Types

This section is empty.

Directories

Path Synopsis
mod

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL