-
Notifications
You must be signed in to change notification settings - Fork 39
/
create.go
61 lines (49 loc) · 1.37 KB
/
create.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"strings"
"github.com/genuinetools/bpfd/api/grpc"
"github.com/genuinetools/bpfd/rules"
"github.com/sirupsen/logrus"
)
const createHelp = `Create one or more rules.`
func (cmd *createCommand) Name() string { return "create" }
func (cmd *createCommand) Args() string { return "[OPTIONS] RULE_FILE [RULE_FILE...]" }
func (cmd *createCommand) ShortHelp() string { return createHelp }
func (cmd *createCommand) LongHelp() string { return createHelp }
func (cmd *createCommand) Hidden() bool { return false }
func (cmd *createCommand) Register(fs *flag.FlagSet) {
}
type createCommand struct {
}
func (cmd *createCommand) Run(ctx context.Context, args []string) error {
if len(args) < 1 {
return errors.New("must pass at least one rule file")
}
// Create the grpc client.
c, err := getClient(ctx, grpcAddress)
if err != nil {
return err
}
prs, names, err := rules.ParseFiles(args...)
if err != nil {
return err
}
logrus.Debugf("Creating rules: %s", strings.Join(names, ", "))
// Create the rules.
for _, rules := range prs {
for _, rule := range rules {
_, err := c.CreateRule(ctx, &grpc.CreateRuleRequest{
Rule: &rule,
})
if err != nil {
return fmt.Errorf("sending CreateRule request for name %s failed: %v", rule.Name, err)
}
fmt.Println(rule.Name)
}
}
return nil
}