-
Notifications
You must be signed in to change notification settings - Fork 512
/
cli.go
109 lines (91 loc) · 2.91 KB
/
cli.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 cli
import (
"os"
"runtime/debug"
"github.com/spf13/cobra"
"github.com/anchore/clio"
"github.com/anchore/grype/cmd/grype/cli/commands"
grypeHandler "github.com/anchore/grype/cmd/grype/cli/ui"
"github.com/anchore/grype/cmd/grype/internal/ui"
"github.com/anchore/grype/grype/vulnerability"
"github.com/anchore/grype/internal/bus"
"github.com/anchore/grype/internal/log"
"github.com/anchore/grype/internal/redact"
"github.com/anchore/stereoscope"
syftHandler "github.com/anchore/syft/cmd/syft/cli/ui"
"github.com/anchore/syft/syft"
)
func Application(id clio.Identification) clio.Application {
app, _ := create(id)
return app
}
func Command(id clio.Identification) *cobra.Command {
_, cmd := create(id)
return cmd
}
func create(id clio.Identification) (clio.Application, *cobra.Command) {
clioCfg := clio.NewSetupConfig(id).
WithGlobalConfigFlag(). // add persistent -c <path> for reading an application config from
WithGlobalLoggingFlags(). // add persistent -v and -q flags tied to the logging config
WithConfigInRootHelp(). // --help on the root command renders the full application config in the help text
WithUIConstructor(
// select a UI based on the logging configuration and state of stdin (if stdin is a tty)
func(cfg clio.Config) ([]clio.UI, error) {
noUI := ui.None(cfg.Log.Quiet)
if !cfg.Log.AllowUI(os.Stdin) || cfg.Log.Quiet {
return []clio.UI{noUI}, nil
}
return []clio.UI{
ui.New(cfg.Log.Quiet,
grypeHandler.New(grypeHandler.DefaultHandlerConfig()),
syftHandler.New(syftHandler.DefaultHandlerConfig()),
),
noUI,
}, nil
},
).
WithInitializers(
func(state *clio.State) error {
// clio is setting up and providing the bus, redact store, and logger to the application. Once loaded,
// we can hoist them into the internal packages for global use.
stereoscope.SetBus(state.Bus)
syft.SetBus(state.Bus)
bus.Set(state.Bus)
redact.Set(state.RedactStore)
log.Set(state.Logger)
syft.SetLogger(state.Logger)
stereoscope.SetLogger(state.Logger)
return nil
},
).
WithPostRuns(func(_ *clio.State, _ error) {
stereoscope.Cleanup()
})
app := clio.New(*clioCfg)
rootCmd := commands.Root(app)
// add sub-commands
rootCmd.AddCommand(
commands.DB(app),
commands.Completion(),
commands.Explain(app),
clio.VersionCommand(id, syftVersion, dbVersion),
)
return app, rootCmd
}
func syftVersion() (string, any) {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
log.Debug("unable to find the buildinfo section of the binary (syft version is unknown)")
return "", ""
}
for _, d := range buildInfo.Deps {
if d.Path == "github.com/anchore/syft" {
return "Syft Version", d.Version
}
}
log.Debug("unable to find 'github.com/anchore/syft' from the buildinfo section of the binary")
return "", ""
}
func dbVersion() (string, any) {
return "Supported DB Schema", vulnerability.SchemaVersion
}