From d52709807a91fcf93a515daca8f79dac850f6fd3 Mon Sep 17 00:00:00 2001 From: xiekeyang Date: Fri, 21 Oct 2016 17:35:33 +0800 Subject: [PATCH] add default log level Signed-off-by: xiekeyang --- cmd/oci-create-runtime-bundle/main.go | 29 ++++++++++++++++++++------- cmd/oci-image-validate/main.go | 23 ++++++++++++++++----- cmd/oci-unpack/main.go | 27 +++++++++++++++++++------ 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/cmd/oci-create-runtime-bundle/main.go b/cmd/oci-create-runtime-bundle/main.go index c517d09..ec09cb1 100644 --- a/cmd/oci-create-runtime-bundle/main.go +++ b/cmd/oci-create-runtime-bundle/main.go @@ -20,6 +20,7 @@ import ( "os" "strings" + "github.com/Sirupsen/logrus" specs "github.com/opencontainers/image-spec/specs-go" "github.com/opencontainers/image-tools/image" "github.com/spf13/cobra" @@ -36,12 +37,13 @@ var bundleTypes = []string{ } type bundleCmd struct { - stdout *log.Logger - stderr *log.Logger - typ string // the type to bundle, can be empty string - ref string - root string - version bool + stdout *log.Logger + stderr *log.Logger + typ string // the type to bundle, can be empty string + ref string + root string + version bool + logLevelString string } func main() { @@ -91,10 +93,24 @@ It is strongly recommended to keep the default value.`, &v.version, "version", false, `Print version information and exit`, ) + + cmd.Flags().StringVar( + &v.logLevelString, "log-level", "error", + `Log level (panic, fatal, error, warn, info, or debug)`, + ) + return cmd } func (v *bundleCmd) Run(cmd *cobra.Command, args []string) { + var err error + + logLevel, err := logrus.ParseLevel(v.logLevelString) + if err != nil { + logrus.Fatalf("%s", err) + } + logrus.SetLevel(logLevel) + if v.version { v.stdout.Printf("commit: %s", gitCommit) v.stdout.Printf("spec: %s", specs.Version) @@ -123,7 +139,6 @@ func (v *bundleCmd) Run(cmd *cobra.Command, args []string) { v.typ = typ } - var err error switch v.typ { case image.TypeImageLayout: err = image.CreateRuntimeBundleLayout(args[0], args[1], v.ref, v.root) diff --git a/cmd/oci-image-validate/main.go b/cmd/oci-image-validate/main.go index 5143b63..fd58410 100644 --- a/cmd/oci-image-validate/main.go +++ b/cmd/oci-image-validate/main.go @@ -20,6 +20,7 @@ import ( "os" "strings" + "github.com/Sirupsen/logrus" "github.com/opencontainers/image-spec/schema" specs "github.com/opencontainers/image-spec/specs-go" "github.com/opencontainers/image-tools/image" @@ -41,11 +42,12 @@ var validateTypes = []string{ } type validateCmd struct { - stdout *log.Logger - stderr *log.Logger - typ string // the type to validate, can be empty string - refs []string - version bool + stdout *log.Logger + stderr *log.Logger + typ string // the type to validate, can be empty string + refs []string + version bool + logLevelString string } func main() { @@ -89,10 +91,21 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command { `Print version information and exit`, ) + cmd.Flags().StringVar( + &v.logLevelString, "log-level", "error", + `Log level (panic, fatal, error, warn, info, or debug)`, + ) + return cmd } func (v *validateCmd) Run(cmd *cobra.Command, args []string) { + logLevel, err := logrus.ParseLevel(v.logLevelString) + if err != nil { + logrus.Fatalf("%s", err) + } + logrus.SetLevel(logLevel) + if v.version { v.stdout.Printf("commit: %s", gitCommit) v.stdout.Printf("spec: %s", specs.Version) diff --git a/cmd/oci-unpack/main.go b/cmd/oci-unpack/main.go index ed0ffa7..283e35d 100644 --- a/cmd/oci-unpack/main.go +++ b/cmd/oci-unpack/main.go @@ -20,6 +20,7 @@ import ( "os" "strings" + "github.com/Sirupsen/logrus" specs "github.com/opencontainers/image-spec/specs-go" "github.com/opencontainers/image-tools/image" "github.com/spf13/cobra" @@ -36,11 +37,12 @@ var unpackTypes = []string{ } type unpackCmd struct { - stdout *log.Logger - stderr *log.Logger - typ string // the type to unpack, can be empty string - ref string - version bool + stdout *log.Logger + stderr *log.Logger + typ string // the type to unpack, can be empty string + ref string + version bool + logLevelString string } func main() { @@ -83,10 +85,24 @@ func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command { &v.version, "version", false, `Print version information and exit`, ) + + cmd.Flags().StringVar( + &v.logLevelString, "log-level", "error", + `Log level (panic, fatal, error, warn, info, or debug)`, + ) + return cmd } func (v *unpackCmd) Run(cmd *cobra.Command, args []string) { + var err error + + logLevel, err := logrus.ParseLevel(v.logLevelString) + if err != nil { + logrus.Fatalf("%s", err) + } + logrus.SetLevel(logLevel) + if v.version { v.stdout.Printf("commit: %s", gitCommit) v.stdout.Printf("spec: %s", specs.Version) @@ -110,7 +126,6 @@ func (v *unpackCmd) Run(cmd *cobra.Command, args []string) { v.typ = typ } - var err error switch v.typ { case image.TypeImageLayout: err = image.UnpackLayout(args[0], args[1], v.ref)