-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (90 loc) · 2.58 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
103
104
105
106
107
108
109
package main
import (
"fmt"
"os"
"sort"
"github.com/comunidade-shallom/peristera/apps/cli/system"
"github.com/comunidade-shallom/peristera/pkg/config"
"github.com/comunidade-shallom/peristera/pkg/support"
"github.com/comunidade-shallom/peristera/pkg/support/errors"
"github.com/comunidade-shallom/peristera/pkg/support/logger"
"github.com/pterm/pterm"
zero "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
EnableBashCompletion: true,
Description: "Peristera - Telegram BOT",
Usage: "Peristera CLI",
Version: config.Version(),
Copyright: "https://github.com/comunidade-shallom",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Load configuration from",
DefaultText: fmt.Sprintf("%s/peristera.yml", support.GetBinDirPath()),
},
&cli.BoolFlag{
Name: "no-banner",
Usage: "hide initial banner",
DefaultText: "false",
},
&cli.StringFlag{
Name: "level",
Aliases: []string{"l"},
Usage: "define log level",
DefaultText: "info",
},
&cli.BoolFlag{
Name: "debug",
Usage: "enables debug level",
},
},
Commands: []*cli.Command{WorkerCmd, system.SystemCmd},
Before: beforeRun,
}
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println("Peristera - Telegram BOT")
fmt.Println("")
fmt.Println(config.VersionVerbose())
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.Run(os.Args); err != nil {
zero.Fatal().Err(err).Msg("Fail run application")
os.Exit(1)
}
}
func beforeRun(ctx *cli.Context) error {
pterm.Debug.Debugger = ctx.Bool("debug")
if !ctx.Bool("no-banner") {
pterm.DefaultHeader.
WithMargin(5). //nolint:gomnd
Println("Peristera CLI \n" + config.Version())
}
appConfig, err := config.Load(ctx.String("config"))
logLevel := ctx.String("level")
if pterm.Debug.Debugger {
appConfig.Logger.Level = "debug"
logLevel = "debug"
}
if appConfig.Logger.Debug(logLevel) {
appConfig.Debug = true
}
if err != nil {
e, ok := err.(errors.BusinessError) //nolint:errorlint
if ok && e.ErrorCode == config.ConfigFileWasCreated.ErrorCode {
zero.Warn().Msg(err.Error())
} else {
zero.Fatal().Err(err).Msg("Fail to load config")
return err
}
}
ctx.Context = appConfig.WithContext(ctx.Context)
logger.SetupLogger(appConfig, logLevel)
log := logger.Logger("", appConfig.Tags())
ctx.Context = log.WithContext(appConfig.WithContext(ctx.Context))
return nil
}