1
1
package main
2
2
3
3
import (
4
- "bytes"
5
4
"context"
6
5
"errors"
7
6
"fmt"
@@ -15,8 +14,8 @@ import (
15
14
log "github.com/obalunenko/logger"
16
15
"github.com/urfave/cli/v2"
17
16
17
+ "github.com/obalunenko/advent-of-code/internal/command"
18
18
"github.com/obalunenko/advent-of-code/internal/puzzles"
19
- "github.com/obalunenko/advent-of-code/internal/puzzles/input"
20
19
)
21
20
22
21
func onExit (_ context.Context ) cli.AfterFunc {
@@ -69,7 +68,8 @@ func notFound(ctx context.Context) cli.CommandNotFoundFunc {
69
68
}
70
69
func menu (ctx context.Context ) cli.ActionFunc {
71
70
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 ))
73
73
74
74
years := puzzles .GetYears ()
75
75
@@ -166,7 +166,7 @@ func handlePuzzleChoices(ctx context.Context, year string, opt promptui.Select)
166
166
167
167
stopSpinner := setSpinner ()
168
168
169
- res , err := run (ctx , year , choice )
169
+ res , err := command . Run (ctx , year , choice )
170
170
if err != nil {
171
171
log .WithError (ctx , err ).Error ("Puzzle run failed" )
172
172
@@ -209,51 +209,20 @@ func optionsFromCli(c *cli.Context) []puzzles.RunOption {
209
209
return options
210
210
}
211
211
212
- type optsCtxKey struct {}
212
+ func sessionFromCli (c * cli.Context ) string {
213
+ var sess string
213
214
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
217
218
}
218
219
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
254
223
}
255
224
256
- return res , nil
225
+ return ""
257
226
}
258
227
259
228
// setSpinner runs the displaying of spinner to handle long time operations. Returns stop func.
0 commit comments