grype/cmd/cmd.go

135 lines
2.9 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"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wagoodman/go-partybus"
2022-11-03 16:19:03 +00:00
anchoreLogger "github.com/anchore/go-logger"
"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"
2020-07-24 01:29:05 +00:00
"github.com/anchore/grype/internal/logger"
"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() {
2022-11-03 16:19:03 +00:00
enableConsole := (appConfig.Log.FileLocation == "" || appConfig.CliOptions.Verbosity > 0) && !appConfig.Quiet
2020-08-01 15:58:10 +00:00
cfg := logger.LogrusConfig{
2022-11-03 16:19:03 +00:00
EnableConsole: enableConsole,
2020-05-26 14:37:28 +00:00
EnableFile: appConfig.Log.FileLocation != "",
Level: appConfig.Log.LevelOpt,
Structured: appConfig.Log.Structured,
FileLocation: appConfig.Log.FileLocation,
}
2020-08-01 15:58:10 +00:00
logWrapper := logger.NewLogrusLogger(cfg)
2020-07-24 01:29:05 +00:00
grype.SetLogger(logWrapper)
2020-08-01 15:58:10 +00:00
2022-11-03 16:19:03 +00:00
// TODO: separate syft logger config until grype consumes new logger
syftLoggerCfg := logrus.Config{
EnableConsole: enableConsole,
Level: anchoreLogger.Level(appConfig.Log.LevelOpt.String()),
}
lw, err := logrus.New(syftLoggerCfg)
if err != nil {
panic(err)
}
syft.SetLogger(lw)
2020-08-01 15:58:10 +00:00
stereoscope.SetLogger(&logger.LogrusNestedLogger{
Logger: logWrapper.Logger.WithField("from-lib", "stereoscope"),
2020-08-01 15:58:10 +00:00
})
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)
}