Skip to content

Commit

Permalink
refactor the logger to be a singleton and use init()
Browse files Browse the repository at this point in the history
Cleanup the logger to better follow Go best practices.

Refs: #1001
  • Loading branch information
synfinatic committed Jul 20, 2024
1 parent 81e0984 commit 673ece4
Show file tree
Hide file tree
Showing 24 changed files with 102 additions and 349 deletions.
16 changes: 4 additions & 12 deletions cmd/aws-sso/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@ package main
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

var log *logrus.Logger
var log *logger.Logger

/*
func SetLogger(l *logrus.Logger) {
log = l
}
func GetLogger() *logrus.Logger {
return log
func init() {
log = logger.GetLogger()
}
*/
26 changes: 1 addition & 25 deletions cmd/aws-sso/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ import (
"github.com/posener/complete"

// "github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"

"github.com/synfinatic/aws-sso-cli/internal/config"
"github.com/synfinatic/aws-sso-cli/internal/ecs"
"github.com/synfinatic/aws-sso-cli/internal/ecs/client"
"github.com/synfinatic/aws-sso-cli/internal/ecs/server"
"github.com/synfinatic/aws-sso-cli/internal/helper"
"github.com/synfinatic/aws-sso-cli/internal/predictor"
"github.com/synfinatic/aws-sso-cli/internal/sso"
"github.com/synfinatic/aws-sso-cli/internal/storage"
"github.com/synfinatic/aws-sso-cli/internal/tags"
"github.com/synfinatic/aws-sso-cli/internal/url"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/willabides/kongplete"
)
Expand Down Expand Up @@ -158,20 +152,8 @@ func main() {
Auth: AUTH_UNKNOWN,
}

log = logrus.New()
override := parseArgs(&runCtx)

helper.SetLogger(log)
predictor.SetLogger(log)
sso.SetLogger(log)
storage.SetLogger(log)
tags.SetLogger(log)
url.SetLogger(log)
utils.SetLogger(log)
ecs.SetLogger(log)
server.SetLogger(log)
client.SetLogger(log)

if runCtx.Auth == AUTH_NO_CONFIG {
// side-step the rest of the setup...
if err = runCtx.Kctx.Run(&runCtx); err != nil {
Expand Down Expand Up @@ -331,12 +313,6 @@ func parseArgs(ctx *RunContext) sso.OverrideSettings {
Threads: threads, // must be > 0 to override config
}

log.SetFormatter(&logrus.TextFormatter{
DisableLevelTruncation: true,
PadLevelText: true,
DisableTimestamp: true,
})

return override
}

Expand Down
7 changes: 7 additions & 0 deletions internal/ecs/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ 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"
)

var log *logger.Logger

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

type ECSClient struct {
server string
authToken string
Expand Down
2 changes: 1 addition & 1 deletion internal/ecs/client_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ecs

/*
* AWS SSO CLI
* Copyright (c) 2021-2022 Aaron Turner <synfinatic at gmail dot com>
* Copyright (c) 2021-202 Aaron Turner <synfinatic at gmail dot com>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
Expand Down
2 changes: 2 additions & 0 deletions internal/ecs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strconv"

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

Expand Down Expand Up @@ -34,6 +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 := logger.GetLogger()
w.Header().Set("Content-Type", CHARSET_JSON)
if err := json.NewEncoder(w).Encode(jdata); err != nil {
log.Error(err.Error())
Expand Down
19 changes: 3 additions & 16 deletions internal/ecs/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,10 @@ package ecs
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

var log *logrus.Logger
var log *logger.Logger

Check failure on line 23 in internal/ecs/logger.go

View workflow job for this annotation

GitHub Actions / lint

var `log` is unused (unused)

func SetLogger(l *logrus.Logger) {
log = l
}

/*
func GetLogger() *logrus.Logger {
return log
}
*/

// this is configured by cmd/main.go, but we have this here for unit tests
func init() {
log = logrus.New()
log = logger.GetLogger()
}
40 changes: 0 additions & 40 deletions internal/ecs/server/logger.go

This file was deleted.

7 changes: 7 additions & 0 deletions internal/ecs/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ 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"
)

var log *logger.Logger

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

type EcsServer struct {
listener net.Listener
authToken string
Expand Down
7 changes: 7 additions & 0 deletions internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ import (
"path"

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

var log *logger.Logger

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

//go:embed bash_profile.sh zshrc.sh aws-sso.fish
var embedFiles embed.FS

Expand Down
40 changes: 0 additions & 40 deletions internal/helper/logger.go

This file was deleted.

31 changes: 21 additions & 10 deletions internal/ecs/client/logger.go → internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package logger

/*
* AWS SSO CLI
Expand All @@ -22,19 +22,30 @@ import (
"github.com/sirupsen/logrus"
)

var log *logrus.Logger
var log *Logger

func SetLogger(l *logrus.Logger) {
log = l
type Logger struct {
*logrus.Logger
}

/*
func GetLogger() *logrus.Logger {
return log
func NewLogger(l *logrus.Logger) *Logger {
return &Logger{l}
}
*/

// this is configured by cmd/main.go, but we have this here for unit tests
func init() {
log = logrus.New()
log = &Logger{logrus.New()}
log.SetFormatter(&logrus.TextFormatter{
DisableLevelTruncation: true,
PadLevelText: true,
DisableTimestamp: true,
})

}

func SetLogger(l *Logger) {
log = l
}

func GetLogger() *Logger {
return log
}
40 changes: 0 additions & 40 deletions internal/predictor/logger.go

This file was deleted.

7 changes: 7 additions & 0 deletions internal/predictor/predictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ 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"
)

var log *logger.Logger

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

type Predictor struct {
configFile string
accountids []string
Expand Down
11 changes: 6 additions & 5 deletions internal/sso/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/synfinatic/aws-sso-cli/internal/logger"
)

const (
Expand Down Expand Up @@ -264,11 +265,11 @@ func (suite *CacheTestSuite) TestDeleteOldHistory() {
c.SSO["Default"].Roles.Accounts[123456789012].Roles["Foo"].Tags)

// setup logger for tests
logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
oldLogger := GetLogger()
SetLogger(logger)
defer SetLogger(oldLogger)
logrus_logger, hook := test.NewNullLogger()

Check failure on line 268 in internal/sso/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; var logrus_logger should be logrusLogger (revive)
logrus_logger.SetLevel(logrus.DebugLevel)
old_log := log

Check failure on line 270 in internal/sso/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; var old_log should be oldLog (revive)
log = logger.NewLogger(logrus_logger)
defer func() { log = old_log }()

// remove one because of HistoryMinutes expires
c = suite.setupDeleteOldHistory()
Expand Down
Loading

0 comments on commit 673ece4

Please sign in to comment.