Skip to content

Commit

Permalink
create log file only in config dir
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofnds committed Mar 26, 2023
1 parent d15831f commit 10564bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
28 changes: 27 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package config

import "time"
import (
"fmt"
"os"
"path"
"time"
)

const (
DateFormat = "Jan 02, 2006"
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"astro/config"
"fmt"
"log"
"os"
Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"astro/config"
"astro/habit"
"astro/logger"
"astro/models/list"
Expand All @@ -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())
Expand Down
28 changes: 5 additions & 23 deletions token/token.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 10564bf

Please sign in to comment.