Updated commands to have slightly better structuring

This commit is contained in:
Ceferino Patino 2024-12-15 11:45:45 -06:00
commit 3c8f35ae12
Signed by: c4patino
SSH key fingerprint: SHA256:Wu+cU1t+7zVT9wzew/4meNmeulo66NzMqc22MdDBgXI
5 changed files with 53 additions and 19 deletions

View file

@ -26,7 +26,7 @@ var GenerateCmd = &cobra.Command{
folder := fmt.Sprintf("./day%02d", day)
if err := os.Mkdir(folder, 0755); err != nil {
handleErr(err, folder)
log.Fatal(err)
}
if err := generateMainCode(day); err != nil {

View file

@ -1,51 +1,87 @@
package generate
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
)
func retrieveInput(day int) error {
cookieValue := os.Getenv("COOKIE")
const (
baseURL = "https://adventofcode.com/2024/day/%d/input"
host = "adventofcode.com"
filePerm = 0644
userAgent = "github.com/C4theBomb/advent-of-code by c4patino@gmail.com"
cookieEnvName = "COOKIE"
)
func getCookie() (string, error) {
cookieValue := os.Getenv(cookieEnvName)
if cookieValue == "" {
return errors.New("COOKIE environment variable not set")
return "", fmt.Errorf("%s environment variable not set", cookieEnvName)
}
return cookieValue, nil
}
func createHTTPClient(cookieValue string) (*http.Client, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return err
return nil, fmt.Errorf("failed to create cookie jar: %w", err)
}
domainURL := &url.URL{Scheme: "https", Host: host}
client := &http.Client{Jar: jar}
jar.SetCookies(domainURL, []*http.Cookie{{Name: "session", Value: cookieValue}})
url := fmt.Sprintf("https://adventofcode.com/2024/day/%d/input", day)
return client, nil
}
func fetchInput(client *http.Client, day int) ([]byte, error) {
url := fmt.Sprintf(baseURL, day)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("User-Agent", "github.com/C4theBomb/advent-of-code by c4patino@gmail.com")
cookie := &http.Cookie{Name: "session", Value: cookieValue}
req.AddCookie(cookie)
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
return err
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("Unexpected status code: %d\n%s", resp.StatusCode, string(body)))
return nil, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body))
}
return body, nil
}
func retrieveInput(day int) error {
cookieValue, err := getCookie()
if err != nil {
return err
}
client, err := createHTTPClient(cookieValue)
if err != nil {
return err
}
body, err := fetchInput(client, day)
if err != nil {
return err
}
filename := fmt.Sprintf("./day%02d/input.txt", day)

View file

@ -6,7 +6,7 @@ import (
"os/exec"
)
var mainCodeTemplate = `package day%02d
const mainCodeTemplate = `package day%02d
import (
"bufio"

View file

@ -6,7 +6,7 @@ import (
"os/exec"
)
var testCodeTemplate = `package day%02d
const testCodeTemplate = `package day%02d
import (
"os"

View file

@ -22,7 +22,6 @@ import (
"cpatino.com/advent-of-code/2024/day11"
"cpatino.com/advent-of-code/2024/day12"
"cpatino.com/advent-of-code/2024/day13"
"cpatino.com/advent-of-code/2024/day14"
"cpatino.com/advent-of-code/2024/day15"
)
@ -40,7 +39,6 @@ var days = map[int]func(string) (int, int){
11: day11.Run,
12: day12.Run,
13: day13.Run,
14: day14.Run,
15: day15.Run,
}