Skip to content

Commit

Permalink
feat(logging): add logging solution to cli package
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
  • Loading branch information
jpower432 committed Jun 1, 2022
1 parent 0413080 commit f326a4f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 20 deletions.
10 changes: 7 additions & 3 deletions cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (o *BuildOptions) Validate() error {
}

func (o *BuildOptions) Run(ctx context.Context) error {
_, _ = fmt.Fprintf(o.IOStreams.Out, "Using output directory %q\n", o.Output)
o.Logger.Debugf("Using output directory %q", o.Output)
userSpace, err := workspace.NewLocalWorkspace(o.RootDir)
if err != nil {
return err
Expand Down Expand Up @@ -136,7 +136,7 @@ func (o *BuildOptions) Run(ctx context.Context) error {
}

for path := range fileIndex {
_, _ = fmt.Fprintf(o.IOStreams.Out, "Adding node %s\n", path)
o.Logger.Infof("Adding node %s\n", path)
node := graph.NewNode(path)

perr := &parser.ErrInvalidFormat{}
Expand Down Expand Up @@ -224,7 +224,11 @@ func (o *BuildOptions) Run(ctx context.Context) error {
if err := os.Chdir(renderSpace.Path()); err != nil {
return err
}
defer os.Chdir(cwd)
defer func() {
if err := os.Chdir(cwd); err != nil {
o.Logger.Errorf("%v", err)
}
}()

descs, err := client.GatherDescriptors("", files...)
if err != nil {
Expand Down
42 changes: 27 additions & 15 deletions cli/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cli
import (
"context"
"fmt"
"io/ioutil"
"net/http/httptest"
"net/url"
"os"
"testing"

"github.com/google/go-containerregistry/pkg/registry"
"github.com/stretchr/testify/require"
"github.com/uor-framework/client/cli/log"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand Down Expand Up @@ -106,6 +108,10 @@ func TestBuildValidate(t *testing.T) {
}

func TestBuildRun(t *testing.T) {

testlogr, err := log.NewLogger(ioutil.Discard, "debug")
require.NoError(t, err)

server := httptest.NewServer(registry.New())
t.Cleanup(server.Close)
u, err := url.Parse(server.URL)
Expand All @@ -121,11 +127,13 @@ func TestBuildRun(t *testing.T) {
{
name: "Success/FlatWorkspace",
opts: &BuildOptions{
RootOptions: &RootOptions{IOStreams: genericclioptions.IOStreams{
Out: os.Stdout,
In: os.Stdin,
ErrOut: os.Stderr,
},
RootOptions: &RootOptions{
IOStreams: genericclioptions.IOStreams{
Out: os.Stdout,
In: os.Stdin,
ErrOut: os.Stderr,
},
Logger: testlogr,
},
Destination: fmt.Sprintf("%s/client-test:latest", u.Host),
RootDir: "testdata/flatworkspace",
Expand All @@ -135,11 +143,13 @@ func TestBuildRun(t *testing.T) {
{
name: "Success/MultiLevelWorkspace",
opts: &BuildOptions{
RootOptions: &RootOptions{IOStreams: genericclioptions.IOStreams{
Out: os.Stdout,
In: os.Stdin,
ErrOut: os.Stderr,
},
RootOptions: &RootOptions{
IOStreams: genericclioptions.IOStreams{
Out: os.Stdout,
In: os.Stdin,
ErrOut: os.Stderr,
},
Logger: testlogr,
},
Destination: fmt.Sprintf("%s/client-test:latest", u.Host),
RootDir: "testdata/multi-level-workspace",
Expand All @@ -149,11 +159,13 @@ func TestBuildRun(t *testing.T) {
{
name: "Success/UORParsing",
opts: &BuildOptions{
RootOptions: &RootOptions{IOStreams: genericclioptions.IOStreams{
Out: os.Stdout,
In: os.Stdin,
ErrOut: os.Stderr,
},
RootOptions: &RootOptions{
IOStreams: genericclioptions.IOStreams{
Out: os.Stdout,
In: os.Stdin,
ErrOut: os.Stderr,
},
Logger: testlogr,
},
Destination: fmt.Sprintf("%s/client-test:latest", u.Host),
RootDir: "testdata/uor-template",
Expand Down
58 changes: 58 additions & 0 deletions cli/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package log

import (
"io"

"github.com/sirupsen/logrus"
)

// Logger is an interface the defines logging methods.
type Logger interface {
Errorf(string, ...interface{})
Infof(string, ...interface{})
Warnf(string, ...interface{})
Debugf(string, ...interface{})
Fatalf(string, ...interface{})
}

type standardLogger struct {
logger *logrus.Logger
}

// NewLogger returns a new Logger.
func NewLogger(out io.Writer, level string) (Logger, error) {
lvl, err := logrus.ParseLevel(level)
if err != nil {
return nil, err
}
slogr := &standardLogger{
logger: &logrus.Logger{
Out: out,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: lvl,
},
}

return slogr, nil
}

func (l *standardLogger) Errorf(format string, args ...interface{}) {
l.logger.Logf(logrus.ErrorLevel, format, args...)
}

func (l *standardLogger) Infof(format string, args ...interface{}) {
l.logger.Logf(logrus.InfoLevel, format, args...)
}

func (l *standardLogger) Warnf(format string, args ...interface{}) {
l.logger.Logf(logrus.WarnLevel, format, args...)
}

func (l *standardLogger) Debugf(format string, args ...interface{}) {
l.logger.Logf(logrus.DebugLevel, format, args...)
}

func (l *standardLogger) Fatalf(format string, args ...interface{}) {
l.logger.Logf(logrus.FatalLevel, format, args...)
}
18 changes: 17 additions & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/util/templates"

"github.com/uor-framework/client/cli/log"
)

type RootOptions struct {
genericclioptions.IOStreams
IOStreams genericclioptions.IOStreams
LogLevel string
Logger log.Logger
}

var clientLong = templates.LongDesc(
Expand All @@ -32,11 +36,23 @@ func NewRootCmd() *cobra.Command {
Long: clientLong,
SilenceErrors: false,
SilenceUsage: false,
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
logger, err := log.NewLogger(o.IOStreams.Out, o.LogLevel)
if err != nil {
return err
}
o.Logger = logger
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}

f := cmd.PersistentFlags()
f.StringVarP(&o.LogLevel, "loglevel", "l", "info",
"Log level (debug, info, warn, error, fatal)")

cmd.AddCommand(NewBuildCmd(&o))

return cmd
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/go-containerregistry v0.5.1
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.8.2
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
Expand Down Expand Up @@ -73,7 +74,6 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
Expand Down

0 comments on commit f326a4f

Please sign in to comment.