expose real zap logger to main package

This commit is contained in:
Alex Goodman
2020-05-26 08:21:49 -04:00
parent 1b9effeb49
commit bd09327d40
3 changed files with 27 additions and 21 deletions
+6 -4
View File
@@ -7,14 +7,15 @@ import (
"github.com/anchore/imgbom/imgbom"
"github.com/anchore/imgbom/internal/config"
"github.com/anchore/imgbom/internal/format"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/imgbom/internal/logger"
"github.com/anchore/stereoscope"
"github.com/spf13/viper"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
var appConfig *config.Application
var log *zap.SugaredLogger
func initAppConfig() {
cfg, err := config.LoadConfigFromFile(viper.GetViper(), &cliOpts)
@@ -34,9 +35,10 @@ func initLogging() {
FileLocation: appConfig.Log.FileLocation,
}
appLogger := logger.NewZapLogger(config)
imgbom.SetLogger(appLogger)
stereoscope.SetLogger(appLogger)
logWrapper := logger.NewZapLogger(config)
log = logWrapper.Logger
imgbom.SetLogger(logWrapper)
stereoscope.SetLogger(logWrapper)
}
func logAppConfig() {
+10 -6
View File
@@ -7,7 +7,6 @@ import (
"github.com/anchore/imgbom/imgbom"
"github.com/anchore/imgbom/imgbom/presenter"
"github.com/anchore/imgbom/internal"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope"
"github.com/spf13/cobra"
)
@@ -24,7 +23,7 @@ Supports the following image sources:
"appName": internal.ApplicationName,
}),
Args: cobra.MaximumNArgs(1),
Run: doRunCmd,
Run: runCmdWrapper,
}
func init() {
@@ -42,13 +41,17 @@ func Execute() {
}
}
func doRunCmd(cmd *cobra.Command, args []string) {
func runCmdWrapper(cmd *cobra.Command, args []string) {
os.Exit(doRunCmd(cmd, args))
}
func doRunCmd(cmd *cobra.Command, args []string) int {
userImageStr := args[0]
log.Infof("Fetching image '%s'", userImageStr)
img, err := stereoscope.GetImage(userImageStr)
if err != nil {
log.Errorf("could not fetch image '%s': %w", userImageStr, err)
os.Exit(1)
return 1
}
defer stereoscope.Cleanup()
@@ -56,13 +59,14 @@ func doRunCmd(cmd *cobra.Command, args []string) {
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
if err != nil {
log.Errorf("could not catalog image: %w", err)
os.Exit(1)
return 1
}
log.Info("Complete!")
err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog)
if err != nil {
log.Errorf("could not format catalog results: %w", err)
os.Exit(1)
return 1
}
return 0
}