From 1e957345ed340916c7eb78099a7cadb898581c0a Mon Sep 17 00:00:00 2001 From: Dalton Williams Date: Sun, 1 Dec 2024 11:29:26 -0600 Subject: [PATCH] Better health command. Fixed user view printing and leaderboard parsing --- aocgo.go | 2 +- cmd/aocli/aocli.go | 50 ++++++++++++++++++++++++++-------- internal/resources/Puzzle.go | 2 ++ internal/resources/User.go | 2 +- internal/resources/UserView.go | 2 +- internal/session/session.go | 11 +++++++- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/aocgo.go b/aocgo.go index b119810..458bdf4 100644 --- a/aocgo.go +++ b/aocgo.go @@ -33,7 +33,7 @@ func GetInputAsLineArray() []string { } func getData(year int, day int) []byte { - userToken, err := session.GetSessionToken() + userToken, err := session.GetSessionToken(false) if err != nil { log.Fatal(err) } diff --git a/cmd/aocli/aocli.go b/cmd/aocli/aocli.go index 7d0ea26..0b481f0 100644 --- a/cmd/aocli/aocli.go +++ b/cmd/aocli/aocli.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "go.dalton.dog/aocgo/internal/api" "go.dalton.dog/aocgo/internal/cache" "go.dalton.dog/aocgo/internal/resources" "go.dalton.dog/aocgo/internal/session" @@ -69,20 +70,34 @@ func main() { // (Req) year - 2 or 4 digit year (16 or 2016) // (Opt) day - 1 or 2 digit day (1, 01, 21) func Leaderboard(yearIn, dayIn string) { - year, err := utils.ParseYear(yearIn) - if err != nil { - log.Fatal("Error parsing year!", "err", err) - } - + var year int + var day int + var err error var lb resources.ViewableLB - if dayIn != "0" { - day, err := utils.ParseDay(dayIn) + + if yearIn == "0" { + year, day, err := utils.GetYearAndDayFromCWD() if err != nil { - log.Fatal("Error parsing day from args.", "err", err) + log.Fatal("Error loading leaderboard based on current directory!", "err", err) } lb = resources.LoadOrCreateLeaderboard(year, day) } else { - lb = resources.LoadOrCreateLeaderboard(year, 0) + year, err = utils.ParseYear(yearIn) + if err != nil { + log.Fatal("Error parsing year!", "err", err) + } + } + + if lb == nil { + if dayIn != "0" { + day, err = utils.ParseDay(dayIn) + if err != nil { + log.Fatal("Error parsing day from args.", "err", err) + } + lb = resources.LoadOrCreateLeaderboard(year, day) + } else { + lb = resources.LoadOrCreateLeaderboard(year, 0) + } } if lb == nil { @@ -96,12 +111,23 @@ func Leaderboard(yearIn, dayIn string) { // Health will check if a session key is available so that the program can run. // Command: `aocli health` func Health() { - sessionKey, err := session.GetSessionToken() + sessionToken, err := session.GetSessionToken(true) + if err != nil { - log.Fatal("Test failed! Couldn't properly load a session key.", "err", err) + log.Fatal("Test failed! Couldn't properly load a session token.", "err", err) } - log.Info("Test succeeded! Properly loaded session key", "key", sessionKey) + log.Info("Session token check success!") + + api.InitClient(sessionToken) + + log.Info("API Client initialization check success!") + + // user, err := resources.NewUser(sessionToken) + + resources.LoadOrCreatePuzzle(2016, 1, sessionToken) + + log.Info("Session token appears to be valid, happy solving!") } // User-specific functions diff --git a/internal/resources/Puzzle.go b/internal/resources/Puzzle.go index 85ce50f..be69962 100644 --- a/internal/resources/Puzzle.go +++ b/internal/resources/Puzzle.go @@ -170,6 +170,8 @@ func newPuzzle(year int, day int, userSession string) *Puzzle { if err != nil { log.Fatal("Unable to load user input for the puzzle.", "error", err) + } else if strings.Contains(string(userInput), "log in") { + log.Fatal("Session token appears to be invalid. Login in a browser and get your new token.") } subMap := make(map[int][]*Submission) diff --git a/internal/resources/User.go b/internal/resources/User.go index 4a37c6f..58bb956 100644 --- a/internal/resources/User.go +++ b/internal/resources/User.go @@ -34,7 +34,7 @@ func (u *User) GetToken() string { func NewUser(token string) (*User, error) { var err error if token == "" { - token, err = session.GetSessionToken() + token, err = session.GetSessionToken(false) if err != nil { return nil, err } diff --git a/internal/resources/UserView.go b/internal/resources/UserView.go index b41b6a4..e64382b 100644 --- a/internal/resources/UserView.go +++ b/internal/resources/UserView.go @@ -186,7 +186,7 @@ func getRowForYear(userToken string, year, day int) []string { } if day < 25 { - for d := day; d <= 25; d++ { + for d := day + 1; d <= 25; d++ { stars[d] = lipgloss.NewStyle().Foreground(styles.NoStarsColor).Render("-") } } diff --git a/internal/session/session.go b/internal/session/session.go index 90818fb..1a67447 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -4,16 +4,25 @@ import ( "errors" "os" "path/filepath" + + "github.com/charmbracelet/log" ) -func GetSessionToken() (string, error) { +// GetSessionToken attempts to get a valid session token. +func GetSessionToken(healthLog bool) (string, error) { sessionToken, err := getTokenFromFile("") if sessionToken != "" { + if healthLog { + log.Info("Found session token in config file.", "token", sessionToken) + } return sessionToken, err } sessionToken, err = getTokenFromEnv() if sessionToken != "" { + if healthLog { + log.Info("Found session token in environment variable.", "token", sessionToken) + } return sessionToken, err }