Skip to content

Commit

Permalink
Merge pull request #1029 from synfinatic/console-logger
Browse files Browse the repository at this point in the history
switch to console logger by default
  • Loading branch information
synfinatic authored Aug 20, 2024
2 parents ae2a0f4 + dedb87f commit d1d8570
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ lint: ## Run golangci-lint

test-homebrew: $(DIST_DIR)$(PROJECT_NAME) ## Run the homebrew tests
@$(DIST_DIR)$(PROJECT_NAME) --config /dev/null version 2>/dev/null | grep -q "AWS SSO CLI Version $(PROJECT_VERSION)"
@$(DIST_DIR)$(PROJECT_NAME) --config /dev/null 2>&1 | grep -q "no AWS SSO providers have been configured."
@$(DIST_DIR)$(PROJECT_NAME) --config /dev/null 2>&1 | grep -q "no AWS SSO providers have been configured"

# Build targets for our supported plaforms
windows: $(WINDOWS_BIN) ## Build 64bit x86 Windows binary
Expand Down
8 changes: 6 additions & 2 deletions cmd/aws-sso/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ package main
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import "github.com/synfinatic/flexlog"
import (
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

// initialize the default logger to log to stderr and log at the warn level
func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/posener/complete v1.2.3
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/stretchr/testify v1.9.0
github.com/synfinatic/flexlog v0.0.2
github.com/synfinatic/flexlog v0.0.3
github.com/synfinatic/gotable v0.0.3
github.com/willabides/kongplete v0.2.0
golang.org/x/crypto v0.25.0 // indirect
Expand Down Expand Up @@ -46,7 +46,7 @@ require (
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/synfinatic/flexlog v0.0.2 h1:rdybUZfIBKuARa8yU26luzNC4nJ34UKwzid4yZ7PHOg=
github.com/synfinatic/flexlog v0.0.2/go.mod h1:YQiokc3ujp5GJA04wAXo6ce/LsUK6SrUXsOnHqGKsas=
github.com/synfinatic/flexlog v0.0.3 h1:RUTlSnkzxIi1r6/X1QNbMI1D1butjF5fhMF/3HT/mkU=
github.com/synfinatic/flexlog v0.0.3/go.mod h1:73burLPh9m9iL538Gq95AqkyOQjb15FkO4UW4hf8qm0=
github.com/synfinatic/gotable v0.0.3 h1:KI01OLECmOv7laXVNtw6T4kEHue09z9OuQwtNB8D5Mw=
github.com/synfinatic/gotable v0.0.3/go.mod h1:kWXD1bxZY6tyu6tWK3CIbGAOrtF7teg0ZQotUMDvoMw=
github.com/veqryn/slog-json v0.3.0 h1:jI2ORtKP1uQss4zmTR2uCpIDw/XnUvVdr5+0vDNl4Gk=
Expand Down
3 changes: 2 additions & 1 deletion internal/ecs/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/synfinatic/aws-sso-cli/internal/ecs"
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/storage"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

type ECSClient struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/ecs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"strconv"

"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/storage"
"github.com/synfinatic/flexlog"
)

// Use format as defined here: https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/credentials/endpointcreds
Expand Down Expand Up @@ -35,7 +35,7 @@ func WriteCreds(w http.ResponseWriter, creds *storage.RoleCredentials) {

// JSONResponse return a JSON blob as a result
func JSONResponse(w http.ResponseWriter, jdata interface{}) {
log := flexlog.GetLogger()
log := logger.GetLogger()
w.Header().Set("Content-Type", CHARSET_JSON)
if err := json.NewEncoder(w).Encode(jdata); err != nil {
log.Error(err.Error())
Expand Down
3 changes: 2 additions & 1 deletion internal/ecs/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import (

// "github.com/davecgh/go-spew/spew"
"github.com/synfinatic/aws-sso-cli/internal/ecs"
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/storage"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

type EcsServer struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
"path"

"github.com/riywo/loginshell"
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

//go:embed bash_profile.sh zshrc.sh aws-sso.fish
Expand Down
54 changes: 54 additions & 0 deletions internal/logger/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package logger

import (
"log/slog"
"os"

"github.com/mattn/go-isatty"
"github.com/synfinatic/flexlog"
)

var logger flexlog.FlexLogger
var CreateLogger flexlog.NewLoggerFunc

// initialize the default logger to log to stderr and log at the warn level
func init() {
w := os.Stderr
color := isatty.IsTerminal(w.Fd())
addSource := false
level := slog.LevelWarn

logger = flexlog.NewLogger(flexlog.NewConsole, w, addSource, level, color)

slog.SetDefault(logger.GetLogger())
}

func SetLogger(l flexlog.FlexLogger) {
logger = l
}

func GetLogger() flexlog.FlexLogger {
return logger
}

func SetDefaultLogger(l flexlog.FlexLogger) {
slog.SetDefault(l.GetLogger())
}

// SwitchLogger changes the current logger to the specified type
func SwitchLogger(name string) {
var loggers = map[string]flexlog.NewLoggerFunc{
"console": flexlog.NewConsole,
"json": flexlog.NewJSON,
"tint": flexlog.NewTint,
}
var ok bool
CreateLogger, ok = loggers[name]
if !ok {
logger.Fatal("Invalid logger", "name", name)
}

// switch the logger
logger = flexlog.NewLogger(CreateLogger, logger.Writer(), logger.AddSource(), logger.Level(), logger.Color())
slog.SetDefault(logger.GetLogger())
}
3 changes: 2 additions & 1 deletion internal/predictor/predictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// "github.com/davecgh/go-spew/spew"
"github.com/goccy/go-yaml"
"github.com/posener/complete"
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/sso"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/synfinatic/flexlog"
Expand All @@ -33,7 +34,7 @@ import (
var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

type Predictor struct {
Expand Down
7 changes: 5 additions & 2 deletions internal/sso/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package sso
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import "github.com/synfinatic/flexlog"
import (
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}
3 changes: 2 additions & 1 deletion internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"reflect"
"time"

"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/synfinatic/flexlog"
"github.com/synfinatic/gotable"
Expand All @@ -33,7 +34,7 @@ import (
var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

// this struct should be cached for long term if possible
Expand Down
7 changes: 5 additions & 2 deletions internal/tags/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package tags
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import "github.com/synfinatic/flexlog"
import (
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}
3 changes: 2 additions & 1 deletion internal/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/atotto/clipboard"
"github.com/skratchdot/open-golang/open"
"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/synfinatic/flexlog"
// default opener
Expand All @@ -36,7 +37,7 @@ import (
var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

// taken from https://github.com/honsiorovskyi/open-url-in-container/blob/1.0.3/launcher.sh
Expand Down
3 changes: 2 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
"strings"
"time"

"github.com/synfinatic/aws-sso-cli/internal/logger"
"github.com/synfinatic/flexlog"
)

var log flexlog.FlexLogger

func init() {
log = flexlog.GetLogger()
log = logger.GetLogger()
}

const MAX_AWS_ACCOUNTID = 999999999999
Expand Down

0 comments on commit d1d8570

Please sign in to comment.