New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: logger for output & errors #48
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package calculator | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/aerogear/charmil/pkg/factory" | ||
"github.com/aerogear/charmil/pkg/logging" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Options struct { | ||
Logger func() (logging.Logger, error) | ||
} | ||
|
||
func RootCommand(f *factory.Factory) *cobra.Command { | ||
opts := &Options{ | ||
Logger: f.Logger, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "calc", | ||
Short: "Calculator", | ||
Example: "$ host calc add 2 3", | ||
SilenceUsage: true, | ||
} | ||
|
||
cmd.AddCommand(AddCmd(opts)) | ||
return cmd | ||
} | ||
|
||
func AddCmd(opts *Options) *cobra.Command { | ||
addCmd := &cobra.Command{ | ||
Use: "add", | ||
Short: "Add numbers", | ||
Long: `Adding the numbers`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fstatus, _ := cmd.Flags().GetBool("float") | ||
if fstatus { | ||
addFloat(args, opts) | ||
} else { | ||
addInt(args, opts) | ||
} | ||
}, | ||
} | ||
|
||
addCmd.Flags().BoolP("float", "f", false, "Add Floating Numbers") | ||
return addCmd | ||
} | ||
|
||
func addInt(args []string, opts *Options) { | ||
logger, _ := opts.Logger() | ||
var sum int | ||
for _, ival := range args { | ||
itemp, err := strconv.Atoi(ival) | ||
if err != nil { | ||
logger.Error(err) | ||
} | ||
sum = sum + itemp | ||
} | ||
logger.Output("Addition of numbers", args, "is", sum) | ||
} | ||
|
||
func addFloat(args []string, opts *Options) { | ||
logger, _ := opts.Logger() | ||
var sum float64 | ||
for _, fval := range args { | ||
ftemp, err := strconv.ParseFloat(fval, 64) | ||
if err != nil { | ||
logger.Error(err) | ||
} | ||
sum = sum + ftemp | ||
} | ||
logger.Output("Addition of numbers", args, "is", sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package date | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aerogear/charmil/pkg/factory" | ||
"github.com/aerogear/charmil/pkg/logging" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Options struct { | ||
Logger func() (logging.Logger, error) | ||
} | ||
|
||
func DateCommand(f *factory.Factory) *cobra.Command { | ||
opts := &Options{ | ||
Logger: f.Logger, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "date", | ||
Short: "tell date", | ||
Example: "$ host date", | ||
SilenceUsage: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
logger, _ := opts.Logger() | ||
logger.Output("Date Time is", time.Now()) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package factory | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/aerogear/charmil/pkg/logging" | ||
) | ||
|
||
// create new command factory | ||
func Default() *Factory { | ||
var logger logging.Logger | ||
|
||
loggerFunc := func() (logging.Logger, error) { | ||
if logger != nil { | ||
return logger, nil | ||
} | ||
|
||
loggerBuilder := logging.NewStdLoggerBuilder() | ||
loggerBuilder = loggerBuilder.Streams(os.Stdout, os.Stdin) | ||
logger, err := loggerBuilder.Build() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return logger, nil | ||
} | ||
|
||
return &Factory{ | ||
Logger: loggerFunc, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package factory | ||
|
||
import ( | ||
"github.com/aerogear/charmil/pkg/logging" | ||
) | ||
|
||
// Access to all the commands | ||
type Factory struct { | ||
Logger func() (logging.Logger, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package logging | ||
|
||
type Logger interface { | ||
Output(args ...interface{}) | ||
Error(args ...interface{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
type StdLoggerBuilder struct { | ||
outStream io.Writer | ||
errStream io.Writer | ||
} | ||
|
||
type StdLogger struct { | ||
outStream io.Writer | ||
errStream io.Writer | ||
} | ||
|
||
// returns new StdLoggerBuilder object | ||
func NewStdLoggerBuilder() *StdLoggerBuilder { | ||
builder := new(StdLoggerBuilder) | ||
return builder | ||
} | ||
|
||
// Set input output streams | ||
func (b *StdLoggerBuilder) Streams(out io.Writer, err io.Writer) *StdLoggerBuilder { | ||
b.outStream = out | ||
b.errStream = err | ||
return b | ||
} | ||
|
||
// builds a new logger with configuration stored in builder | ||
func (b *StdLoggerBuilder) Build() (logger *StdLogger, err error) { | ||
logger = new(StdLogger) | ||
logger.outStream = b.outStream | ||
logger.errStream = b.errStream | ||
if logger.outStream == nil { | ||
logger.outStream = os.Stdout | ||
} | ||
if logger.errStream == nil { | ||
logger.errStream = os.Stderr | ||
} | ||
|
||
return | ||
} | ||
|
||
func (l *StdLogger) Output(args ...interface{}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs |
||
fmt.Fprintln(l.outStream, args...) | ||
} | ||
|
||
func (l *StdLogger) Error(args ...interface{}) { | ||
fmt.Fprintln(l.errStream, args...) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs please