Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor the logger to be a singleton and use init() #1005

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading