Skip to content

Commit fda8e78

Browse files
authored
Don't store inputs, as @ericwastl asked (#57)
* refactor: Don't store inputs; Move command to internal; Regression test * chore: Remove logs * docs: Add comments * fix: Sonar code smells
1 parent c1068f6 commit fda8e78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1176
-3433
lines changed

.github/workflows/release.yml

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,113 @@ jobs:
3333
go version
3434
shell: bash
3535
36+
- name: Set up cache
37+
uses: actions/cache@v2.1.7
38+
env:
39+
cache-name: cache-vendored-tools-${{ secrets.CACHE_VERSION }}
40+
with:
41+
path: |
42+
${{ env.GOBIN }}
43+
~/.cache/go-build
44+
~/go/pkg/mod
45+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
46+
restore-keys: |
47+
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
48+
49+
- name: Install tools
50+
run: |
51+
make install-tools
52+
shell: bash
53+
54+
- name: Build
55+
run: |
56+
make build
57+
regression_test:
58+
strategy:
59+
fail-fast: false
60+
max-parallel: 2
61+
matrix:
62+
os: [ 'ubuntu-20.04' ]
63+
go: [ '1.17' ]
64+
runs-on: ${{ matrix.os }}
65+
name: Lint & Test & Build & Release
66+
steps:
67+
- name: Checkout
68+
uses: actions/checkout@v2.4.0
69+
with:
70+
fetch-depth: 0
71+
72+
- name: Set up go
73+
uses: actions/setup-go@v2.1.4
74+
with:
75+
stable: 'true'
76+
go-version: ${{ matrix.go }}
77+
78+
- run: |
79+
go version
80+
shell: bash
81+
82+
- name: Set up cache
83+
uses: actions/cache@v2.1.7
84+
env:
85+
cache-name: cache-vendored-tools-${{ secrets.CACHE_VERSION }}
86+
with:
87+
path: |
88+
${{ env.GOBIN }}
89+
~/.cache/go-build
90+
~/go/pkg/mod
91+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
92+
restore-keys: |
93+
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
94+
95+
- name: Install tools
96+
run: |
97+
make install-tools
98+
shell: bash
99+
100+
- name: Run regression tests
101+
run: |
102+
make test-regression
103+
shell: bash
104+
105+
linting:
106+
strategy:
107+
fail-fast: false
108+
max-parallel: 1
109+
matrix:
110+
os: [ 'ubuntu-20.04' ]
111+
go: [ '1.17' ]
112+
runs-on: ${{ matrix.os }}
113+
name: Lint & Test & Build & Release
114+
steps:
115+
- name: Checkout
116+
uses: actions/checkout@v2.4.0
117+
with:
118+
fetch-depth: 0
119+
120+
- name: Set up go
121+
uses: actions/setup-go@v2.1.4
122+
with:
123+
stable: 'true'
124+
go-version: ${{ matrix.go }}
125+
126+
- run: |
127+
go version
128+
shell: bash
129+
130+
- name: Set up cache
131+
uses: actions/cache@v2.1.7
132+
env:
133+
cache-name: cache-vendored-tools-${{ secrets.CACHE_VERSION }}
134+
with:
135+
path: |
136+
${{ env.GOBIN }}
137+
~/.cache/go-build
138+
~/go/pkg/mod
139+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
140+
restore-keys: |
141+
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
142+
36143
- name: Install tools
37144
run: |
38145
make install-tools
@@ -48,9 +155,48 @@ jobs:
48155
make lint-pipeline
49156
shell: bash
50157

51-
- name: Test
158+
release:
159+
needs: [build, regression_test, linting]
160+
strategy:
161+
fail-fast: false
162+
max-parallel: 1
163+
matrix:
164+
os: [ 'ubuntu-20.04' ]
165+
go: [ '1.17' ]
166+
runs-on: ${{ matrix.os }}
167+
name: Lint & Test & Build & Release
168+
steps:
169+
- name: Checkout
170+
uses: actions/checkout@v2.4.0
171+
with:
172+
fetch-depth: 0
173+
174+
- name: Set up go
175+
uses: actions/setup-go@v2.1.4
176+
with:
177+
stable: 'true'
178+
go-version: ${{ matrix.go }}
179+
180+
- run: |
181+
go version
182+
shell: bash
183+
184+
- name: Set up cache
185+
uses: actions/cache@v2.1.7
186+
env:
187+
cache-name: cache-vendored-tools-${{ secrets.CACHE_VERSION }}
188+
with:
189+
path: |
190+
${{ env.GOBIN }}
191+
~/.cache/go-build
192+
~/go/pkg/mod
193+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
194+
restore-keys: |
195+
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/tools/go.sum') }}
196+
197+
- name: Install tools
52198
run: |
53-
make test
199+
make install-tools
54200
shell: bash
55201

