-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathcommand.go
84 lines (79 loc) · 1.97 KB
/
command.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
78
79
80
81
82
83
84
package processcmd
import (
"encoding/json"
"github.com/bitmagnet-io/bitmagnet/internal/classifier"
"github.com/bitmagnet-io/bitmagnet/internal/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/processor"
"github.com/bitmagnet-io/bitmagnet/internal/protocol"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
"go.uber.org/zap"
)
type Params struct {
fx.In
Processor lazy.Lazy[processor.Processor]
Logger *zap.SugaredLogger
}
type Result struct {
fx.Out
Command *cli.Command `group:"commands"`
}
func New(p Params) (Result, error) {
return Result{Command: &cli.Command{
Name: "process",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "infoHash",
},
&cli.StringFlag{
Name: "classifierFlags",
Value: "{}",
Usage: "optional JSON-encoded runtime flags to pass to the classifier",
},
&cli.BoolFlag{
Name: "apisDisabled",
Value: false,
Usage: "disable API calls for the classifier workflow",
},
&cli.BoolFlag{
Name: "localSearchDisabled",
Value: false,
Usage: "disable local search queries for the classifier workflow",
},
},
Action: func(ctx *cli.Context) error {
pr, err := p.Processor.Get()
if err != nil {
return err
}
var flags classifier.Flags
strFlags := ctx.String("classifierFlags")
if err := json.Unmarshal([]byte(strFlags), &flags); err != nil {
return cli.Exit("invalid flags", 1)
}
if ctx.Bool("apisDisabled") {
flags["apis_enabled"] = false
}
if ctx.Bool("localSearchDisabled") {
flags["local_search_enabled"] = false
}
var infoHashes []protocol.ID
for _, infoHash := range ctx.StringSlice("infoHash") {
id, err := protocol.ParseID(infoHash)
if err != nil {
return err
}
infoHashes = append(infoHashes, id)
}
if err != nil {
return err
}
return pr.Process(ctx.Context, processor.MessageParams{
ClassifyMode: processor.ClassifyModeRematch,
ClassifierFlags: flags,
InfoHashes: infoHashes,
})
},
},
}, nil
}