2020-05-26 14:37:28 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-12-21 20:23:37 +00:00
|
|
|
"encoding/json"
|
2020-05-26 14:37:28 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-12-21 20:23:37 +00:00
|
|
|
"sort"
|
2020-05-26 14:37:28 +00:00
|
|
|
|
2022-03-03 19:50:24 +00:00
|
|
|
"github.com/gookit/color"
|
2022-12-07 23:30:38 +00:00
|
|
|
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"
|
2021-10-01 17:03:50 +00:00
|
|
|
"github.com/anchore/grype/internal/log"
|
2021-12-21 20:23:37 +00:00
|
|
|
"github.com/anchore/grype/internal/version"
|
2020-08-01 15:58:10 +00:00
|
|
|
"github.com/anchore/stereoscope"
|
2020-07-24 01:26:03 +00:00
|
|
|
"github.com/anchore/syft/syft"
|
2020-05-26 14:37:28 +00:00
|
|
|
)
|
|
|
|
|
2021-10-01 17:03:50 +00:00
|
|
|
var (
|
|
|
|
appConfig *config.Application
|
|
|
|
eventBus *partybus.Bus
|
|
|
|
eventSubscription *partybus.Subscription
|
|
|
|
)
|
2020-05-26 17:31:50 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-07-21 16:34:39 +00:00
|
|
|
cobra.OnInitialize(
|
2021-10-01 17:03:50 +00:00
|
|
|
initRootCmdConfigOptions,
|
2020-07-21 16:34:39 +00:00
|
|
|
initAppConfig,
|
|
|
|
initLogging,
|
|
|
|
logAppConfig,
|
2021-12-21 20:23:37 +00:00
|
|
|
logAppVersion,
|
2020-07-30 23:06:27 +00:00
|
|
|
initEventBus,
|
2020-07-21 16:34:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:31:50 +00:00
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2021-12-03 16:43:50 +00:00
|
|
|
_ = stderrPrintLnf(err.Error())
|
2020-05-26 17:31:50 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 14:37:28 +00:00
|
|
|
|
2021-10-01 17:03:50 +00:00
|
|
|
func initRootCmdConfigOptions() {
|
|
|
|
if err := bindRootConfigOptions(rootCmd.Flags()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:37:28 +00:00
|
|
|
func initAppConfig() {
|
2021-10-01 17:03:50 +00:00
|
|
|
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() {
|
2022-12-07 23:30:38 +00:00
|
|
|
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,
|
2022-12-07 23:30:38 +00:00
|
|
|
Level: appConfig.Log.Level,
|
2020-05-26 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 23:30:38 +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
|
|
|
}
|
2022-12-07 23:30:38 +00:00
|
|
|
|
|
|
|
logWrapper, err := logrus.New(cfg)
|
2022-11-03 16:19:03 +00:00
|
|
|
if err != nil {
|
2022-12-07 23:30:38 +00:00
|
|
|
// 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
|
|
|
}
|
2022-12-07 23:30:38 +00:00
|
|
|
grype.SetLogger(logWrapper)
|
2023-05-08 21:03:14 +00:00
|
|
|
syft.SetLogger(logWrapper.Nested("from-lib", "syft"))
|
|
|
|
stereoscope.SetLogger(logWrapper.Nested("from-lib", "stereoscope"))
|
2020-05-26 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func logAppConfig() {
|
2021-10-01 17:03:50 +00:00
|
|
|
log.Debugf("application config:\n%+v", color.Magenta.Sprint(appConfig.String()))
|
2020-05-26 14:37:28 +00:00
|
|
|
}
|
2020-07-30 23:06:27 +00:00
|
|
|
|
2021-12-21 20:23:37 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:06:27 +00:00
|
|
|
func initEventBus() {
|
|
|
|
eventBus = partybus.NewBus()
|
|
|
|
eventSubscription = eventBus.Subscribe()
|
|
|
|
|
|
|
|
stereoscope.SetBus(eventBus)
|
|
|
|
syft.SetBus(eventBus)
|
|
|
|
grype.SetBus(eventBus)
|
|
|
|
}
|