Skip to content

Commit

Permalink
Pull out GetLoglevel to a separate package
Browse files Browse the repository at this point in the history
Add option to set loglevel while initializing plugin config
  • Loading branch information
nikhilsbhat committed Jul 1, 2023
1 parent cda8e3c commit 27bc52c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 52 deletions.
22 changes: 2 additions & 20 deletions gocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"crypto/x509"
"reflect"
"sort"
"strings"
"time"

goCdLogger "github.com/nikhilsbhat/gocd-sdk-go/pkg/logger"
"github.com/nikhilsbhat/gocd-sdk-go/pkg/plugin"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -165,7 +165,7 @@ type Auth struct {
// NewClient returns new instance of httpClient when invoked.
func NewClient(baseURL string, auth Auth, logLevel string, caContent []byte) GoCd {
logger := log.New()
logger.SetLevel(GetLoglevel(logLevel))
logger.SetLevel(goCdLogger.GetLoglevel(logLevel))
logger.WithField(goCdAPILoggerName, true)
logger.SetFormatter(&log.JSONFormatter{})

Expand Down Expand Up @@ -196,24 +196,6 @@ func NewClient(baseURL string, auth Auth, logLevel string, caContent []byte) GoC
}
}

// GetLoglevel sets the loglevel to the kind of log asked for.
func GetLoglevel(level string) log.Level {
switch strings.ToLower(level) {
case log.WarnLevel.String():
return log.WarnLevel
case log.DebugLevel.String():
return log.DebugLevel
case log.TraceLevel.String():
return log.TraceLevel
case log.FatalLevel.String():
return log.FatalLevel
case log.ErrorLevel.String():
return log.ErrorLevel
default:
return log.InfoLevel
}
}

