forked from oceanc80/jira-stalebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (88 loc) · 2.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"context"
"os"
"os/signal"
"syscall"
jira "github.com/andygrunwald/go-jira/v2/onpremise"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/joelanford/jira-stalebot/internal/stalebot"
)
var (
zapLevel = zap.NewAtomicLevel()
)
func setupLogger() logr.Logger {
zCfg := zap.NewProductionConfig()
if (isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())) ||
(isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())) ||
(isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) {
zCfg = zap.NewDevelopmentConfig()
}
zCfg.Level = zapLevel
z, err := zCfg.Build(zap.AddStacktrace(zapcore.DPanicLevel))
if err != nil {
panic(err)
}
return zapr.NewLogger(z)
}
func main() {
log := setupLogger()
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if err := rootCmd(log).ExecuteContext(ctx); err != nil {
exitError(log, "execution error", err)
}
}
func rootCmd(log logr.Logger) *cobra.Command {
var (
configFile string
dryRun bool
verbosity uint
skipPrompt bool
)
cmd := &cobra.Command{
Use: "jira-stalebot",
Run: func(cmd *cobra.Command, args []string) {
zapLevel.SetLevel(-zapcore.Level(verbosity))
setupLog := log.WithName("setup")
pat, err := stalebot.LoadPersonalAccessToken()
if err != nil {
exitError(setupLog, "load personal access token", err)
}
cfg, err := stalebot.LoadConfig(configFile)
if err != nil {
exitError(setupLog, "load stalebot config", err)
}
tp := &jira.PATAuthTransport{Token: pat}
cl, err := jira.NewClient(cfg.JiraBaseURL, tp.Client())
if err != nil {
exitError(setupLog, "create jira client", err)
}
stalebotLog := log.WithName("stalebot")
bot := stalebot.Stalebot{
Client: cl,
Config: *cfg,
DryRun: dryRun,
Prompt: !skipPrompt,
Logger: stalebotLog,
}
if err := bot.Run(cmd.Context()); err != nil {
exitError(stalebotLog, "run stalebot", err)
}
},
}
cmd.Flags().StringVar(&configFile, "config", "config.yaml", "Stalebot config file")
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Dry run (don't make any changes)")
cmd.Flags().UintVarP(&verbosity, "verbosity", "v", 0, "Log verbosity (higher number is more verbose)")
cmd.Flags().BoolVarP(&skipPrompt, "yes", "y", false, "skip confirmation prompts for operations")
return cmd
}
func exitError(l logr.Logger, msg string, err error) {
l.Error(err, msg)
os.Exit(1)
}