56202
- name: Run GoReleaser

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ test-cover:
4040
./scripts/tests/coverage.sh
4141
.PHONY: test-cover
4242

43+
## Test regression.
44+
test-regression:
45+
./scripts/tests/run-regression.sh
46+
.PHONY: test-regression
47+
4348
## Tests sonar report generate.
4449
test-sonar-report:
4550
./scripts/tests/sonar-report.sh

cmd/aoc-cli/flags.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const (
99
flagShortElapsed = "e"
1010
flagBenchmark = "bench"
1111
flagShortBenchmark = "b"
12+
flagSession = "session"
13+
flagShortSession = "s"
1214
)
1315

1416
func cmdRunFlags() []cli.Flag {
@@ -42,7 +44,22 @@ func cmdRunFlags() []cli.Flag {
4244
HasBeenSet: false,
4345
}
4446

45-
res = append(res, &elapsed, &benchmark)
47+
session := cli.StringFlag{
48+
Name: flagSession,
49+
Aliases: []string{flagShortSession},
50+
Usage: "AOC auth session to get inputs",
51+
EnvVars: []string{"AOC_SESSION"},
52+
FilePath: "",
53+
Required: true,
54+
Hidden: false,
55+
TakesFile: false,
56+
Value: "",
57+
DefaultText: "",
58+
Destination: nil,
59+
HasBeenSet: false,
60+
}
61+
62+
res = append(res, &elapsed, &benchmark, &session)
4663

4764
return res
4865
}

cmd/aoc-cli/handlers.go

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bytes"
54
"context"
65
"errors"
76
"fmt"
@@ -15,8 +14,8 @@ import (
1514
log "github.com/obalunenko/logger"
1615
"github.com/urfave/cli/v2"
1716

17+
"github.com/obalunenko/advent-of-code/internal/command"
1818
"github.com/obalunenko/advent-of-code/internal/puzzles"
19-
"github.com/obalunenko/advent-of-code/internal/puzzles/input"
2019
)
2120

