Skip to content

Commit

Permalink
Removing config log references
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Nov 5, 2023
1 parent 6a3dcd0 commit b56982c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
28 changes: 14 additions & 14 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"
"github.com/esnet/gdg/internal/tools"
"github.com/thoas/go-funk"
"log/slog"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"

"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"log"
)

func (s *Configuration) ClearContexts() {
Expand All @@ -29,7 +29,7 @@ func (s *Configuration) ClearContexts() {
log.Fatal("Failed to make save changes")
}

log.Info("All contexts were cleared")
slog.Info("All contexts were cleared")

}

Expand Down Expand Up @@ -69,19 +69,19 @@ func (s *Configuration) CopyContext(src, dest string) {
if err != nil {
log.Fatal("Failed to make save changes")
}
log.Infof("Copied %s context to %s please check your config to confirm", src, dest)
slog.Info("Copied context to destination, please check your config to confirm", "sourceContext", src, "destinationContext", dest)
}

func (s *Configuration) PrintContext(name string) {
name = strings.ToLower(name)
grafana, ok := s.GetAppConfig().GetContexts()[name]
if !ok {
log.Errorf("context %s was not found", name)
slog.Error("context was not found", "context", name)
return
}
d, err := yaml.Marshal(grafana)
if err != nil {
log.WithError(err).Fatal("failed to serialize context")
log.Fatal("failed to serialize context", "err", err)
}
fmt.Printf("---%s:\n%s\n\n", name, string(d))

Expand All @@ -93,7 +93,7 @@ func (s *Configuration) DeleteContext(name string) {
contexts := s.GetAppConfig().GetContexts()
_, ok := contexts[name]
if !ok {
log.Infof("Context not found, cannot delete context named '%s'", name)
slog.Info("Context not found, cannot delete context", "context", name)
return
}
delete(contexts, name)
Expand All @@ -108,10 +108,10 @@ func (s *Configuration) DeleteContext(name string) {
if err != nil {
log.Fatal("Failed to make save changes")
}
log.Infof("Delete %s context and set new context to %s", name, s.GetAppConfig().ContextName)
slog.Info("Deleted context and set new context to", "deletedContext", name, "newActiveContext", s.GetAppConfig().ContextName)
}

// ChangeContext
// ChangeContext changes active context
func (s *Configuration) ChangeContext(name string) {
name = strings.ToLower(name)
_, ok := s.GetAppConfig().GetContexts()[name]
Expand All @@ -123,7 +123,7 @@ func (s *Configuration) ChangeContext(name string) {
if err != nil {
log.Fatal("Failed to make save changes")
}
log.Infof("Change context to: '%s'", name)
slog.Info("Changed context", "context", name)
}

// SaveToDisk Persists current configuration to disk
Expand Down Expand Up @@ -151,7 +151,7 @@ func (app *AppConfig) GetContextMap() map[string]interface{} {
response := make(map[string]interface{})
data, err := json.Marshal(app.Contexts)
if err != nil {
log.Errorf("could not serialize contexts")
slog.Error("could not serialize contexts")
return response
}
err = json.Unmarshal(data, &response)
Expand Down Expand Up @@ -215,13 +215,13 @@ func setMapValueEnvOverride(keys []string, mapValue map[string]interface{}, valu
if len(keys) > 1 {
rawInnerObject, ok := mapValue[keys[0]]
if !ok {
log.Warn("No Inner map exists, cannot set Env Override")
slog.Warn("No Inner map exists, cannot set Env Override")
return
}

innerMap, ok := rawInnerObject.(map[string]interface{})
if !ok {
log.Warn("cannot traverse full map path. Unable to set ENV override. Returning ")
slog.Warn("cannot traverse full map path. Unable to set ENV override. Returning ")
return
}
setMapValueEnvOverride(keys[1:], innerMap, value)
Expand Down Expand Up @@ -269,7 +269,7 @@ func InitConfig(override, defaultConfig string) {
ok := errors.As(err, &configFileNotFoundError)

if err != nil && ok {
log.Info("No configuration file has been found, creating a default configuration")
slog.Info("No configuration file has been found, creating a default configuration")
err = os.MkdirAll("config", os.ModePerm)
if err != nil {
log.Fatal("unable to create configuration folder: 'config'")
Expand Down
13 changes: 7 additions & 6 deletions internal/config/config_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"errors"
"fmt"
"github.com/esnet/grafana-swagger-api-golang/goclient/models"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"log"
"log/slog"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (ds *ConnectionSettings) FiltersEnabled() bool {
func (ds *ConnectionSettings) GetCredentials(connectionEntity models.AddDataSourceCommand) (*GrafanaConnection, error) {
data, err := json.Marshal(connectionEntity)
if err != nil {
log.Warn("Unable to marshall Connection, unable to fetch credentials")
slog.Warn("Unable to marshall Connection, unable to fetch credentials")
return nil, fmt.Errorf("unable to marshall Connection, unable to fetch credentials")
}
//Get Auth based on New Matching Rules
Expand All @@ -80,14 +81,14 @@ func (ds *ConnectionSettings) GetCredentials(connectionEntity models.AddDataSour
for _, rule := range entry.Rules {
fieldObject := parser.Get(rule.Field)
if !fieldObject.Exists() {
log.Warnf("Unable to find a field titled: %s in datasource, skipping validation rule", rule.Field)
slog.Warn("Unable to find a matching field in datasource, skipping validation rule", "filedName", rule.Field)
valid = false
continue
}
fieldValue := fieldObject.String()
p, err := regexp.Compile(rule.Regex)
if err != nil {
log.Warnf("Unable to compile regex: %s to match against field %s, skipping validation", rule.Regex, rule.Field)
slog.Warn("Unable to compile regex to match against field, skipping validation", "regex", rule.Regex, "fieldName", rule.Field)
valid = false
}
if !p.Match([]byte(fieldValue)) {
Expand All @@ -108,7 +109,7 @@ func (ds *ConnectionSettings) GetCredentials(connectionEntity models.AddDataSour
func (ds *ConnectionSettings) IsExcluded(item interface{}) bool {
data, err := json.Marshal(item)
if err != nil {
log.Warn("Unable to serialize object, cannot validate")
slog.Warn("Unable to serialize object, cannot validate")
return true
}

Expand All @@ -124,7 +125,7 @@ func (ds *ConnectionSettings) IsExcluded(item interface{}) bool {
fieldValue := fieldParse.String()
p, err := regexp.Compile(field.Regex)
if err != nil {
log.Warnf("Invalid regex for filter rule with field: %s", field.Field)
slog.Warn("Invalid regex for filter rule", "field", field.Field)
return true
}
match := p.Match([]byte(fieldValue))
Expand Down
5 changes: 3 additions & 2 deletions internal/config/config_new_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package config

import (
"github.com/AlecAivazis/survey/v2"
log "github.com/sirupsen/logrus"
"log"
"log/slog"
"strings"
)

Expand Down Expand Up @@ -122,6 +123,6 @@ func (s *Configuration) NewContext(name string) {
if err != nil {
log.Fatal("could not save configuration.")
}
log.Infof("New configuration %s has been created", name)
slog.Info("New configuration has been created", "newContext", name)

}
10 changes: 5 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/grafana-swagger-api-golang/goclient/models"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"log/slog"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -47,10 +47,10 @@ func TestSetup(t *testing.T) {
os.Setenv("GDG_CONTEXT_NAME", "qa")
config.InitConfig("testing.yml", "")
conf := config.Config().ViperConfig()
log.Info(conf.ConfigFileUsed())
slog.Info(conf.ConfigFileUsed())

confobj := config.Config().GetAppConfig()
log.Infof(confobj.ContextName)
slog.Info(confobj.ContextName)
assert.NotNil(t, conf)
context := conf.GetString("context_name")
assert.Equal(t, context, "qa")
Expand All @@ -70,10 +70,10 @@ func TestWatchedFoldersConfig(t *testing.T) {
os.Setenv("GDG_CONTEXT_NAME", "qa")
config.InitConfig("testing.yml", "")
conf := config.Config().ViperConfig()
log.Info(conf.ConfigFileUsed())
slog.Info(conf.ConfigFileUsed())

confobj := config.Config().GetAppConfig()
log.Infof(confobj.ContextName)
slog.Info(confobj.ContextName)
assert.NotNil(t, conf)
context := conf.GetString("context_name")
assert.Equal(t, context, "qa")
Expand Down
5 changes: 3 additions & 2 deletions internal/tools/prompt_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package tools
import (
"bufio"
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"log"
"log/slog"
"os"
"strings"
)
Expand All @@ -27,7 +28,7 @@ func GetUserConfirmation(msg, error string, terminate bool) bool {
ans, _ := r.ReadString('\n')
ans = strings.ToLower(ans)
if !slices.Contains(validResponse, rune(ans[0])) {
log.Error("Invalid response, please try again. Only [yes/no] are supported")
slog.Error("Invalid response, please try again. Only [yes/no] are supported")
continue
}
//Validate Response
Expand Down

0 comments on commit b56982c

Please sign in to comment.