From 47b34f5163bd8295ebe5440b79d7d9c31e94f686 Mon Sep 17 00:00:00 2001 From: Tobias Nehrlich Date: Tue, 17 Dec 2019 21:07:57 +0100 Subject: [PATCH] Add flag for log level --- cmd/cmd.go | 44 ++++++++++++++++++++++++++++++++++++++------ git/git.go | 2 +- main.go | 22 ---------------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 647151c..b5f6157 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,24 +1,35 @@ package cmd import ( - "github.com/appuio/image-cleanup/cleanup" + "os" + "github.com/appuio/image-cleanup/docker" "github.com/appuio/image-cleanup/git" "github.com/appuio/image-cleanup/openshift" "github.com/appuio/image-cleanup/version" "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" ) +// Options is a struct holding the options of the root command +type Options struct { + LogLevel string +} + // NewCleanupCommand creates the `image-cleanup` command func NewCleanupCommand() *cobra.Command { + o := Options{} cmds := &cobra.Command{ - Use: "image-cleanup", - Short: "image-cleanup cleans up docker images", - Long: "image-cleanup cleans up docker images.", - Run: runHelp, + Use: "image-cleanup", + Short: "image-cleanup cleans up docker images", + Long: "image-cleanup cleans up docker images.", + PersistentPreRun: o.init, + Run: runHelp, } - cmds.AddCommand(cleanup.NewCleanupCommand()) + cmds.PersistentFlags().StringVarP(&o.LogLevel, "verbosity", "v", "info", "Log level to use") + cmds.AddCommand(docker.NewTagCommand()) cmds.AddCommand(git.NewGitCommand()) cmds.AddCommand(openshift.NewImageStreamCleanupCommand()) @@ -30,3 +41,24 @@ func NewCleanupCommand() *cobra.Command { func runHelp(cmd *cobra.Command, args []string) { cmd.Help() } + +func (o *Options) init(cmd *cobra.Command, args []string) { + configureLogging(o.LogLevel) +} + +func configureLogging(logLevel string) { + + log.SetFormatter(&log.TextFormatter{ + FullTimestamp: true, + }) + + log.SetOutput(os.Stderr) + + level, err := log.ParseLevel(logLevel) + if err != nil { + log.WithField("error", err).Warn("Using info level.") + log.SetLevel(log.InfoLevel) + } else { + log.SetLevel(level) + } +} diff --git a/git/git.go b/git/git.go index 551f3e4..9c44d1d 100644 --- a/git/git.go +++ b/git/git.go @@ -48,7 +48,7 @@ func GetCommitHashes(repoPath string, commitLimit int) []string { for i := 0; i < commitLimit; i++ { commit, err := commitIter.Next() if err != nil { - log.WithError(err).Fatal("Could not get commit.") + log.WithError(err).Debug("Could not get commit.") } else { commitHashes = append(commitHashes, commit.Hash.String()) } diff --git a/main.go b/main.go index 44778cd..e4a1776 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,6 @@ import ( "github.com/appuio/image-cleanup/cmd" "github.com/appuio/image-cleanup/version" - - log "github.com/sirupsen/logrus" ) // CLI Version constants @@ -23,27 +21,7 @@ func main() { command := cmd.NewCleanupCommand() - configureLogging() - if err := command.Execute(); err != nil { os.Exit(1) } } - -func configureLogging() { - - log.SetFormatter(&log.TextFormatter{ - FullTimestamp: true, - }) - - log.SetOutput(os.Stderr) - - //TODO: To make this configurable via flag - level, err := log.ParseLevel("debug") - if err != nil { - log.WithField("error", err).Warn("Using info level.") - log.SetLevel(log.InfoLevel) - } else { - log.SetLevel(level) - } -}