-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
75 lines (64 loc) · 1.63 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"flag"
"io"
"log"
"os"
"github.com/Merovius/AdventOfCode/internal/aoc"
"github.com/google/renameio/v2"
"github.com/google/subcommands"
)
type inputCmd struct {
day int
outName string
}
func (*inputCmd) Name() string {
return "input"
}
func (*inputCmd) Synopsis() string {
return "Fetch the puzzle input"
}
func (*inputCmd) Usage() string {
return "Fetch the puzzle input for a given day. Defaults to the most recent day."
}
func (cmd *inputCmd) SetFlags(fs *flag.FlagSet) {
fs.IntVar(&cmd.day, "day", 0, "day to fetch")
fs.StringVar(&cmd.outName, "out", "-", "file to write the output to")
}
func (cmd *inputCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
log.SetFlags(0)
if f.NArg() > 0 {
log.Println("usage: input [-day <day>] [-out <outname>]")
return subcommands.ExitUsageError
}
var w io.WriteCloser
if cmd.outName == "-" {
w = os.Stdout
} else {
pf, err := renameio.NewPendingFile(cmd.outName)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
defer pf.Cleanup()
w = (*pendingFile)(pf)
}
_, client := args[0].(*config), args[1].(*aoc.Client)
if err := client.Input(ctx, cmd.day, w); err != nil {
log.Println(err)
return subcommands.ExitFailure
}
if err := w.Close(); err != nil {
log.Println(err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
type pendingFile renameio.PendingFile
func (f *pendingFile) Write(p []byte) (int, error) {
return (*renameio.PendingFile)(f).Write(p)
}
func (f *pendingFile) Close() error {
return (*renameio.PendingFile)(f).CloseAtomicallyReplace()
}