Skip to content

Commit

Permalink
easily print messages by logrus
Browse files Browse the repository at this point in the history
Signed-off-by: xiekeyang <xiekeyang@huawei.com>
  • Loading branch information
xiekeyang committed Nov 21, 2016
1 parent 0ebdfc9 commit 9ae60ce
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 92 deletions.
41 changes: 14 additions & 27 deletions cmd/oci-create-runtime-bundle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"log"
"os"
"strings"

Expand All @@ -37,8 +36,6 @@ 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
Expand All @@ -47,21 +44,14 @@ type bundleCmd struct {
}

func main() {
stdout := log.New(os.Stdout, "", 0)
stderr := log.New(os.Stderr, "", 0)

cmd := newBundleCmd(stdout, stderr)
cmd := newBundleCmd()
if err := cmd.Execute(); err != nil {
stderr.Println(err)
os.Exit(1)
logrus.Fatal(err)
}
}

func newBundleCmd(stdout, stderr *log.Logger) *cobra.Command {
v := &bundleCmd{
stdout: stdout,
stderr: stderr,
}
func newBundleCmd() *cobra.Command {
v := &bundleCmd{}

cmd := &cobra.Command{
Use: "oci-create-runtime-bundle [src] [dest]",
Expand Down Expand Up @@ -107,34 +97,32 @@ func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {

logLevel, err := logrus.ParseLevel(v.logLevelString)
if err != nil {
logrus.Fatalf("%s", err)
logrus.Fatal(err)
}
logrus.SetLevel(logLevel)

if v.version {
v.stdout.Printf("commit: %s", gitCommit)
v.stdout.Printf("spec: %s", specs.Version)
fmt.Printf("commit: %s\n", gitCommit)
fmt.Printf("spec: %s\n", specs.Version)
os.Exit(0)

}
if len(args) != 2 {
v.stderr.Print("both src and dest must be provided")
if err := cmd.Usage(); err != nil {
v.stderr.Println(err)
logrus.WithError(err).Errorf("usage failed")
}
os.Exit(1)
logrus.Fatal("both src and dest must be provided")
}

if _, err := os.Stat(args[1]); os.IsNotExist(err) {
v.stderr.Printf("destination path %s does not exist", args[1])
os.Exit(1)
_, err = os.Stat(args[1])
if os.IsNotExist(err) {
logrus.Fatalf("destination path %s does not exist", args[1])
}

if v.typ == "" {
typ, err := image.Autodetect(args[0])
if err != nil {
v.stderr.Printf("%q: autodetection failed: %v", args[0], err)
os.Exit(1)
logrus.Fatalf("%q: autodetection failed: %v", args[0], err)
}
v.typ = typ
}
Expand All @@ -152,8 +140,7 @@ func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {
}

if err != nil {
v.stderr.Printf("creating failed: %v", err)
os.Exit(1)
logrus.Fatalf("creating failed: %v", err)
}

os.Exit(0)
Expand Down
43 changes: 16 additions & 27 deletions cmd/oci-image-validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"log"
"os"
"strings"

Expand All @@ -42,30 +41,21 @@ 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
logLevelString string
}

func main() {
stdout := log.New(os.Stdout, "", 0)
stderr := log.New(os.Stderr, "", 0)

cmd := newValidateCmd(stdout, stderr)
cmd := newValidateCmd()
if err := cmd.Execute(); err != nil {
stderr.Println(err)
os.Exit(1)
logrus.Fatal(err)
}
}

func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
v := &validateCmd{
stdout: stdout,
stderr: stderr,
}
func newValidateCmd() *cobra.Command {
v := &validateCmd{}

cmd := &cobra.Command{
Use: "oci-image-validate FILE...",
Expand Down Expand Up @@ -102,48 +92,47 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
func (v *validateCmd) Run(cmd *cobra.Command, args []string) {
logLevel, err := logrus.ParseLevel(v.logLevelString)
if err != nil {
logrus.Fatalf("%s", err)
logrus.Fatal(err)
}
logrus.SetLevel(logLevel)

if v.version {
v.stdout.Printf("commit: %s", gitCommit)
v.stdout.Printf("spec: %s", specs.Version)
fmt.Printf("commit: %s\n", gitCommit)
fmt.Printf("spec: %s\n", specs.Version)
os.Exit(0)
}

if len(args) < 1 {
v.stderr.Printf("no files specified")
if err := cmd.Usage(); err != nil {
v.stderr.Println(err)
logrus.WithError(err).Errorf("usage failed")
}
os.Exit(1)
logrus.Fatal("no files specified")
}

var exitcode int
for _, arg := range args {
err := v.validatePath(arg)

if err == nil {
v.stdout.Printf("%s: OK", arg)
logrus.Infof("%s: OK", arg)
continue
}

var errs []error
if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
errs = verr.Errs
} else if serr, ok := errors.Cause(err).(*schema.SyntaxError); ok {
v.stderr.Printf("%s:%d:%d: validation failed: %v", arg, serr.Line, serr.Col, err)
logrus.WithError(err).Errorf("%s:%d:%d: validation failed", arg, serr.Line, serr.Col)
exitcode = 1
continue
} else {
v.stderr.Printf("%s: validation failed: %v", arg, err)
logrus.WithError(err).Errorf("%s: validation failed", arg)
exitcode = 1
continue
}

for _, err := range errs {
v.stderr.Printf("%s: validation failed: %v", arg, err)
logrus.WithError(err).Errorf("%s: validation failed", arg)
}

exitcode = 1
Expand All @@ -166,13 +155,13 @@ func (v *validateCmd) validatePath(name string) error {

switch typ {
case image.TypeImageLayout:
return image.ValidateLayout(name, v.refs, v.stdout)
return image.ValidateLayout(name, v.refs)
case image.TypeImage:
return image.Validate(name, v.refs, v.stdout)
return image.Validate(name, v.refs)
}

if len(v.refs) != 0 {
fmt.Printf("WARNING: type %q does not support refs, which are only appropriate if type is image or imageLayout.\n", typ)
logrus.Warnf("type %q does not support refs, which are only appropriate if type is image or imageLayout.\n", typ)
}

f, err := os.Open(name)
Expand Down
35 changes: 11 additions & 24 deletions cmd/oci-unpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"log"
"os"
"strings"

Expand All @@ -37,30 +36,21 @@ 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
logLevelString string
}

func main() {
stdout := log.New(os.Stdout, "", 0)
stderr := log.New(os.Stderr, "", 0)

cmd := newUnpackCmd(stdout, stderr)
cmd := newUnpackCmd()
if err := cmd.Execute(); err != nil {
stderr.Println(err)
os.Exit(1)
logrus.Fatal(err)
}
}

func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command {
v := &unpackCmd{
stdout: stdout,
stderr: stderr,
}
func newUnpackCmd() *cobra.Command {
v := &unpackCmd{}

cmd := &cobra.Command{
Use: "unpack [src] [dest]",
Expand Down Expand Up @@ -99,29 +89,27 @@ func (v *unpackCmd) Run(cmd *cobra.Command, args []string) {

logLevel, err := logrus.ParseLevel(v.logLevelString)
if err != nil {
logrus.Fatalf("%s", err)
logrus.Fatal(err)
}
logrus.SetLevel(logLevel)

if v.version {
v.stdout.Printf("commit: %s", gitCommit)
v.stdout.Printf("spec: %s", specs.Version)
fmt.Printf("commit: %s\n", gitCommit)
fmt.Printf("spec: %s\n", specs.Version)
os.Exit(0)
}

if len(args) != 2 {
v.stderr.Print("both src and dest must be provided")
if err := cmd.Usage(); err != nil {
v.stderr.Println(err)
logrus.WithError(err).Errorf("usage failed")
}
os.Exit(1)
logrus.Fatal("both src and dest must be provided")
}

if v.typ == "" {
typ, err := image.Autodetect(args[0])
if err != nil {
v.stderr.Printf("%q: autodetection failed: %v", args[0], err)
os.Exit(1)
logrus.Fatalf("%q: autodetection failed: %v", args[0], err)
}
v.typ = typ
}
Expand All @@ -138,8 +126,7 @@ func (v *unpackCmd) Run(cmd *cobra.Command, args []string) {
}

if err != nil {
v.stderr.Printf("unpacking failed: %v", err)
os.Exit(1)
logrus.Fatalf("unpacking failed: %v", err)
}

os.Exit(0)
Expand Down
22 changes: 9 additions & 13 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,44 @@ package image
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"

"github.com/Sirupsen/logrus"
"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

// ValidateLayout walks through the given file tree and validates the manifest
// pointed to by the given refs or returns an error if the validation failed.
func ValidateLayout(src string, refs []string, out *log.Logger) error {
return validate(newPathWalker(src), refs, out)
func ValidateLayout(src string, refs []string) error {
return validate(newPathWalker(src), refs)
}

// Validate walks through the given .tar file and validates the manifest
// pointed to by the given refs or returns an error if the validation failed.
func Validate(tarFile string, refs []string, out *log.Logger) error {
func Validate(tarFile string, refs []string) error {
f, err := os.Open(tarFile)
if err != nil {
return errors.Wrap(err, "unable to open file")
}
defer f.Close()

return validate(newTarWalker(tarFile, f), refs, out)
return validate(newTarWalker(tarFile, f), refs)
}

var validRefMediaTypes = []string{
v1.MediaTypeImageManifest,
v1.MediaTypeImageManifestList,
}

func validate(w walker, refs []string, out *log.Logger) error {
func validate(w walker, refs []string) error {
ds, err := listReferences(w)
if err != nil {
return err
}
if len(refs) == 0 && len(ds) == 0 {
// TODO(runcom): ugly, we'll need a better way and library
// to express log levels.
// see /~https://github.com/opencontainers/image-spec/issues/288
out.Print("WARNING: no descriptors found")
logrus.Warnf("no descriptors found")
}

if len(refs) == 0 {
Expand Down Expand Up @@ -87,9 +84,8 @@ func validate(w walker, refs []string, out *log.Logger) error {
if err := m.validate(w); err != nil {
return err
}
if out != nil {
out.Printf("reference %q: OK", ref)
}

logrus.Infof("reference %q: OK", ref)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestValidateLayout(t *testing.T) {
t.Fatal(err)
}

err = ValidateLayout(root, []string{refTag}, nil)
err = ValidateLayout(root, []string{refTag})
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 9ae60ce

Please sign in to comment.