nitter/main.go

77 lines
1.7 KiB
Go
Raw Normal View History

2023-03-08 08:05:13 +00:00
package main
import (
2023-03-08 13:40:04 +00:00
"context"
"flag"
2023-03-08 08:05:13 +00:00
"os"
2023-03-08 13:40:04 +00:00
"os/signal"
2023-03-08 08:05:13 +00:00
2023-03-08 13:40:04 +00:00
"github.com/spf13/cobra"
"golang.org/x/exp/slog"
2023-03-08 13:40:04 +00:00
"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)))
},
}
)
2023-03-08 08:05:13 +00:00
func main() {
slogOptions := slog.HandlerOptions{
AddSource: true,
Level: slog.LevelInfo,
}
slog.SetDefault(slog.New(slogOptions.NewTextHandler(os.Stderr)))
root.PersistentFlags().AddGoFlagSet(prepareLoggingFlags())
2023-03-08 13:40:04 +00:00
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())
2023-03-08 08:05:13 +00:00
2023-03-08 13:40:04 +00:00
if err := root.ExecuteContext(ctx); err != nil {
slog.Error("Error occurred during exeuction", err)
os.Exit(1)
2023-03-08 08:05:13 +00:00
}
}
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
}