Skip to content

Commit

Permalink
add default log level
Browse files Browse the repository at this point in the history
Signed-off-by: xiekeyang <xiekeyang@huawei.com>
  • Loading branch information
xiekeyang committed Oct 28, 2016
1 parent 18d19bc commit d527098
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
29 changes: 22 additions & 7 deletions cmd/oci-create-runtime-bundle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions cmd/oci-image-validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 21 additions & 6 deletions cmd/oci-unpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d527098

Please sign in to comment.