Skip to content

Commit

Permalink
Add flag for log level
Browse files Browse the repository at this point in the history
  • Loading branch information
anothertobi committed Dec 17, 2019
1 parent 3881e6c commit 47b34f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
44 changes: 38 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
22 changes: 0 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

0 comments on commit 47b34f5

Please sign in to comment.