From 49f5da36ec7d4bbda04d7390d12f647dddc3e75a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Sowa?= Date: Wed, 28 Oct 2020 11:53:07 +0100 Subject: [PATCH] Make logs location use cache dir --- cmd/root.go | 8 +++----- internal/pkg/cache/cache.go | 4 ++-- internal/pkg/cache/cache_test.go | 4 ++-- internal/pkg/token/token.go | 10 +++++----- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index a767fd8..a0cc59e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,7 @@ import ( "github.com/loophole/cli/internal/app/loophole" lm "github.com/loophole/cli/internal/app/loophole/models" + "github.com/loophole/cli/internal/pkg/cache" "github.com/mattn/go-colorable" "github.com/mitchellh/go-homedir" "github.com/rs/zerolog" @@ -66,16 +67,13 @@ func init() { } func initLogger() { - logLocation := "logs/" + time.Now().Format("2006-01-02--15-04-05") + ".log" - - if _, err := os.Stat("logs"); err != nil { - os.Mkdir("logs", 0700) - } + logLocation := cache.GetLocalStorageFile(fmt.Sprintf("%s, %s", time.Now().Format("2006-01-02--15-04-05"), ".log"), "logs") f, err := os.Create(logLocation) if err != nil { stdlog.Fatalln("Error creating log file:", err) } + consoleWriter := zerolog.ConsoleWriter{Out: colorable.NewColorableStderr()} multi := zerolog.MultiLevelWriter(consoleWriter, f) log.Logger = zerolog.New(multi).With().Timestamp().Logger() diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index b67921a..60c932c 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -24,12 +24,12 @@ func GetLocalStorageDir(directoryName string) string { } // GetLocalStorageFile returns local file for loophole cache purposes -func GetLocalStorageFile(fileName string) string { +func GetLocalStorageFile(fileName string, directoryName string) string { home, err := homedir.Dir() if err != nil { log.Fatal().Err(err).Msg("Error reading user home directory ") } - dirName := path.Join(home, ".loophole") + dirName := path.Join(home, ".loophole", directoryName) err = os.MkdirAll(dirName, os.ModePerm) if err != nil { log.Fatal().Err(err).Msg("Error creating local cache directory") diff --git a/internal/pkg/cache/cache_test.go b/internal/pkg/cache/cache_test.go index a3a1667..e1204f8 100644 --- a/internal/pkg/cache/cache_test.go +++ b/internal/pkg/cache/cache_test.go @@ -44,7 +44,7 @@ func TestGetLocalStorageFileReturnsCorrectPath(t *testing.T) { filename := "expected-filename" expectedPath := fmt.Sprintf("%s/.loophole/%s", home, filename) - filePath := GetLocalStorageFile(filename) + filePath := GetLocalStorageFile(filename, "") if expectedPath != filePath { t.Fatalf("Created directory path '%s' is different than expected: '%s'", filePath, expectedPath) @@ -53,7 +53,7 @@ func TestGetLocalStorageFileReturnsCorrectPath(t *testing.T) { func TestGetLocalStorageDirDoesntCreateFile(t *testing.T) { filename := "expected-filename" - filePath := GetLocalStorageFile(filename) + filePath := GetLocalStorageFile(filename, "") _, err := os.Stat(filePath) if !os.IsNotExist(err) { diff --git a/internal/pkg/token/token.go b/internal/pkg/token/token.go index 2e12c69..ac73408 100644 --- a/internal/pkg/token/token.go +++ b/internal/pkg/token/token.go @@ -46,7 +46,7 @@ type TokenSpec struct { } func IsTokenSaved() bool { - tokensLocation := cache.GetLocalStorageFile("tokens.json") + tokensLocation := cache.GetLocalStorageFile("tokens.json", "") if _, err := os.Stat(tokensLocation); os.IsNotExist(err) { return false @@ -57,7 +57,7 @@ func IsTokenSaved() bool { } func SaveToken(token *TokenSpec) error { - tokensLocation := cache.GetLocalStorageFile("tokens.json") + tokensLocation := cache.GetLocalStorageFile("tokens.json", "") tokenBytes, err := json.Marshal(token) if err != nil { @@ -234,7 +234,7 @@ func RefreshToken() error { } func DeleteTokens() error { - tokensLocation := cache.GetLocalStorageFile("tokens.json") + tokensLocation := cache.GetLocalStorageFile("tokens.json", "") err := os.Remove(tokensLocation) if err != nil { @@ -244,7 +244,7 @@ func DeleteTokens() error { } func GetAccessToken() (string, error) { - tokensLocation := cache.GetLocalStorageFile("tokens.json") + tokensLocation := cache.GetLocalStorageFile("tokens.json", "") tokens, err := ioutil.ReadFile(tokensLocation) if err != nil { @@ -259,7 +259,7 @@ func GetAccessToken() (string, error) { } func GetRefreshToken() (string, error) { - tokensLocation := cache.GetLocalStorageFile("tokens.json") + tokensLocation := cache.GetLocalStorageFile("tokens.json", "") tokens, err := ioutil.ReadFile(tokensLocation) if err != nil {