diff --git a/cmd/aws-sso/logger.go b/cmd/aws-sso/logger.go index 3f73f65c..cb70188a 100644 --- a/cmd/aws-sso/logger.go +++ b/cmd/aws-sso/logger.go @@ -18,18 +18,10 @@ package main * along with this program. If not, see . */ -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() } -*/ diff --git a/cmd/aws-sso/main.go b/cmd/aws-sso/main.go index f13616b0..0ae3abbe 100644 --- a/cmd/aws-sso/main.go +++ b/cmd/aws-sso/main.go @@ -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" ) @@ -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 { @@ -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 } diff --git a/internal/ecs/client/client.go b/internal/ecs/client/client.go index 906abec1..7fc93341 100644 --- a/internal/ecs/client/client.go +++ b/internal/ecs/client/client.go @@ -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 diff --git a/internal/ecs/client_request_test.go b/internal/ecs/client_request_test.go index 99ccda18..70a69a4e 100644 --- a/internal/ecs/client_request_test.go +++ b/internal/ecs/client_request_test.go @@ -2,7 +2,7 @@ package ecs /* * AWS SSO CLI - * Copyright (c) 2021-2022 Aaron Turner + * Copyright (c) 2021-202 Aaron Turner * * This program is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License as diff --git a/internal/ecs/http.go b/internal/ecs/http.go index cdda3e07..91ea66f4 100644 --- a/internal/ecs/http.go +++ b/internal/ecs/http.go @@ -5,6 +5,7 @@ import ( "net/http" "strconv" + "github.com/synfinatic/aws-sso-cli/internal/logger" "github.com/synfinatic/aws-sso-cli/internal/storage" ) @@ -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()) diff --git a/internal/ecs/logger.go b/internal/ecs/logger.go index ec98a631..cf78a90f 100644 --- a/internal/ecs/logger.go +++ b/internal/ecs/logger.go @@ -18,23 +18,10 @@ package ecs * along with this program. If not, see . */ -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 -} -*/ - -// this is configured by cmd/main.go, but we have this here for unit tests func init() { - log = logrus.New() + log = logger.GetLogger() } diff --git a/internal/ecs/server/logger.go b/internal/ecs/server/logger.go deleted file mode 100644 index 50b1dac9..00000000 --- a/internal/ecs/server/logger.go +++ /dev/null @@ -1,40 +0,0 @@ -package server - -/* - * AWS SSO CLI - * Copyright (c) 2021-2024 Aaron Turner - * - * This program is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or with the authors permission any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Logger - -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() -} diff --git a/internal/ecs/server/server.go b/internal/ecs/server/server.go index a53677ea..1718fa84 100644 --- a/internal/ecs/server/server.go +++ b/internal/ecs/server/server.go @@ -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 diff --git a/internal/helper/helper.go b/internal/helper/helper.go index e0aff164..09b40219 100644 --- a/internal/helper/helper.go +++ b/internal/helper/helper.go @@ -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 diff --git a/internal/helper/logger.go b/internal/helper/logger.go deleted file mode 100644 index 44fde282..00000000 --- a/internal/helper/logger.go +++ /dev/null @@ -1,40 +0,0 @@ -package helper - -/* - * AWS SSO CLI - * Copyright (c) 2021-2024 Aaron Turner - * - * This program is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or with the authors permission any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Logger - -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() -} diff --git a/internal/ecs/client/logger.go b/internal/logger/logger.go similarity index 69% rename from internal/ecs/client/logger.go rename to internal/logger/logger.go index 640572b8..53706d67 100644 --- a/internal/ecs/client/logger.go +++ b/internal/logger/logger.go @@ -1,4 +1,4 @@ -package client +package logger /* * AWS SSO CLI @@ -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 } diff --git a/internal/predictor/logger.go b/internal/predictor/logger.go deleted file mode 100644 index 1988bdb6..00000000 --- a/internal/predictor/logger.go +++ /dev/null @@ -1,40 +0,0 @@ -package predictor - -/* - * AWS SSO CLI - * Copyright (c) 2021-2024 Aaron Turner - * - * This program is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or with the authors permission any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Logger - -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() -} diff --git a/internal/predictor/predictor.go b/internal/predictor/predictor.go index 9fdb922a..928c1eec 100644 --- a/internal/predictor/predictor.go +++ b/internal/predictor/predictor.go @@ -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 diff --git a/internal/sso/cache_test.go b/internal/sso/cache_test.go index 5ba0d507..1430dfc9 100644 --- a/internal/sso/cache_test.go +++ b/internal/sso/cache_test.go @@ -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 ( @@ -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() + logrus_logger.SetLevel(logrus.DebugLevel) + old_log := log + log = logger.NewLogger(logrus_logger) + defer func() { log = old_log }() // remove one because of HistoryMinutes expires c = suite.setupDeleteOldHistory() diff --git a/internal/sso/logger.go b/internal/sso/logger.go index 81281fb8..7017d404 100644 --- a/internal/sso/logger.go +++ b/internal/sso/logger.go @@ -18,21 +18,10 @@ package sso * along with this program. If not, see . */ -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 -} - -// this is configured by cmd/main.go, but we have this here for unit tests func init() { - log = logrus.New() + log = logger.GetLogger() } diff --git a/internal/storage/keyring_test.go b/internal/storage/keyring_test.go index 69316aaf..e4f9fafd 100644 --- a/internal/storage/keyring_test.go +++ b/internal/storage/keyring_test.go @@ -32,6 +32,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" ) type KeyringSuite struct { @@ -493,16 +494,16 @@ func TestSplitCredentials(t *testing.T) { assert.NoError(t, err) // setup logger for testing - logger, hook := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - oldLogger := GetLogger() - SetLogger(logger) + logrus_logger, hook := test.NewNullLogger() + logrus_logger.SetLevel(logrus.DebugLevel) + old_log := log + log = logger.NewLogger(logrus_logger) + defer func() { log = old_log }() defer func() { os.RemoveAll(d) os.Unsetenv(ENV_SSO_FILE_PASSWORD) keyringGOOS = "" - SetLogger(oldLogger) }() os.Setenv(ENV_SSO_FILE_PASSWORD, "justapassword") diff --git a/internal/storage/logging.go b/internal/storage/logging.go deleted file mode 100644 index ab4820b7..00000000 --- a/internal/storage/logging.go +++ /dev/null @@ -1,38 +0,0 @@ -package storage - -/* - * AWS SSO CLI - * Copyright (c) 2021-2024 Aaron Turner - * - * This program is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or with the authors permission any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Logger - -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() -} diff --git a/internal/storage/storage.go b/internal/storage/storage.go index d3bcb7b2..aca1636e 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -25,10 +25,17 @@ import ( "reflect" "time" + "github.com/synfinatic/aws-sso-cli/internal/logger" "github.com/synfinatic/aws-sso-cli/internal/utils" "github.com/synfinatic/gotable" ) +var log *logger.Logger + +func init() { + log = logger.GetLogger() +} + // this struct should be cached for long term if possible type RegisterClientData struct { AuthorizationEndpoint string `json:"authorizationEndpoint,omitempty"` diff --git a/internal/tags/logger.go b/internal/tags/logger.go index 963b77a3..e0036e5d 100644 --- a/internal/tags/logger.go +++ b/internal/tags/logger.go @@ -18,21 +18,10 @@ package tags * along with this program. If not, see . */ -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 -} - -// this is configured by cmd/main.go, but we have this here for unit tests func init() { - log = logrus.New() + log = logger.GetLogger() } diff --git a/internal/url/logger.go b/internal/url/logger.go deleted file mode 100644 index bfb335ff..00000000 --- a/internal/url/logger.go +++ /dev/null @@ -1,38 +0,0 @@ -package url - -/* - * AWS SSO CLI - * Copyright (c) 2021-2024 Aaron Turner - * - * This program is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or with the authors permission any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Logger - -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() -} diff --git a/internal/url/url.go b/internal/url/url.go index 3ae16ba4..3b86aa7b 100644 --- a/internal/url/url.go +++ b/internal/url/url.go @@ -27,10 +27,18 @@ import ( "strings" "github.com/atotto/clipboard" - "github.com/skratchdot/open-golang/open" // default opener + "github.com/skratchdot/open-golang/open" + "github.com/synfinatic/aws-sso-cli/internal/logger" "github.com/synfinatic/aws-sso-cli/internal/utils" + // default opener ) +var log *logger.Logger + +func init() { + log = logger.GetLogger() +} + // taken from https://github.com/honsiorovskyi/open-url-in-container/blob/1.0.3/launcher.sh var FIREFOX_PLUGIN_COLORS []string = []string{ "blue", diff --git a/internal/url/url_test.go b/internal/url/url_test.go index 407dabee..9de3e669 100644 --- a/internal/url/url_test.go +++ b/internal/url/url_test.go @@ -26,7 +26,6 @@ import ( "os" "testing" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -229,17 +228,6 @@ func TestNewAction(t *testing.T) { assert.Error(t, err) assert.Equal(t, ConfigProfilesAction(ConfigProfilesOpen), b) } - -func TestLogger(t *testing.T) { - first := GetLogger() - defer SetLogger(first) - - log := logrus.New() - SetLogger(log) - - l := GetLogger() - assert.Equal(t, log, l) -} func TestNewConfigProfilesAction(t *testing.T) { t.Parallel() a, err := NewConfigProfilesAction("exec") diff --git a/internal/utils/logger.go b/internal/utils/logger.go deleted file mode 100644 index e7e923dc..00000000 --- a/internal/utils/logger.go +++ /dev/null @@ -1,38 +0,0 @@ -package utils - -/* - * AWS SSO CLI - * Copyright (c) 2021-2024 Aaron Turner - * - * This program is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or with the authors permission any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Logger - -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() -} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index ef863817..a1c0ad27 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -27,8 +27,16 @@ import ( "strconv" "strings" "time" + + "github.com/synfinatic/aws-sso-cli/internal/logger" ) +var log *logger.Logger + +func init() { + log = logger.GetLogger() +} + const MAX_AWS_ACCOUNTID = 999999999999 // GetHomePath returns the absolute path of the provided path with the first ~