Skip to content

Commit

Permalink
chore: rebase & remove conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithans committed Jul 8, 2021
1 parent 744ef46 commit 7e1382b
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 301 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 17 additions & 7 deletions core/factory/default.go
Expand Up @@ -2,20 +2,30 @@ package factory

import (
"fmt"
"os"

"github.com/aerogear/charmil/core/config"
"github.com/aerogear/charmil/core/debug"
"github.com/aerogear/charmil/core/iostreams"
"github.com/aerogear/charmil/core/localize"
"github.com/aerogear/charmil/core/logging"
)

// Default creates new instance of factory for plugins
func Default(localizer localize.Localizer, cfgHandler *config.CfgHandler) *Factory {
func Default(localizer localize.Localizer) *Factory {
io := iostreams.System()

var logger logging.Logger
// initializing logger
loggerFunc := func() (logging.Logger, error) {
if logger != nil {
return logger, nil
}

loggerBuilder := logging.NewStdLoggerBuilder()
loggerBuilder = loggerBuilder.Streams(os.Stdout, os.Stdin)
loggerBuilder = loggerBuilder.Streams(io.Out, io.ErrOut)

debugEnabled := debug.Enabled()
loggerBuilder = loggerBuilder.Debug(debugEnabled)

logger, err := loggerBuilder.Build()
if err != nil {
return nil, err
Expand All @@ -31,8 +41,8 @@ func Default(localizer localize.Localizer, cfgHandler *config.CfgHandler) *Facto
}

return &Factory{
Logger: logger,
Localizer: localizer,
CfgHandler: cfgHandler,
IOStreams: io,
Logger: logger,
Localizer: localizer,
}
}
3 changes: 3 additions & 0 deletions core/factory/factory.go
Expand Up @@ -2,13 +2,16 @@ package factory

import (
"github.com/aerogear/charmil/core/config"
"github.com/aerogear/charmil/core/iostreams"
"github.com/aerogear/charmil/core/localize"
"github.com/aerogear/charmil/core/logging"
)

// Factory is an abstract type which provides
// the access of charmil packages to the commands
type Factory struct {
// Type which defines the streams for the CLI
IOStreams *iostreams.IOStreams
// Logger provides functions for unified logging
Logger logging.Logger
// Localizer localizes the text in commands
Expand Down
File renamed without changes.
@@ -1,7 +1,7 @@
package flag

import (
flagutil "github.com/aerogear/charmil/pkg/cmdutil/flags"
flagutil "github.com/aerogear/charmil/core/cmdutil/flags"
)

// ValidOutput checks if value v is a valid value for --output
Expand Down
File renamed without changes.
9 changes: 1 addition & 8 deletions core/localize/i18n.go
Expand Up @@ -53,14 +53,7 @@ func New(cfg *Config) (Localizer, error) {
if cfg == nil {
cfg = &Config{}
}
// if cfg.Files == nil {
// cfg.Files = LocalizerGetDefaultLocales()
// cfg.Path = "locales"
// cfg.Format = "toml"
// }
// if cfg.Language == nil {
// cfg.Language = localize.GetDefaultLanguage()
// }

if cfg.Format == "" {
cfg.Format = "toml"
}
Expand Down
9 changes: 9 additions & 0 deletions core/logging/logger.go
Expand Up @@ -2,16 +2,25 @@ package logging

// Logger is an Interface for logging messages in unified way
type Logger interface {

// DebugEnabled returns true if the debug level is enabled.
DebugEnabled() bool

// InfoEnabled returns true if the information level is enabled.
InfoEnabled() bool

// Info prints an formatted output message
// using fmt.Fprint function
Info(args ...interface{})

// Error prints an formatted error message
// using fmt.Fprint function
Error(args ...interface{})

// Infof prints an formatted output message
// using fmt.Fprintf function
Infof(format string, args ...interface{})

// Errorf prints an formatted error message
// using fmt.Fprintf function
Errorf(format string, args ...interface{})
Expand Down
59 changes: 54 additions & 5 deletions core/logging/std_logger.go
Expand Up @@ -12,20 +12,33 @@ import (
// the configuration to build the logger which uses
// standard output and error streams or custom writers
type StdLoggerBuilder struct {
outStream io.Writer
errStream io.Writer
debugEnabled bool
infoEnabled bool
errorEnabled bool
outStream io.Writer
errStream io.Writer
}

// StdLogger is a type of logger which uses
// standard output and error streams, or custom writers
type StdLogger struct {
outStream io.Writer
errStream io.Writer
debugEnabled bool
infoEnabled bool
errorEnabled bool
outStream io.Writer
errStream io.Writer
}

// NewStdLoggerBuilder returns new StdLoggerBuilder object
func NewStdLoggerBuilder() *StdLoggerBuilder {
// Allocate the object:
builder := new(StdLoggerBuilder)

// Set default values:
builder.debugEnabled = false
builder.infoEnabled = true
builder.errorEnabled = true

return builder
}

Expand All @@ -36,10 +49,47 @@ func (b *StdLoggerBuilder) Streams(out io.Writer, err io.Writer) *StdLoggerBuild
return b
}

// Debug enables or disables the debug level.
func (b *StdLoggerBuilder) Debug(flag bool) *StdLoggerBuilder {
b.debugEnabled = flag
return b
}

// Info enables or disables the information level.
func (b *StdLoggerBuilder) Info(flag bool) *StdLoggerBuilder {
b.infoEnabled = flag
return b
}

// Error enables or disables the error level.
func (b *StdLoggerBuilder) Error(flag bool) *StdLoggerBuilder {
b.errorEnabled = flag
return b
}

// DebugEnabled returns true iff the debug level is enabled.
func (l *StdLogger) DebugEnabled() bool {
return l.debugEnabled
}

// InfoEnabled returns true iff the information level is enabled.
func (l *StdLogger) InfoEnabled() bool {
return l.infoEnabled
}

// ErrorEnabled returns true iff the error level is enabled.
func (l *StdLogger) ErrorEnabled() bool {
return l.errorEnabled
}

// Build creates a new logger instance
// with configuration stored in builder
func (b *StdLoggerBuilder) Build() (logger *StdLogger, err error) {
// Allocate and populate the object:
logger = new(StdLogger)
logger.debugEnabled = b.debugEnabled
logger.infoEnabled = b.infoEnabled
logger.errorEnabled = b.errorEnabled
logger.outStream = b.outStream
logger.errStream = b.errStream
if logger.outStream == nil {
Expand All @@ -48,7 +98,6 @@ func (b *StdLoggerBuilder) Build() (logger *StdLogger, err error) {
if logger.errStream == nil {
logger.errStream = os.Stderr
}

return
}

Expand Down
4 changes: 2 additions & 2 deletions examples/host/main.go
Expand Up @@ -55,15 +55,15 @@ func init() {
}

// Creates a new factory instance with default settings
cmdFactory = factory.Default(nil, h)
cmdFactory = factory.Default(nil)
}

func main() {
// Sets a value into config
cfg.Key4 = "val4"

// Add plugin CLI into host
echoCmd, err := echo.EchoCommand(cmdFactory)
echoCmd, err := echo.EchoCommand()
if err != nil {
log.Fatal(err)
}
Expand Down
35 changes: 10 additions & 25 deletions examples/plugins/echo/echo.go
Expand Up @@ -2,7 +2,6 @@ package echo

import (
"embed"
"log"

"github.com/aerogear/charmil/core/factory"
"github.com/aerogear/charmil/core/localize"
Expand All @@ -23,17 +22,8 @@ type Options struct {
Localize localize.Localizer
}

// Initializes a zero-valued struct and stores its address
var cfg = &config{}

// Date Command
func EchoCommand(f *factory.Factory) (*cobra.Command, error) {

// Stores the config for localizer
cfg.LocConfig = localize.Config{
Language: language.English,
Path: "examples/plugins/echo/locales/en/en.yaml",
Format: "yaml"}
func EchoCommand() (*cobra.Command, error) {

// Initialize localizer providing the language, locals and format of locals file
loc, err := localize.New(&localize.Config{
Expand All @@ -46,19 +36,20 @@ func EchoCommand(f *factory.Factory) (*cobra.Command, error) {
return nil, err
}

opts := &factory.Factory{
Logger: f.Logger,
Localizer: loc,
CfgHandler: f.CfgHandler,
// Create new/default instance of factory
newFactory := factory.Default(loc)
opts := &Options{
Logger: newFactory.Logger,
Localize: newFactory.Localizer,
}

// creating new echo command
// using localizer to access default text by ID provided in locals
cmd := &cobra.Command{
Use: opts.Localizer.LocalizeByID("echo.cmd.use"),
Short: opts.Localizer.LocalizeByID("echo.cmd.short"),
Long: opts.Localizer.LocalizeByID("echo.cmd.long"),
Example: opts.Localizer.LocalizeByID("echo.cmd.example"),
Use: opts.Localize.LocalizeByID("echo.cmd.use"),
Short: opts.Localize.LocalizeByID("echo.cmd.short"),
Long: opts.Localize.LocalizeByID("echo.cmd.long"),
Example: opts.Localize.LocalizeByID("echo.cmd.example"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

Expand All @@ -69,11 +60,5 @@ func EchoCommand(f *factory.Factory) (*cobra.Command, error) {
},
}

// Merges the current plugin config into the host CLI config
err = c.MergePluginCfg("echo", opts.CfgHandler, cfg)
if err != nil {
log.Fatal(err)
}

return cmd, nil
}
3 changes: 3 additions & 0 deletions go.mod
Expand Up @@ -4,10 +4,13 @@ go 1.16

require (
github.com/BurntSushi/toml v0.3.1
github.com/fatih/color v1.12.0
github.com/imdario/mergo v0.3.12
github.com/mattn/go-isatty v0.0.12
github.com/nicksnyder/go-i18n/v2 v2.1.2
github.com/pelletier/go-toml v1.2.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/tidwall/sjson v1.1.7
golang.org/x/text v0.3.3
gopkg.in/yaml.v2 v2.4.0
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Expand Up @@ -38,6 +38,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -110,7 +112,11 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand Down Expand Up @@ -242,6 +248,9 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
47 changes: 0 additions & 47 deletions starter/pkg/cmd/factory/default.go

This file was deleted.

0 comments on commit 7e1382b

Please sign in to comment.