From 10564bf5e56c1c7a3c2d0177c2b2dabdd817ba1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Fernandes?= Date: Sun, 26 Mar 2023 14:57:29 -0300 Subject: [PATCH] create log file only in config dir --- config/config.go | 28 +++++++++++++++++++++++++++- logger/logger.go | 3 ++- main.go | 7 +++++++ token/token.go | 28 +++++----------------------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/config/config.go b/config/config.go index b1a1e3e..6725e95 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,11 @@ package config -import "time" +import ( + "fmt" + "os" + "path" + "time" +) const ( DateFormat = "Jan 02, 2006" @@ -15,4 +20,25 @@ const ( var ( Width int Height int + + ConfigDirPath string + LogFilePath string + TokenFilePath string ) + +func Init() error { + home, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("could not get user home dir: %w", err) + } + + ConfigDirPath := path.Join(home, ".config", "astro") + if err := os.MkdirAll(ConfigDirPath, 0755); err != nil { + return fmt.Errorf("could not create dir %q: %w", ConfigDirPath, err) + } + + LogFilePath = path.Join(ConfigDirPath, "log.log") + TokenFilePath = path.Join(ConfigDirPath, "token") + + return nil +} diff --git a/logger/logger.go b/logger/logger.go index fc2b552..fe36ad7 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,6 +1,7 @@ package logger import ( + "astro/config" "fmt" "log" "os" @@ -14,7 +15,7 @@ var ( ) func Init() error { - f, err := os.OpenFile("log.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + f, err := os.OpenFile(config.LogFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { return fmt.Errorf("failed to open log file: %w", err) } diff --git a/main.go b/main.go index ceecc32..a370bff 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "astro/config" "astro/habit" "astro/logger" "astro/models/list" @@ -12,13 +13,19 @@ import ( ) func main() { + if err := config.Init(); err != nil { + log.Fatal(err) + } + if err := logger.Init(); err != nil { log.Fatal(err) } + tok, err := token.Init() if err != nil { log.Fatal(err) } + habit.InitClient(tok) state.GetAll() p := tea.NewProgram(list.NewList(), tea.WithAltScreen()) diff --git a/token/token.go b/token/token.go index 8522063..5e5087b 100644 --- a/token/token.go +++ b/token/token.go @@ -1,46 +1,34 @@ package token import ( + "astro/config" "astro/habit" "errors" "fmt" "io" "os" - "path" -) - -var ( - configDir string - tokenPath string ) func Init() (string, error) { - home, err := os.UserHomeDir() - if err != nil { - return "", fmt.Errorf("could not get user home dir: %w", err) - } - configDir = path.Join(home, ".config", "astro") - tokenPath = path.Join(configDir, "token") - if err := ensureTokenExists(); err != nil { return "", fmt.Errorf("could no create token: %w", err) } - f, err := os.Open(tokenPath) + f, err := os.Open(config.TokenFilePath) if err != nil { return "", fmt.Errorf("could not open token file: %w", err) } token, err := io.ReadAll(f) if err != nil { - return "", fmt.Errorf("could not read token file(%q): %w", tokenPath, err) + return "", fmt.Errorf("could not read token file(%q): %w", config.TokenFilePath, err) } return string(token), nil } func ensureTokenExists() error { - _, err := os.Stat(tokenPath) + _, err := os.Stat(config.TokenFilePath) if err == nil { return nil } @@ -49,11 +37,6 @@ func ensureTokenExists() error { return fmt.Errorf("could not stat token file: %w", err) } - err = os.MkdirAll(configDir, 0755) - if err != nil { - return fmt.Errorf("could not create dir %q: %w", configDir, err) - } - res, err := habit.NewAPI().CreateToken() if err != nil { return fmt.Errorf("could not create token: %w", err) @@ -65,8 +48,7 @@ func ensureTokenExists() error { return fmt.Errorf("could not read response body: %w", err) } - err = os.WriteFile(tokenPath, b, 0644) - if err != nil { + if err = os.WriteFile(config.TokenFilePath, b, 0644); err != nil { return fmt.Errorf("could not write token file: %w", err) }