package main import ( "context" "flag" "os" "os/signal" "github.com/spf13/cobra" "golang.org/x/exp/slog" "code.icb4dc0.de/prskr/nitter/internal/commands" ) var ( loggingConfig = struct { LogLevel *slog.LevelVar AddSource bool }{ LogLevel: new(slog.LevelVar), } root = &cobra.Command{ Use: "nitter", TraverseChildren: true, PersistentPreRun: func(_ *cobra.Command, _ []string) { slogOptions := slog.HandlerOptions{ AddSource: loggingConfig.AddSource, Level: loggingConfig.LogLevel.Level(), } slog.SetDefault(slog.New(slogOptions.NewTextHandler(os.Stderr))) }, } ) func main() { slogOptions := slog.HandlerOptions{ AddSource: true, Level: slog.LevelInfo, } slog.SetDefault(slog.New(slogOptions.NewTextHandler(os.Stderr))) root.PersistentFlags().AddGoFlagSet(prepareLoggingFlags()) root.PersistentFlags().StringP("namespace", "n", "", "Namespace a.k.a. organization/owner/group of the repository [$NITTER_NAMESPACE]") root.PersistentFlags().StringP("repo", "r", "", "Repo to interact with [$NITTER_REPO]") ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) defer cancel() root.AddCommand(commands.Gitea()) if err := root.ExecuteContext(ctx); err != nil { slog.Error("Error occurred during exeuction", err) os.Exit(1) } } func prepareLoggingFlags() *flag.FlagSet { flagSet := flag.NewFlagSet("logging", flag.PanicOnError) flagSet.TextVar( loggingConfig.LogLevel, "log-level", loggingConfig.LogLevel, "set log level", ) flagSet.BoolVar( &loggingConfig.AddSource, "add source line to log messages", false, "Enable to get detailed information where the log was produced", ) return flagSet }