Skip to content

Commit a5b83f3

Browse files
authored
Refactor(): Split years and puzzles selection into separate menu layers (#12)
* 2019/day05: Add puzzle spec and stub for solution * input/2019: Add day05 input * puzzles/solutions: Register day05 puzzle * update gitignore * Update dependencies; Fix markdown linters * chore(): upgrade go version to 1.14 * chore(): gomnd linter config * chore(): Linters fix * upgrade vendor * chore(): Update vendor * chore(): Upgrade go version and dependencies * chore(): Update tools and add go version to Readme * fix(): Fix after merge * refactor(): Split menu to separate layers years selection and puzzles. * fix(): Remove day05 solution.
1 parent b5ebb8e commit a5b83f3

34 files changed

+523
-215
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out*
13+
.vscode/
1314

1415
.idea
1516

.golangci.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ linters-settings:
3535
simple: true
3636
range-loops: true # Report preallocation suggestions on range loops, true by default
3737
for-loops: false # Report preallocation suggestions on for loops, false by default
38+
gomnd:
39+
settings:
40+
mnd:
41+
# the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
42+
checks: case,condition,operation,return
3843
gocritic:
3944
enabled-checks:
4045
- docStub
@@ -78,24 +83,20 @@ linters-settings:
7883
underef:
7984
skipRecvDeref: true
8085

81-
8286
run:
83-
84-
8587
skip-dirs:
8688
- vendor/
8789

8890
output:
8991
format: colored-line-number
9092

9193
issues:
92-
# Excluding configuration per-path, per-linter, per-text and per-source
94+
# Excluding configuration per-path, per-linter, per-text and per-source
9395
exclude-rules:
94-
# Exclude some linters from running on tests files.
95-
- path: _test\.go
96-
linters:
96+
# Exclude some linters from running on tests files.
97+
- path: _test\.go
98+
linters:
9799
- gocyclo
98100
- funlen
99101
- dupl
100102
- gosec
101-

.goreleaser.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ builds:
1212
- linux
1313
goarch:
1414
- amd64
15-
- 386
1615
- arm
1716
env:
1817
- CGO_ENABLED=0

.tools/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
This package is used to track CI tool dependencies.
1+
# Tools
22

3-
* [Why does installing a tool via go get fail with error cannot find main module](https://github.com/golang/go/wiki/Modules#why-does-installing-a-tool-via-go-get-fail-with-error-cannot-find-main-module)
3+
This package used to track CI tool dependencies.
4+
5+
- [Why does installing a tool via go get fail with error cannot find main module](https://github.com/golang/go/wiki/Modules#why-does-installing-a-tool-via-go-get-fail-with-error-cannot-find-main-module)

.tools/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module linters
1+
module tools
22

33
go 1.15
44

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
script:
3636
- echo "Build stage"
3737
- make compile
38-
after_success:
38+
after_success: true
3939
deploy:
4040
skip_cleanup: true
4141
provider: script
@@ -44,4 +44,4 @@ jobs:
4444
tags: true
4545
condition: "$TRAVIS_OS_NAME = linux"
4646
addons:
47-
ssh_known_hosts: github.com
47+
ssh_known_hosts: github.com

Makefile

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
11
NAME=aoc-cli
22
BIN_DIR=./bin
33

4-
# COLORS
5-
GREEN := $(shell tput -Txterm setaf 2)
6-
YELLOW := $(shell tput -Txterm setaf 3)
7-
WHITE := $(shell tput -Txterm setaf 7)
8-
RESET := $(shell tput -Txterm sgr0)
9-
10-
114
TARGET_MAX_CHAR_NUM=20
125

13-
14-
define colored
15-
@echo '${GREEN}$1${RESET}'
16-
endef
17-
186
## Show help
197
help:
208
${call colored, help is running...}
219
@echo ''
2210
@echo 'Usage:'
23-
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
11+
@echo ' make <target>'
2412
@echo ''
2513
@echo 'Targets:'
2614
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
2715
helpMessage = match(lastLine, /^## (.*)/); \
2816
if (helpMessage) { \
2917
helpCommand = substr($$1, 0, index($$1, ":")-1); \
3018
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
31-
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
19+
printf " %-$(TARGET_MAX_CHAR_NUM)s %s\n", helpCommand, helpMessage; \
3220
} \
3321
} \
3422
{ lastLine = $$0 }' $(MAKEFILE_LIST)
23+
.PHONY: help
3524

3625
## app compile
3726
compile:
38-
${call colored, compile is running...}
3927
./scripts/compile.sh
4028
.PHONY: compile
4129

4230
## Cross os compile
4331
cross-compile:
44-
${call colored, compile is running...}
4532
./scripts/cross-compile.sh
4633
.PHONY: cross-compile
4734

4835
## lint project
4936
lint:
50-
${call colored, lint is running...}
5137
./scripts/run-linters.sh
5238
.PHONY: lint
5339

5440
## Lint in CI
5541
lint-ci:
56-
${call colored, lint_ci is running...}
5742
./scripts/run-linters-ci.sh
5843
.PHONY: lint-ci
5944

@@ -64,37 +49,31 @@ pretty-markdown:
6449

6550
## Test all packages
6651
test:
67-
${call colored, test is running...}
6852
./scripts/run-tests.sh
6953
.PHONY: test
7054

7155
## Test coverage
7256
test-cover:
73-
${call colored, test-cover is running...}
7457
./scripts/coverage.sh
7558
.PHONY: test-cover
7659

7760
## Increase version number
7861
new-version: lint test compile
79-
${call colored, new version is running...}
8062
./scripts/version.sh
8163
.PHONY: new-version
8264

8365
## Release
8466
release:
85-
${call colored, release is running...}
8667
./scripts/release.sh
8768
.PHONY: release
8869

8970
## Fix imports sorting
9071
imports:
91-
${call colored, sort and group imports...}
9272
./scripts/fix-imports.sh
9373
.PHONY: imports
9474

9575
## fetch all dependencies for scripts
9676
dependencies:
97-
${call colored, dependensies is running...}
9877
./scripts/get-dependencies.sh
9978
.PHONY: dependencies
10079

@@ -121,3 +100,14 @@ generate:
121100
.PHONY: generate
122101

123102
.DEFAULT_GOAL := test
103+
104+
## vendor-check if dependencies were not changed.
105+
vendor-check:
106+
./scripts/check-vendor.sh
107+
.PHONY: vendor-check
108+
109+
vet:
110+
./scripts/vet.sh
111+
.PHONY: vet
112+
113+
.DEFAULT_GOAL := test

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# advent-of-code
2+
3+
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/oleg-balunenko/advent-of-code)
14
[![Build Status](https://travis-ci.com/oleg-balunenko/advent-of-code.svg?branch=master)](https://travis-ci.com/oleg-balunenko/advent-of-code)
25
[![Go Report Card](https://goreportcard.com/badge/github.com/oleg-balunenko/advent-of-code)](https://goreportcard.com/report/github.com/oleg-balunenko/advent-of-code)
36
[![codecov](https://codecov.io/gh/oleg-balunenko/advent-of-code/branch/master/graph/badge.svg)](https://codecov.io/gh/oleg-balunenko/advent-of-code)
47
[![GoDoc](https://godoc.org/github.com/oleg-balunenko/advent-of-code?status.svg)](https://godoc.org/github.com/oleg-balunenko/advent-of-code)
58

6-
# advent-of-code
79
My solutions of puzzles for [Advent Of Code](https://adventofcode.com/)
810

911
This repository contains solutions for puzzles and cli tool to run solutions to get answers for input on site.

cmd/aoc-cli/main.go

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
const (
2222
exit = "exit"
23+
back = "back"
2324
)
2425

2526
var (
@@ -42,6 +43,53 @@ func main() {
4243
}
4344
}
4445

46+
func menu() error {
47+
years := puzzles.GetYears()
48+
49+
prompt := promptui.Select{
50+
Label: nil,
51+
Items: append(years, exit),
52+
Size: 0,
53+
CursorPos: 0,
54+
IsVimMode: false,
55+
HideHelp: false,
56+
HideSelected: false,
57+
Templates: nil,
58+
Keys: nil,
59+
Searcher: nil,
60+
StartInSearchMode: false,
61+
Pointer: nil,
62+
Stdin: nil,
63+
Stdout: nil,
64+
}
65+
66+
return handleYearChoices(prompt)
67+
}
68+
69+
func handleYearChoices(opt promptui.Select) error {
70+
for {
71+
_, choice, err := opt.Run()
72+
if err != nil {
73+
return errors.Wrap(err, "prompt failed")
74+
}
75+
76+
if isExit(choice) {
77+
return nil
78+
}
79+
80+
err = menuPuzzle(choice)
81+
if errors.Is(err, errExit) {
82+
return nil
83+
}
84+
85+
if err != nil {
86+
log.Error(err)
87+
88+
continue
89+
}
90+
}
91+
}
92+
4593
func setLogger() {
4694
l, err := log.ParseLevel(*logLevel)
4795
if err != nil {
@@ -70,14 +118,14 @@ func setLogger() {
70118
log.SetFormatter(formatter)
71119
}
72120

73-
func menu() error {
74-
solvers := puzzles.Solvers()
121+
func menuPuzzle(year string) error {
122+
solvers := puzzles.NamesByYear(year)
75123

76124
pageSize := 20
77125

78126
prompt := promptui.Select{
79-
Label: "Puzzles menu (input 'exit' for exit)",
80-
Items: append(solvers, exit),
127+
Label: "Puzzles menu (exit' for exit; back - to return to year selection)",
128+
Items: append(solvers, back, exit),
81129
Size: pageSize,
82130
CursorPos: 0,
83131
IsVimMode: false,
@@ -92,28 +140,33 @@ func menu() error {
92140
Stdout: nil,
93141
}
94142

95-
return handleChoices(prompt)
143+
return handlePuzzleChoices(year, prompt)
96144
}
97145

98-
func handleChoices(opt promptui.Select) error {
146+
func handlePuzzleChoices(year string, opt promptui.Select) error {
99147
for {
100148
_, choice, err := opt.Run()
101149
if err != nil {
102150
return errors.Wrap(err, "prompt failed")
103151
}
104152

105153
if isExit(choice) {
154+
return errExit
155+
}
156+
157+
if isBack(choice) {
106158
return nil
107159
}
108160

109-
res, err := run(choice)
161+
res, err := run(year, choice)
110162
if err != nil {
111163
log.Error(err)
112164

113165
continue
114166
}
115167

116168
log.WithFields(log.Fields{
169+
"year": res.Year,
117170
"name": res.Name,
118171
"part1": res.Part1,
119172
"part2": res.Part2,
@@ -125,14 +178,25 @@ func isExit(input string) bool {
125178
return strings.EqualFold(exit, input)
126179
}
127180

128-
func run(puzzle string) (puzzles.Result, error) {
129-
s, err := puzzles.GetSolver(puzzle)
181+
func isBack(input string) bool {
182+
return strings.EqualFold(back, input)
183+
}
184+
185+
var errExit = errors.New("exit is chosen")
186+
187+
func run(year string, name string) (puzzles.Result, error) {
188+
s, err := puzzles.GetSolver(year, name)
130189
if err != nil {
131190
return puzzles.Result{}, errors.Wrap(err, "failed to get solver")
132191
}
133192

193+
fullName, err := puzzles.MakeName(s.Year(), s.Name())
194+
if err != nil {
195+
return puzzles.Result{}, fmt.Errorf("failed to make full name: %w", err)
196+
}
197+
134198
asset, err := input.Asset(filepath.Clean(
135-
filepath.Join(input.InputDir, fmt.Sprintf("%s.txt", s.Name()))))
199+
filepath.Join(input.InputDir, fmt.Sprintf("%s.txt", fullName))))
136200
if err != nil {
137201
return puzzles.Result{}, errors.Wrap(err, "failed to open input data")
138202
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ module github.com/oleg-balunenko/advent-of-code
33
go 1.15
44

55
require (
6+
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
67
github.com/lunixbochs/vtclean v1.0.0 // indirect
78
github.com/manifoldco/promptui v0.8.0
89
github.com/mattn/go-colorable v0.1.8 // indirect
910
github.com/pkg/errors v0.9.1
1011
github.com/sirupsen/logrus v1.7.0
12+
github.com/smartystreets/assertions v1.2.0 // indirect
13+
github.com/smartystreets/goconvey v1.6.4 // indirect
1114
github.com/stretchr/testify v1.6.1
1215
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1 // indirect
16+
golang.org/x/tools v0.0.0-20201105220310-78b158585360 // indirect
1317
)

0 commit comments

Comments
 (0)