Skip to content

Commit

Permalink
Merge pull request #51 from loophole/logs-location
Browse files Browse the repository at this point in the history
Make logs location use cache dir
  • Loading branch information
Morishiri committed Oct 28, 2020
2 parents 41f00d4 + 49f5da3 commit 95033a3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
8 changes: 3 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 95033a3

Please sign in to comment.