-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcf.go
116 lines (92 loc) · 2.73 KB
/
hcf.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
110
111
112
113
114
115
116
package main
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/ppxl/harbor-cve-finder/cmd"
"github.com/ppxl/harbor-cve-finder/logging"
)
const flagLogLevel = "log-level"
// the go compiler sets these values, these don't need to be public
var (
version string
goVersion string
)
var (
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.ExtraInfo = func() map[string]string {
return map[string]string{"Go version": goVersion}
}
app.Name = "hcf"
app.Usage = "find CVEs for certain packages in harbor trivy reports"
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
}
app.CustomAppHelpTemplate = createCustomAppHelpTemplate()
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("%s\n", err.Error())
osExit.Exit(1)
}
}
type anExit interface {
Exit(exitCode int)
}
type realOsExit struct{}
func (ex *realOsExit) Exit(exitCode int) {
os.Exit(exitCode)
}
func createCustomAppHelpTemplate() string {
return `NAME:
{{template "helpNameTemplate" .}}
USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
VERSION:
app version: {{.Version}}{{range $key, $value := ExtraInfo}}
{{$key}}: {{$value}}{{end}}{{end}}{{end}}{{if .Description}}
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}
{{- if len .Authors}}
AUTHOR{{template "authorsTemplate" .}}{{end}}{{if .VisibleCommands}}
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
GLOBAL OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
GLOBAL OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .Copyright}}
COPYRIGHT:
{{template "copyrightTemplate" .}}{{end}}
`
}