-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolve_challenge.go
77 lines (67 loc) · 1.95 KB
/
solve_challenge.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
76
77
package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"time"
"github.com/samber/lo"
"github.com/spf13/cobra"
"go.uber.org/dig"
)
type runSolveChallengeCommandParams struct {
dig.In `ignore-unexported:"true"`
RootLogger *slog.Logger
Challenges challengesService
// client params
challengeToSolve string
complexity int
// Expected in a form host:port
output io.Writer
}
func runSolveChallengeCommand(
ctx context.Context,
params runSolveChallengeCommandParams,
) error {
startedAt := time.Now()
solution, err := params.Challenges.SolveChallenge(
ctx,
params.complexity,
params.challengeToSolve,
)
if err != nil {
return fmt.Errorf("failed to solve challenge: %w", err)
}
solutionDuration := time.Since(startedAt)
fmt.Fprintln(params.output, "Challenge solve result")
fmt.Fprint(params.output, "Complexity: ")
fmt.Fprintln(params.output, params.complexity)
fmt.Fprint(params.output, "Solution: ")
fmt.Fprintln(params.output, solution)
fmt.Fprint(params.output, "Duration: ")
fmt.Fprintln(params.output, solutionDuration)
return nil
}
func newSolveChallengeCmd(container *dig.Container) *cobra.Command {
cmd := &cobra.Command{
Use: "solve-challenge",
Short: "Command to test solving challenges logic",
}
challenge := "any text can be here"
complexity := 1
silent := false
cmd.Flags().StringVar(&challenge, "challenge", challenge, "Challenge to solve")
cmd.Flags().IntVarP(&complexity, "complexity", "c", complexity, "Complexity of the solution (e.g leading hash zeros)")
cmd.Flags().BoolVar(&silent, "silent", silent, "Do not produce any output. Just solve")
cmd.RunE = func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
return container.Invoke(func(params runSolveChallengeCommandParams) error {
params.challengeToSolve = challenge
params.complexity = complexity
params.output = lo.If(silent, io.Discard).Else(os.Stdout)
return runSolveChallengeCommand(ctx, params)
})
}
return cmd
}