Skip to content

Commit

Permalink
Better health command. Fixed user view printing and leaderboard parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DaltonSW committed Dec 1, 2024
1 parent 3a8439a commit 1e95734
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion aocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
50 changes: 38 additions & 12 deletions cmd/aocli/aocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/resources/Puzzle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/UserView.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("-")
}
}
Expand Down
11 changes: 10 additions & 1 deletion internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1e95734

Please sign in to comment.