diff --git a/README.md b/README.md index 00f2156..06018af 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ # adventofcode-fetcher -Fetches inputs from Advent Of Code events +CLI command that fetches inputs for Advent Of Code event puzzles + +## Installation +### go install +``` +go install github.com/streakinthesky/adventofcode-fetcher/aoc +``` + +## To Use +Log into (Advent Of Code)[https://adventofcode.com] and grab the value of the cookie named `session` with your browser's dev tools([for chrome](https://developer.chrome.com/docs/devtools/storage/cookies/)). + +You can copy and paste the value in a file called `session` in the current directory. It must be the raw value on the first line of the file. Then use the `aoc` command with the url of the puzzle inputs you want to fetch: +``` +aoc fetch https://adventofcode.com/2022/day/1 +``` + +OR + +You can pass it directly into the command using the `-session` flag (ie. the session value is 1234567890abcdef) +``` +aoc fetch --session=1234567890abcdef https://adventofcode.com/2022/day/1 +``` + +OR + +You can save it in any file and pass the file path into the `-session` flag (ie. saved in a file `../path/to/file`) +``` +aoc fetch --session ../path/to/file https://adventofcode.com/2022/day/1 +``` + +These will save the inputs for the event in a file called `inputs.txt` in the current directory. diff --git a/cli/cli.go b/aoc/cli.go similarity index 88% rename from cli/cli.go rename to aoc/cli.go index 83be2ce..a1448f6 100644 --- a/cli/cli.go +++ b/aoc/cli.go @@ -1,4 +1,4 @@ -package cli +package main import ( "errors" @@ -18,7 +18,7 @@ var sessionFlag = flag.String("session", "./session", "session token from advent const SESSION_TOKEN = "AOC_SESSION" -func Run() (url, sessionParam string, err error) { +func run() (url, sessionParam string, err error) { flag.Parse() args := initArgs() if len(args) < 1 || args[0] != "fetch" { @@ -36,7 +36,7 @@ func isPath(input string) bool { return regexp.MustCompile("^[./]").MatchString(input) } -func GrabSessionID(sessionParam string) (sessionID string, err error) { +func grabSessionID(sessionParam string) (sessionID string, err error) { err = errors.New("No session id found") if isPath(sessionParam) { diff --git a/cli/cli_test.go b/aoc/cli_test.go similarity index 90% rename from cli/cli_test.go rename to aoc/cli_test.go index f17b99e..3bda844 100644 --- a/cli/cli_test.go +++ b/aoc/cli_test.go @@ -1,4 +1,4 @@ -package cli +package main import ( "testing" @@ -9,7 +9,7 @@ func TestParsingArgs(t *testing.T) { args := []string{"command"} initArgs = mockFlagArgs(args) - if _, _, err := Run(); err == nil { + if _, _, err := run(); err == nil { t.Error("Should return error if no args") } }) @@ -18,7 +18,7 @@ func TestParsingArgs(t *testing.T) { args := []string{"fetch", "url"} initArgs = mockFlagArgs(args) - if _, _, err := Run(); err != nil { + if _, _, err := run(); err != nil { t.Errorf("First argument as fetch should be valid. Got error: %s", err.Error()) } }) @@ -29,7 +29,7 @@ func TestParsingArgs(t *testing.T) { initArgs = mockFlagArgs(args) expectedErrMsg := "Did you want to call \"fetch\"?" - if _, _, err = Run(); err == nil { + if _, _, err = run(); err == nil { t.Error("Expected an error") } @@ -45,7 +45,7 @@ func TestParsingArgs(t *testing.T) { initArgs = mockFlagArgs(args) expectedErrMsg := "Please enter a url" - if _, _, err = Run(); err == nil { + if _, _, err = run(); err == nil { t.Error("Expected and error") } @@ -59,7 +59,7 @@ func TestParsingArgs(t *testing.T) { args := []string{"fetch", "url"} initArgs = mockFlagArgs(args) - url, _, err := Run() + url, _, err := run() if err != nil { t.Errorf("Should not have error, got error: %s", err.Error()) } @@ -75,7 +75,7 @@ func TestParsingArgs(t *testing.T) { defaultSessionFlagVal := "./session" - _, sessionID, err := Run() + _, sessionID, err := run() if err != nil { t.Errorf("Should not have an error, got error: %s", err.Error()) } @@ -86,11 +86,11 @@ func TestParsingArgs(t *testing.T) { }) } -func TestGrabbingSessionId(t *testing.T) { +func TestGrabSessionId(t *testing.T) { t.Run("Returns error if no cookie found", func(t *testing.T) { readFile = mockReadFile([]byte{}, nil) - sessionID, err := GrabSessionID("") + sessionID, err := grabSessionID("") if err == nil { t.Errorf("Expected an error, got %s", sessionID) } @@ -102,7 +102,7 @@ func TestGrabbingSessionId(t *testing.T) { pathToFile := "/path" readFile = mockReadFile(mockFile, nil) - sessionID, err := GrabSessionID(pathToFile) + sessionID, err := grabSessionID(pathToFile) if err != nil { t.Errorf("Expected no error, got %s", err) } @@ -120,7 +120,7 @@ func TestGrabbingSessionId(t *testing.T) { pathToFile := "/path" readFile = mockReadFile([]byte(mockFile), nil) - sessionID, err := GrabSessionID(pathToFile) + sessionID, err := grabSessionID(pathToFile) if err != nil { t.Errorf("Expected no error, got %s", err.Error()) } @@ -132,7 +132,7 @@ func TestGrabbingSessionId(t *testing.T) { t.Run("Should return session id from params", func(t *testing.T) { expectedID := "abc123" - actualID, err := GrabSessionID(expectedID) + actualID, err := grabSessionID(expectedID) if err != nil { t.Errorf("Expected no error, got error: %s", err.Error()) } @@ -147,7 +147,7 @@ func TestGrabbingSessionId(t *testing.T) { readFile = mockReadFile([]byte(expectedId), nil) idPath := "/file/path" - sessionID, err := GrabSessionID(idPath) + sessionID, err := grabSessionID(idPath) if err != nil { t.Errorf("Expected no error, got error: %s", err.Error()) } diff --git a/fetcher/fetcher.go b/aoc/fetcher.go similarity index 87% rename from fetcher/fetcher.go rename to aoc/fetcher.go index e25eea8..2a50d5b 100644 --- a/fetcher/fetcher.go +++ b/aoc/fetcher.go @@ -1,7 +1,4 @@ -// Package fetcher grabs inputs for the user from Advent of Code. -// Validates url inputs for valid Advent of Code urls -// Makes sure there is a cookie for the request -package fetcher +package main import ( "errors" @@ -80,7 +77,7 @@ func checkCookie(cookie http.Cookie) error { } // MakeCookie makes a session cookie from a sessionID -func MakeCookie(sessionID string) (cookie http.Cookie, err error) { +func makeCookie(sessionID string) (cookie http.Cookie, err error) { if len(sessionID) == 0 { return cookie, errors.New("sessionId must have a value") } @@ -93,7 +90,7 @@ func MakeCookie(sessionID string) (cookie http.Cookie, err error) { } // Fetch fetches input for advent of code url and a user's session cookie -func Fetch(url string, cookie http.Cookie) (res *http.Response, err error) { +func fetch(url string, cookie http.Cookie) (res *http.Response, err error) { if err = validateURL(url); err != nil { return res, err } diff --git a/fetcher/fetcher_test.go b/aoc/fetcher_test.go similarity index 94% rename from fetcher/fetcher_test.go rename to aoc/fetcher_test.go index b481b75..98a97f0 100644 --- a/fetcher/fetcher_test.go +++ b/aoc/fetcher_test.go @@ -1,4 +1,4 @@ -package fetcher +package main import ( "errors" @@ -174,7 +174,7 @@ func TestCreatingCookie(t *testing.T) { t.Run("Should create session cookie", func(t *testing.T) { sessionID := "abc" - cookie, _ := MakeCookie(sessionID) + cookie, _ := makeCookie(sessionID) if cookie.Value != sessionID { t.Errorf("Expected cookie to have value %s, got %s", sessionID, cookie.Value) } @@ -183,7 +183,7 @@ func TestCreatingCookie(t *testing.T) { t.Run("Should return error if no sessionID", func(t *testing.T) { var sessionID string - if _, err := MakeCookie(sessionID); err == nil { + if _, err := makeCookie(sessionID); err == nil { t.Error("Expected error when no sessionID") } @@ -203,7 +203,7 @@ func TestFetching(t *testing.T) { } client = &mockClient{res: http.Response{StatusCode: 200}} - if _, err := Fetch(url, cookie); err != nil { + if _, err := fetch(url, cookie); err != nil { t.Error(err) } }) @@ -216,7 +216,7 @@ func TestFetching(t *testing.T) { } client = &mockClient{} - if _, err := Fetch(url, cookie); err == nil { + if _, err := fetch(url, cookie); err == nil { t.Errorf("Should return an error with invalid url: %s", url) } }) @@ -228,7 +228,7 @@ func TestFetching(t *testing.T) { } client = &mockClient{} - if _, err := Fetch(url, cookie); err == nil { + if _, err := fetch(url, cookie); err == nil { t.Errorf("Should return error with invalid cooke: %s", cookie.String()) } }) @@ -249,7 +249,7 @@ func TestFetching(t *testing.T) { err: errors.New("There was an error fetching the site"), } - if res, err := Fetch(url, cookie); err == nil { + if res, err := fetch(url, cookie); err == nil { t.Errorf("Request should return an error with status code %d, but got %d", expected.StatusCode, res.StatusCode) } }) @@ -268,7 +268,7 @@ func TestFetching(t *testing.T) { res: expectedRes, } - res, err := Fetch(url, cookie) + res, err := fetch(url, cookie) if err != nil { t.Errorf("Should not have an error, but got %s", err) } diff --git a/aoc/go.mod b/aoc/go.mod new file mode 100644 index 0000000..8d030f2 --- /dev/null +++ b/aoc/go.mod @@ -0,0 +1,3 @@ +module github.com/streakinthesky/adventofcode-fetcher/aoc + +go 1.19 diff --git a/main.go b/aoc/main.go similarity index 80% rename from main.go rename to aoc/main.go index 8afe044..75df2cc 100644 --- a/main.go +++ b/aoc/main.go @@ -6,9 +6,6 @@ import ( "fmt" "io" "os" - - "github.com/streakinthesky/adventofcode-fetcher/cli" - "github.com/streakinthesky/adventofcode-fetcher/fetcher" ) var ( @@ -16,22 +13,22 @@ var ( ) func main() { - url, sessionFlag, err := cli.Run() + url, sessionFlag, err := run() if err != nil { handleError(err, 2) } - sessionID, err := cli.GrabSessionID(sessionFlag) + sessionID, err := grabSessionID(sessionFlag) if err != nil { handleError(err, 2) } - cookie, err := fetcher.MakeCookie(sessionID) + cookie, err := makeCookie(sessionID) if err != nil { handleError(err, 1) } - res, err := fetcher.Fetch(url, cookie) + res, err := fetch(url, cookie) if err != nil { handleError(err, 18) } diff --git a/main_test.go b/aoc/main_test.go similarity index 100% rename from main_test.go rename to aoc/main_test.go diff --git a/go.mod b/go.mod deleted file mode 100644 index 67279ba..0000000 --- a/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/streakinthesky/adventofcode-fetcher - -go 1.19