-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsa.go
79 lines (65 loc) · 1.5 KB
/
hsa.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
package main
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/ppxl/harbor-space-analyzer/cmd"
"github.com/ppxl/harbor-space-analyzer/logging"
)
const flagLogLevel = "log-level"
var (
version string
log = logging.GetInstance()
osExit anExit = &realOsExit{}
)
func configureLogging(cliCtx *cli.Context) error {
logLevelStr := cliCtx.String(flagLogLevel)
logLevel, err := logrus.ParseLevel(logLevelStr)
if err != nil {
return fmt.Errorf("failed to parse log level %s: %w", logLevelStr, err)
}
err = logging.Init(logLevel)
if err != nil {
return fmt.Errorf("failed to initialize logger: %w", err)
}
return nil
}
func main() {
app := cli.NewApp()
app.Version = version
app.Name = "hsa"
app.Usage = "analyze a harbor instance in terms of space"
app.Commands = []*cli.Command{cmd.Analyze}
app.HideHelpCommand = true
app.Flags = parseGlobalFlags()
app.Before = func(cliCtx *cli.Context) error {
err := configureLogging(cliCtx)
exitOnError(err)
return nil
}
err := app.Run(os.Args)
exitOnError(err)
}
func parseGlobalFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: flagLogLevel,
Usage: "a log level of lower than error will produce all kind of silly output",
Value: "error",
},
}
}
func exitOnError(err error) {
if err != nil {
fmt.Printf("%#v\n", err)
osExit.Exit(1)
}
}
type anExit interface {
Exit(exitCode int)
}
type realOsExit struct{}
func (ex *realOsExit) Exit(exitCode int) {
os.Exit(exitCode)
}