Skip to content

Commit

Permalink
Merge pull request #7 from StreakInTheSky/go-install-aoc
Browse files Browse the repository at this point in the history
Go install aoc
  • Loading branch information
StreakInTheSky authored Dec 9, 2022
2 parents 8cd2d5d + 628c020 commit 906343f
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 41 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions cli/cli.go → aoc/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package main

import (
"errors"
Expand All @@ -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" {
Expand All @@ -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) {
Expand Down
26 changes: 13 additions & 13 deletions cli/cli_test.go → aoc/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package main

import (
"testing"
Expand All @@ -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")
}
})
Expand All @@ -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())
}
})
Expand All @@ -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")
}

Expand All @@ -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")
}

Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down
9 changes: 3 additions & 6 deletions fetcher/fetcher.go → aoc/fetcher.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions fetcher/fetcher_test.go → aoc/fetcher_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fetcher
package main

import (
"errors"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}

Expand All @@ -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)
}
})
Expand All @@ -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)
}
})
Expand All @@ -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())
}
})
Expand All @@ -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)
}
})
Expand All @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions aoc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/streakinthesky/adventofcode-fetcher/aoc

go 1.19
11 changes: 4 additions & 7 deletions main.go → aoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,29 @@ import (
"fmt"
"io"
"os"

"github.com/streakinthesky/adventofcode-fetcher/cli"
"github.com/streakinthesky/adventofcode-fetcher/fetcher"
)

var (
createFile = os.Create
)

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)
}
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions go.mod

This file was deleted.

0 comments on commit 906343f

Please sign in to comment.