func (auth *Auth) setAuth(newClient *resty.Client) {
if len(auth.BearerToken) != 0 {
newClient.SetAuthToken(auth.BearerToken)
Expand Down
24 changes: 0 additions & 24 deletions gocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,9 @@ import (

"github.com/nikhilsbhat/gocd-sdk-go"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func Test_getLoglevel(t *testing.T) {
t.Run("should return warn level", func(t *testing.T) {
actual := gocd.GetLoglevel("warning")
assert.Equal(t, log.WarnLevel, actual)
})
t.Run("should return trace level", func(t *testing.T) {
actual := gocd.GetLoglevel("trace")
assert.Equal(t, log.TraceLevel, actual)
})
t.Run("should return debug level", func(t *testing.T) {
actual := gocd.GetLoglevel("debug")
assert.Equal(t, log.DebugLevel, actual)
})
t.Run("should return fatal level", func(t *testing.T) {
actual := gocd.GetLoglevel("fatal")
assert.Equal(t, log.FatalLevel, actual)
})
t.Run("should return error level", func(t *testing.T) {
actual := gocd.GetLoglevel("error")
assert.Equal(t, log.ErrorLevel, actual)
})
}

func TestGetGoCDMethodNames(t *testing.T) {
t.Run("should list all method names", func(t *testing.T) {
response := gocd.GetGoCDMethodNames()
Expand Down
25 changes: 25 additions & 0 deletions pkg/logger/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package logger

import (
"strings"

log "github.com/sirupsen/logrus"
)

// GetLoglevel sets the loglevel to the kind of log asked for.
func GetLoglevel(level string) log.Level {
switch strings.ToLower(level) {
case log.WarnLevel.String():
return log.WarnLevel
case log.DebugLevel.String():
return log.DebugLevel
case log.TraceLevel.String():
return log.TraceLevel
case log.FatalLevel.String():
return log.FatalLevel
case log.ErrorLevel.String():
return log.ErrorLevel
default:
return log.InfoLevel
}
}
36 changes: 36 additions & 0 deletions pkg/logger/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package logger_test

import (
"testing"

"github.com/nikhilsbhat/gocd-sdk-go/pkg/logger"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func Test_getLoglevel(t *testing.T) {
t.Run("should return warn level", func(t *testing.T) {
actual := logger.GetLoglevel("warning")
assert.Equal(t, log.WarnLevel, actual)
})
t.Run("should return trace level", func(t *testing.T) {
actual := logger.GetLoglevel("trace")
assert.Equal(t, log.TraceLevel, actual)
})
t.Run("should return debug level", func(t *testing.T) {
actual := logger.GetLoglevel("debug")
assert.Equal(t, log.DebugLevel, actual)
})
t.Run("should return fatal level", func(t *testing.T) {
actual := logger.GetLoglevel("fatal")
assert.Equal(t, log.FatalLevel, actual)
})
t.Run("should return error level", func(t *testing.T) {
actual := logger.GetLoglevel("error")
assert.Equal(t, log.ErrorLevel, actual)
})
t.Run("should return error level", func(t *testing.T) {
actual := logger.GetLoglevel("")
assert.Equal(t, log.InfoLevel, actual)
})
}
5 changes: 3 additions & 2 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/nikhilsbhat/gocd-sdk-go/pkg/errors"
goCdLogger "github.com/nikhilsbhat/gocd-sdk-go/pkg/logger"

"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -231,9 +232,9 @@ func (cfg *Config) Download() (string, error) {
return pluginLocalPath, nil
}

func NewPluginConfig(version, path, url string) Plugin {
func NewPluginConfig(version, path, url, loglevel string) Plugin {
logger := log.New()
logger.SetLevel(log.TraceLevel)
logger.SetLevel(goCdLogger.GetLoglevel(loglevel))
logger.WithField("pipeline-validator", true)
logger.SetFormatter(&log.JSONFormatter{})

Expand Down
12 changes: 6 additions & 6 deletions pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type YAMLPluginTestSuite struct {
}

func (suite *YAMLPluginTestSuite) SetupTest() {
cfg := plugin.NewPluginConfig("0.13.0", "", "")
cfg := plugin.NewPluginConfig("0.13.0", "", "", "debug")

homePath, err := os.UserHomeDir()
suite.NoError(err)
Expand Down Expand Up @@ -103,7 +103,7 @@ type JOSNPluginTestSuite struct {
}

func (suite *JOSNPluginTestSuite) SetupTest() {
cfg := plugin.NewPluginConfig("0.6.0", "", "")
cfg := plugin.NewPluginConfig("0.6.0", "", "", "debug")

homePath, err := os.UserHomeDir()
suite.NoError(err)
Expand Down Expand Up @@ -179,7 +179,7 @@ type GroovyPluginTestSuite struct {
}

func (suite *GroovyPluginTestSuite) SetupTest() {
cfg := plugin.NewPluginConfig("2.1.3-512", "", "")
cfg := plugin.NewPluginConfig("2.1.3-512", "", "", "debug")

homePath, err := os.UserHomeDir()
suite.NoError(err)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestGroovyPipelineValidateTestSuite(t *testing.T) {

func TestConfig_Download(t *testing.T) {
t.Run("should be able to download the plugin successfully", func(t *testing.T) {
cfg := plugin.NewPluginConfig("0.13.0", "", "")
cfg := plugin.NewPluginConfig("0.13.0", "", "", "debug")

homePath, err := os.UserHomeDir()
assert.NoError(t, err)
Expand All @@ -268,7 +268,7 @@ func TestConfig_Download(t *testing.T) {
})

t.Run("should error out due to unsupported plugin type", func(t *testing.T) {
cfg := plugin.NewPluginConfig("0.13.0", "", "")
cfg := plugin.NewPluginConfig("0.13.0", "", "", "debug")

homePath, err := os.UserHomeDir()
assert.NoError(t, err)
Expand All @@ -284,7 +284,7 @@ func TestConfig_Download(t *testing.T) {
})

t.Run("should error out due to wrong url set", func(t *testing.T) {
cfg := plugin.NewPluginConfig("0.13.0", "", "://github.com/gocd-contrib/gocd-groovy-dsl-config-plugin")
cfg := plugin.NewPluginConfig("0.13.0", "", "://github.com/gocd-contrib/gocd-groovy-dsl-config-plugin", "debug")

homePath, err := os.UserHomeDir()
assert.NoError(t, err)
Expand Down

0 comments on commit 27bc52c

Please sign in to comment.