Skip to content

Commit

Permalink
Merge pull request #1005 from synfinatic/move-logger
Browse files Browse the repository at this point in the history
refactor the logger to be a singleton and use init()
  • Loading branch information
synfinatic authored Jul 20, 2024
2 parents 81e0984 + c4f5138 commit 86be47d
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 373 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
40 changes: 0 additions & 40 deletions internal/ecs/logger.go

This file was deleted.

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.

30 changes: 20 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,29 @@ 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
Loading

0 comments on commit 86be47d

Please sign in to comment.