2023-03-08 08:05:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-08 13:40:04 +00:00
|
|
|
"context"
|
2023-03-09 13:28:33 +00:00
|
|
|
"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"
|
2023-03-09 13:28:33 +00:00
|
|
|
"golang.org/x/exp/slog"
|
2023-03-08 13:40:04 +00:00
|
|
|
|
|
|
|
"code.icb4dc0.de/prskr/nitter/internal/commands"
|
|
|
|
)
|
|
|
|
|
2023-03-09 13:28:33 +00:00
|
|
|
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() {
|
2023-03-09 13:28:33 +00:00
|
|
|
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 {
|
2023-03-09 13:28:33 +00:00
|
|
|
slog.Error("Error occurred during exeuction", err)
|
|
|
|
os.Exit(1)
|
2023-03-08 08:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 13:28:33 +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
|
|
|
|
}
|