diff --git a/cmd/bom/main.go b/cmd/bom/main.go index b75fafc..9ac538b 100644 --- a/cmd/bom/main.go +++ b/cmd/bom/main.go @@ -2,24 +2,17 @@ package main import ( "os" - "time" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "github.com/sirupsen/logrus" + zlog "github.com/rs/zerolog/log" "stackerbuild.io/stacker-bom/pkg/cli" + "stackerbuild.io/stacker-bom/pkg/log" ) func main() { - // setup logging - logrus.SetLevel(logrus.ErrorLevel) - - zerolog.TimeFieldFormat = time.RFC3339 - zerolog.SetGlobalLevel(zerolog.InfoLevel) - log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr}) + log.SetLevel(log.InfoLevel) if err := cli.NewRootCmd().Execute(); err != nil { - log.Error().Err(err).Msg("action failed") + zlog.Error().Err(err).Msg("action failed") os.Exit(1) } } diff --git a/pkg/bom/doc.go b/pkg/bom/doc.go index 0e3bd22..1597481 100644 --- a/pkg/bom/doc.go +++ b/pkg/bom/doc.go @@ -66,6 +66,8 @@ func MergeDocuments(dir, name, author, organization, output string) error { sdoc.Creator.Organization = organization sdoc.Creator.Tool = []string{fmt.Sprintf("stackerbuild.io/stacker-bom@%s", buildgen.Commit)} + mcount := 0 + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -85,6 +87,9 @@ func MergeDocuments(dir, name, author, organization, output string) error { sdoc.Files = MergeMaps(sdoc.Files, doc.Files) sdoc.Packages = MergeMaps(sdoc.Packages, doc.Packages) + log.Info().Str("path", path).Msg("file found for merging") + mcount++ + return nil }) if err != nil { @@ -93,10 +98,14 @@ func MergeDocuments(dir, name, author, organization, output string) error { return err } - if err := WriteDocument(sdoc, output); err != nil { - log.Error().Err(err).Str("name", output).Msg("unable to write merged doc") + if mcount > 0 { + if err := WriteDocument(sdoc, output); err != nil { + log.Error().Err(err).Str("name", output).Msg("unable to write merged doc") - return err + return err + } + + log.Info().Int("files", mcount).Str("dir", dir).Str("output", output).Msg("merged files") } return nil diff --git a/pkg/log/log.go b/pkg/log/log.go new file mode 100644 index 0000000..c19dc49 --- /dev/null +++ b/pkg/log/log.go @@ -0,0 +1,32 @@ +package log + +import ( + "os" + "time" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/sirupsen/logrus" +) + +type Level int + +const ( + // DebugLevel defines debug log level. + DebugLevel Level = iota + // InfoLevel defines info log level. + InfoLevel + // WarnLevel defines warn log level. + WarnLevel + // ErrorLevel defines error log level. + ErrorLevel +) + +func SetLevel(level Level) { + // setup logging + logrus.SetLevel(logrus.ErrorLevel) + + zerolog.TimeFieldFormat = time.RFC3339 + zerolog.SetGlobalLevel(zerolog.Level(level)) + log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr}) +}