grype/cmd/cmd.go

133 lines
3 KiB
Go
Raw Normal View History

2020-05-26 14:37:28 +00:00
package cmd
import (
"encoding/json"
2020-05-26 14:37:28 +00:00
"fmt"
"os"
"sort"
2020-05-26 14:37:28 +00:00
2022-03-03 19:50:24 +00:00
"github.com/gookit/color"
logrusUpstream "github.com/sirupsen/logrus"
2022-03-03 19:50:24 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wagoodman/go-partybus"
2022-11-03 16:19:03 +00:00
"github.com/anchore/go-logger/adapter/logrus"
2020-07-24 01:29:05 +00:00
"github.com/anchore/grype/grype"
"github.com/anchore/grype/internal/config"
"github.com/anchore/grype/internal/log"
"github.com/anchore/grype/internal/version"
2020-08-01 15:58:10 +00:00
"github.com/anchore/stereoscope"
"github.com/anchore/syft/syft"
2020-05-26 14:37:28 +00:00
)
var (
appConfig *config.Application
eventBus *partybus.Bus
eventSubscription *partybus.Subscription
)
2020-05-26 17:31:50 +00:00
func init() {
cobra.OnInitialize(
initRootCmdConfigOptions,
initAppConfig,
initLogging,
logAppConfig,
logAppVersion,
initEventBus,
)
}
2020-05-26 17:31:50 +00:00
func Execute() {
if err := rootCmd.Execute(); err != nil {
_ = stderrPrintLnf(err.Error())
2020-05-26 17:31:50 +00:00
os.Exit(1)
}
}
2020-05-26 14:37:28 +00:00
func initRootCmdConfigOptions() {
if err := bindRootConfigOptions(rootCmd.Flags()); err != nil {
panic(err)
}
}
2020-05-26 14:37:28 +00:00
func initAppConfig() {
cfg, err := config.LoadApplicationConfig(viper.GetViper(), persistentOpts)
2020-05-26 14:37:28 +00:00
if err != nil {
fmt.Printf("failed to load application config: \n\t%+v\n", err)
os.Exit(1)
}
appConfig = cfg
}
func initLogging() {
cfg := logrus.Config{
EnableConsole: (appConfig.Log.FileLocation == "" || appConfig.CliOptions.Verbosity > 0) && !appConfig.Quiet,
2020-05-26 14:37:28 +00:00
FileLocation: appConfig.Log.FileLocation,
Level: appConfig.Log.Level,
2020-05-26 14:37:28 +00:00
}
if appConfig.Log.Structured {
cfg.Formatter = &logrusUpstream.JSONFormatter{
TimestampFormat: "2006-01-02T15:04:05.000Z",
DisableTimestamp: false,
DisableHTMLEscape: false,
PrettyPrint: false,
}
2022-11-03 16:19:03 +00:00
}
logWrapper, err := logrus.New(cfg)
2022-11-03 16:19:03 +00:00
if err != nil {
// this is kinda circular, but we can't return an error... ¯\_(ツ)_/¯
// I'm going to leave this here in case we one day have a different default logger other than the "discard" logger
log.Error("unable to initialize logger: %+v", err)
return
2022-11-03 16:19:03 +00:00
}
grype.SetLogger(logWrapper)
syft.SetLogger(logWrapper.Nested("from-lib", "syft"))
stereoscope.SetLogger(logWrapper.Nested("from-lib", "stereoscope"))
2020-05-26 14:37:28 +00:00
}
func logAppConfig() {
log.Debugf("application config:\n%+v", color.Magenta.Sprint(appConfig.String()))
2020-05-26 14:37:28 +00:00
}
func logAppVersion() {
versionInfo := version.FromBuild()
log.Infof("grype version: %s", versionInfo.Version)
var fields map[string]interface{}
bytes, err := json.Marshal(versionInfo)
if err != nil {
return
}
err = json.Unmarshal(bytes, &fields)
if err != nil {
return
}
keys := make([]string, 0, len(fields))
for k := range fields {
keys = append(keys, k)
}
sort.Strings(keys)
for idx, field := range keys {
value := fields[field]
branch := "├──"
if idx == len(fields)-1 {
branch = "└──"
}
log.Debugf(" %s %s: %s", branch, field, value)
}
}
func initEventBus() {
eventBus = partybus.NewBus()
eventSubscription = eventBus.Subscribe()
stereoscope.SetBus(eventBus)
syft.SetBus(eventBus)
grype.SetBus(eventBus)
}