Skip to content

Commit

Permalink
feat: Integrate termlink (#253)
Browse files Browse the repository at this point in the history
* fix: Goreleaser scripts

* fix: Golangci config

* chore: Bump go-tools image version to v0.5.0

* feat: Add termlink support
  • Loading branch information
obalunenko authored Mar 25, 2023
1 parent 902c556 commit aab8b3c
Show file tree
Hide file tree
Showing 263 changed files with 24,415 additions and 2,877 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
Expand All @@ -82,13 +81,11 @@ linters:
- nolintlint
- rowserrcheck
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- revive
- wsl
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VERSION ?= $(shell git describe --tags $(git rev-list --tags --max-count=1))
APP_NAME?=aoc-cli
SHELL := env APP_NAME=$(APP_NAME) $(SHELL)

GOTOOLS_IMAGE_TAG?=v0.4.3
GOTOOLS_IMAGE_TAG?=v0.5.0
SHELL := env GOTOOLS_IMAGE_TAG=$(GOTOOLS_IMAGE_TAG) $(SHELL)

COMPOSE_TOOLS_FILE=deployments/docker-compose/go-tools-docker-compose.yml
Expand Down
47 changes: 34 additions & 13 deletions cmd/aoc-cli/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/manifoldco/promptui"
promptlist "github.com/manifoldco/promptui/list"
log "github.com/obalunenko/logger"
"github.com/savioxavier/termlink"
"github.com/urfave/cli/v2"

"github.com/obalunenko/advent-of-code/internal/command"
Expand Down Expand Up @@ -146,7 +147,7 @@ func searcher(items []string) promptlist.Searcher {

func handleYearChoices(ctx context.Context, opt promptui.Select) error {
for {
_, choice, err := opt.Run()
_, yearOpt, err := opt.Run()
if err != nil {
if isAbort(err) {
return nil
Expand All @@ -155,11 +156,11 @@ func handleYearChoices(ctx context.Context, opt promptui.Select) error {
return fmt.Errorf("prompt failed: %w", err)
}

if isExit(choice) {
if isExit(yearOpt) {
return nil
}

err = menuPuzzle(ctx, choice)
err = menuPuzzle(ctx, yearOpt)
if err != nil {
if errors.Is(err, errExit) {
return nil
Expand All @@ -174,7 +175,7 @@ func handleYearChoices(ctx context.Context, opt promptui.Select) error {

func handlePuzzleChoices(ctx context.Context, year string, opt promptui.Select) error {
for {
_, choice, err := opt.Run()
_, dayOpt, err := opt.Run()
if err != nil {
if isAbort(err) {
return errExit
Expand All @@ -183,31 +184,47 @@ func handlePuzzleChoices(ctx context.Context, year string, opt promptui.Select)
return fmt.Errorf("prompt failed: %w", err)
}

if isExit(choice) {
if isExit(dayOpt) {
return errExit
}

if isBack(choice) {
if isBack(dayOpt) {
return nil
}

stopSpinner := setSpinner()

res, err := command.Run(ctx, year, choice)
res, err := command.Run(ctx, year, dayOpt)
if err != nil {
log.WithError(ctx, err).Error("Puzzle run failed")

stopSpinner()

if errors.Is(err, command.ErrUnauthorized) {
fmt.Println(termlink.Link("Authorize here", "https://adventofcode.com/auth/login"))

log.WithError(ctx, err).Fatal("Session expired")
}

log.WithError(ctx, err).Error("Puzzle run failed")

continue
}

stopSpinner()
stopSpinner("Solved!")

url := getURL(year, dayOpt)

fmt.Println(res.String())

fmt.Println(termlink.Link("Enter puzzle answers here", url))
}
}

func getURL(year, day string) string {
const urlFmt = "https://adventofcode.com/%s/day/%s"

return fmt.Sprintf(urlFmt, year, day)
}

func isExit(in string) bool {
return strings.EqualFold(exit, in)
}
Expand Down Expand Up @@ -253,13 +270,13 @@ func sessionFromCli(c *cli.Context) string {
}

// setSpinner runs the displaying of spinner to handle long time operations. Returns stop func.
func setSpinner() func() {
func setSpinner() func(msg ...string) {
const delayMs = 100

s := spinner.New(
spinner.CharSets[62],
delayMs*time.Millisecond,
spinner.WithFinalMSG("Solved!"),
spinner.WithFinalMSG(""),
spinner.WithHiddenCursor(true),
spinner.WithColor("yellow"),
spinner.WithWriter(os.Stderr),
Expand All @@ -269,7 +286,11 @@ func setSpinner() func() {

s.Start()

return func() {
return func(msg ...string) {
if len(msg) != 0 {
s.FinalMSG = msg[0]
}

s.Stop()
}
}
33 changes: 33 additions & 0 deletions cmd/aoc-cli/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/obalunenko/advent-of-code/internal/puzzles"
)

func Test_makeMenuItemsList(t *testing.T) {
Expand Down Expand Up @@ -59,3 +61,34 @@ func Test_searcher(t *testing.T) {

assert.False(t, s("1", 2))
}

func Test_getUrl(t *testing.T) {
type args struct {
year string
day string
}

tests := []struct {
name string
args args
want string
}{
{
name: "",
args: args{
year: puzzles.Year2022.String(),
day: puzzles.Day01.String(),
},
want: "https://adventofcode.com/2022/day/1",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getURL(tt.args.year, tt.args.day)

assert.Equalf(t, tt.want, got,
"getURL(%v, %v)", tt.args.year, tt.args.day)
})
}
}
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/obalunenko/getenv v1.4.2
github.com/obalunenko/logger v0.6.0
github.com/obalunenko/version v1.1.0
github.com/savioxavier/termlink v1.2.1
github.com/stretchr/testify v1.8.2
github.com/urfave/cli/v2 v2.25.0
)
Expand All @@ -18,16 +19,17 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evalphobia/logrus_sentry v0.8.2 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/jwalton/go-supportscolor v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
31 changes: 21 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ=
github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/jwalton/go-supportscolor v1.1.0 h1:HsXFJdMPjRUAx8cIW6g30hVSFYaxh9yRQwEWgkAR7lQ=
github.com/jwalton/go-supportscolor v1.1.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/obalunenko/getenv v1.4.2 h1:m/Cer+5/Di+kbpNhtmZMcYXOquqqCjlgtvSc4X89e/M=
github.com/obalunenko/getenv v1.4.2/go.mod h1:trfsd8x+Ulqtw8DHKTmL1DdM9wgUd8IvOUgo22GLn3U=
github.com/obalunenko/logger v0.6.0 h1:Sif4MYn6aGUA5pBTcKkMtJXTZNRZ52EiP4KLiFgMEkI=
Expand All @@ -37,6 +40,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/savioxavier/termlink v1.2.1 h1:O9ZQvk9BPQQK4JQeMB56ZfV8uam0Ts+f97mJme7+dq8=
github.com/savioxavier/termlink v1.2.1/go.mod h1:WA7FTALNwN41NGnmQMIrnjAYTsEhIAZ4RuzgEiB0Jp8=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -52,11 +57,17 @@ github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6f
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
17 changes: 16 additions & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package command
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"time"
Expand All @@ -12,13 +13,27 @@ import (
"github.com/obalunenko/advent-of-code/internal/puzzles/input"
)

var (
// ErrUnauthorized returns when session is empty or invalid.
ErrUnauthorized = errors.New("unauthorized")
)

// Run runs puzzle solving for passed year/day date.
func Run(ctx context.Context, year, day string) (puzzles.Result, error) {
const timeout = time.Second * 30

cli := input.NewFetcher(http.DefaultClient, timeout)

return run(ctx, cli, year, day)
result, err := run(ctx, cli, year, day)
if err != nil {
if errors.Is(err, input.ErrUnauthorized) {
return puzzles.Result{}, ErrUnauthorized
}

return puzzles.Result{}, err
}

return result, nil
}

func run(ctx context.Context, cli input.Fetcher, year, day string) (puzzles.Result, error) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/release/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ export GO_BUILD_LDFLAGS="-s -w \

goreleaser check

goreleaser build --rm-dist --single-target --snapshot
goreleaser build --clean --single-target --snapshot
2 changes: 1 addition & 1 deletion scripts/release/local-snapshot-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ export GO_BUILD_LDFLAGS="-s -w \
-X ${BUILDINFO_VARS_PKG}.appname=${APP} \
-X ${BUILDINFO_VARS_PKG}.goversion=${GOVERSION}"

goreleaser --snapshot --skip-publish --rm-dist
goreleaser --snapshot --skip-publish --clean
2 changes: 1 addition & 1 deletion scripts/release/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ export GO_BUILD_LDFLAGS="-s -w \
-X ${BUILDINFO_VARS_PKG}.appname=${APP} \
-X ${BUILDINFO_VARS_PKG}.goversion=${GOVERSION}"

goreleaser release --rm-dist
goreleaser release --clean
5 changes: 0 additions & 5 deletions vendor/github.com/fatih/color/.travis.yml

This file was deleted.

27 changes: 0 additions & 27 deletions vendor/github.com/fatih/color/Gopkg.lock

This file was deleted.

Loading

0 comments on commit aab8b3c

Please sign in to comment.