2221
func onExit(_ context.Context) cli.AfterFunc {
@@ -69,7 +68,8 @@ func notFound(ctx context.Context) cli.CommandNotFoundFunc {
6968
}
7069
func menu(ctx context.Context) cli.ActionFunc {
7170
return func(c *cli.Context) error {
72-
ctx = contextWithOptions(ctx, optionsFromCli(c))
71+
ctx = command.ContextWithOptions(ctx, optionsFromCli(c))
72+
ctx = command.ContextWithSession(ctx, sessionFromCli(c))
7373

7474
years := puzzles.GetYears()
7575

@@ -166,7 +166,7 @@ func handlePuzzleChoices(ctx context.Context, year string, opt promptui.Select)
166166

167167
stopSpinner := setSpinner()
168168

169-
res, err := run(ctx, year, choice)
169+
res, err := command.Run(ctx, year, choice)
170170
if err != nil {
171171
log.WithError(ctx, err).Error("Puzzle run failed")
172172

@@ -209,51 +209,20 @@ func optionsFromCli(c *cli.Context) []puzzles.RunOption {
209209
return options
210210
}
211211

212-
type optsCtxKey struct{}
212+
func sessionFromCli(c *cli.Context) string {
213+
var sess string
213214

214-
func contextWithOptions(ctx context.Context, opts []puzzles.RunOption) context.Context {
215-
if len(opts) == 0 {
216-
return ctx
215+
sess = c.String(flagSession)
216+
if sess != "" {
217+
return sess
217218
}
218219

219-
return context.WithValue(ctx, optsCtxKey{}, opts)
220-
}
221-
222-
func optionsFromContext(ctx context.Context) []puzzles.RunOption {
223-
v := ctx.Value(optsCtxKey{})
224-
225-
opts, ok := v.([]puzzles.RunOption)
226-
if !ok {
227-
return []puzzles.RunOption{}
228-
}
229-
230-
return opts
231-
}
232-
233-
func run(ctx context.Context, year, day string) (puzzles.Result, error) {
234-
s, err := puzzles.GetSolver(year, day)
235-
if err != nil {
236-
return puzzles.Result{}, fmt.Errorf("failed to get solver: %w", err)
237-
}
238-
239-
fullName, err := puzzles.MakeName(s.Year(), s.Day())
240-
if err != nil {
241-
return puzzles.Result{}, fmt.Errorf("failed to make full name: %w", err)
242-
}
243-
244-
asset, err := input.Asset(fmt.Sprintf("%s.txt", fullName))
245-
if err != nil {
246-
return puzzles.Result{}, fmt.Errorf("failed to open input data: %w", err)
247-
}
248-
249-
opts := optionsFromContext(ctx)
250-
251-
res, err := puzzles.Run(s, bytes.NewReader(asset), opts...)
252-
if err != nil {
253-
return puzzles.Result{}, fmt.Errorf("failed to run [%s]: %w", fullName, err)
220+
sess = c.String(flagShortSession)
221+
if sess != "" {
222+
return sess
254223
}
255224

256-
return res, nil
225+
return ""
257226
}
258227

259228
// setSpinner runs the displaying of spinner to handle long time operations. Returns stop func.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.17
55
require (
66
github.com/briandowns/spinner v1.13.0
77
github.com/manifoldco/promptui v0.9.0
8+
github.com/obalunenko/getenv v0.0.0-20211201164541-6fa80e1bd028
89
github.com/obalunenko/logger v0.1.0
910
github.com/obalunenko/version v1.1.0
1011
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx
2727
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
2828
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
2929
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
30+
github.com/obalunenko/getenv v0.0.0-20211201164541-6fa80e1bd028 h1:J9Cxgf004HVyZwJpJtr7f9x/jUZZQO+WX2D2y5zyBHk=
31+
github.com/obalunenko/getenv v0.0.0-20211201164541-6fa80e1bd028/go.mod h1:xXXx25+fQ7SGRkrLwTqCfZ96XAJQGtGZb16j3Qd0Ues=
3032
github.com/obalunenko/logger v0.1.0 h1:CSdZZPXznz8DJQ1zFoy/uwCyynAVsZHuYdNLmtC/gSg=
3133
github.com/obalunenko/logger v0.1.0/go.mod h1:09zbNCrGvrIzuiKLQFy2yWWDh3R/lXkz3TZUtUa6FRg=
3234
github.com/obalunenko/version v1.1.0 h1:yVua7OHnK3+MJpendeMmAlfzVmq7R1h8MO3Ufz7HEec=

internal/command/command.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Package command holds functionality for running puzzle solving command.
2+
package command
3+
4+
import (
5+
"bytes"
6+
"context"
7+
"fmt"
8+
9+
"github.com/obalunenko/advent-of-code/internal/puzzles"
10+
"github.com/obalunenko/advent-of-code/internal/puzzles/input"
11+
)
12+
13+
// Run runs puzzle solving for passed year/day date.
14+
func Run(ctx context.Context, year, day string) (puzzles.Result, error) {
15+
s, err := puzzles.GetSolver(year, day)
16+
if err != nil {
17+
return puzzles.Result{}, fmt.Errorf("failed to get solver: %w", err)
18+
}
19+
20+
fullName, err := puzzles.MakeName(s.Year(), s.Day())
21+
if err != nil {
22+
return puzzles.Result{}, fmt.Errorf("failed to make full name: %w", err)
23+
}
24+
25+
asset, err := input.Get(ctx, input.Date{
26+
Year: year,
27+
Day: day,
28+
}, SessionFromContext(ctx))
29+
if err != nil {
30+
return puzzles.Result{}, err
31+
}
32+
33+
opts := OptionsFromContext(ctx)
34+
35+
res, err := puzzles.Solve(s, bytes.NewReader(asset), opts...)
36+
if err != nil {
37+
return puzzles.Result{}, fmt.Errorf("failed to run [%s]: %w", fullName, err)
38+
}
39+
40+
return res, nil
41+
}

0 commit comments

Comments
 (0)