-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (73 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"flag"
"fmt"
"os"
"time"
_ "github.com/joho/godotenv/autoload"
"github.com/joshmenden/aocplz/internal/fetch"
"github.com/joshmenden/aocplz/internal/printit"
"github.com/joshmenden/aocplz/internal/solve"
)
var (
now = time.Now()
day = now.Day() + 1
year = now.Year()
requiredVars = []string{
"AOCPLZ_SESSION_TOKEN",
"AOCPLZ_ROOT_DIR",
}
)
func main() {
err := validateEnvVars()
if err != nil {
handleError(err)
}
fetchCmd := flag.NewFlagSet("fetch", flag.ExitOnError)
dayPtr := fetchCmd.Int("day", day, "the aoc day you're solving")
yearPtr := fetchCmd.Int("year", year, "the aoc year you're solving")
waitPtr := fetchCmd.Bool("wait", false, "whether or not to wait for the puzzle to be ready")
dontOpenPtr := fetchCmd.Bool("dont-open", false, "if this flag is included, the puzzle won't automatically open in your browser")
if len(os.Args) < 2 {
handleError(fmt.Errorf("expected 'fetch' or 'solve' command"))
}
switch os.Args[1] {
case "fetch":
fetchCmd.Parse(os.Args[2:])
puzzleReady, diff := fetch.IsPuzzleReady(*dayPtr, *yearPtr)
if !puzzleReady {
msg := fmt.Sprintf("the puzzle for day %v won't be ready until %v from now", *dayPtr, diff)
if *waitPtr {
printit.Info(msg)
err = fetch.WaitForPuzzle(dayPtr, yearPtr, dontOpenPtr)
if err != nil {
handleError(err)
}
} else {
handleError(fmt.Errorf(msg))
}
} else {
err := fetch.FetchDayInput(dayPtr, yearPtr, dontOpenPtr)
if err != nil {
handleError(err)
}
}
printit.Success("Fetch Complete, Happy Coding!")
case "solve":
solve.SolveDayPuzzle()
default:
handleError(fmt.Errorf("expected 'fetch' or 'solve' command"))
}
}
func handleError(err error) {
printit.Error(err.Error())
os.Exit(1)
}
func validateEnvVars() error {
for _, varName := range requiredVars {
if os.Getenv(varName) == "" {
return fmt.Errorf("%s must be set", varName)
}
}
return nil
}