Add support for multiple years

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-11-26 22:20:28 +00:00
parent 27325316ba
commit 412646bec5
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
3 changed files with 57 additions and 3 deletions

View file

@ -15,7 +15,8 @@ const (
)
var args struct {
ChallengeDay *int `arg:"-d,--day" help:"challenge day number to run"`
Year string `arg:"-y,--year" help:"AoC year to use"`
ChallengeDay *int `arg:"-d,--day" help:"challenge day number to run"`
Implementation string `arg:"-i,--implementation" help:"implementation to use"`
}
@ -23,8 +24,14 @@ func run() error {
arg.MustParse(&args)
// List and select year
selectedYear, err := selectYear(challengeDir)
if err != nil {
return err
}
// List and select challenges
selectedChallenge, err := selectChallenge(challengeDir)
selectedChallenge, err := selectChallenge(selectedYear)
if err != nil {
return err
}

View file

@ -48,7 +48,7 @@ func (g *golangRunner) Run() chan ResultOrError {
}
// determine package import path
buildPath := fmt.Sprintf("github.com/codemicro/adventOfCode/challenges/%s", filepath.Base(g.dir))
buildPath := fmt.Sprintf("github.com/codemicro/adventOfCode/challenges/%s/%s", filepath.Base(filepath.Dir(g.dir)), filepath.Base(g.dir))
importPath := buildPath + "/go"
// generate code

View file

@ -6,6 +6,8 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/codemicro/adventOfCode/runtime/challenge"
"github.com/codemicro/adventOfCode/runtime/runners"
"os"
"path/filepath"
"strings"
)
@ -30,6 +32,51 @@ func userSelect(question string, choices []string) (int, error) {
return -1, nil
}
func selectYear(dir string) (string, error) {
var opts []string
entries, err := os.ReadDir(dir)
if err != nil {
return "", err
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
opts = append(opts, entry.Name())
}
if len(opts) == 0 {
return "", errors.New("no years to use")
}
if args.Year != "" {
for _, x := range opts {
if x == args.Year {
fmt.Printf("Selecting year %s\n", args.Year)
return filepath.Join(dir, x), nil
}
}
fmt.Printf("Could not locate year %s\n", args.Year)
}
var selectedYearIndex int
if x := len(opts); x == 1 {
selectedYearIndex = 0
fmt.Printf("Automatically selecting year %s\n", opts[selectedYearIndex])
} else {
selectedYearIndex, err = userSelect("Which year do you want to use?", opts)
if err != nil {
return "", err
}
}
return filepath.Join(dir, opts[selectedYearIndex]), nil
}
func selectChallenge(dir string) (*challenge.Challenge, error) {
challenges, err := challenge.ListingFromDir(dir)