-
Notifications
You must be signed in to change notification settings - Fork 48
/
attach.go
86 lines (72 loc) · 2.41 KB
/
attach.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
85
86
package queries
import (
"context"
"fmt"
"github.com/kubemq-io/kubemq-community/config"
"github.com/kubemq-io/kubemq-community/pkg/utils"
"github.com/spf13/cobra"
)
type AttachOptions struct {
cfg *config.Config
include []string
exclude []string
resources []string
}
var queriesAttachExamples = `
# attach to all 'queries' channels and output running messages
kubemq queries attach *
# attach to some-query 'queries' channel and output running messages
kubemq queries attach some-query
# attach to some-queries1 and some-queries2 'queries' channels and output running messages
kubemq queries attach some-queries1 some-queries2
# attach to some-queries 'queries' channel and output running messages filter by include regex (some*)
kubemq queries attach some-queries -i some*
# attach to some-queries 'queries' channel and output running messages filter by exclude regex (not-some*)
kubemq queries attach some-queries -e not-some*
`
var queriesAttachLong = `Queries attach command allows to display 'queries' channel content for debugging proposes`
var queriesAttachShort = `Queries attach to 'queries' channels command`
func NewCmdQueriesAttach(ctx context.Context, cfg *config.Config) *cobra.Command {
o := &AttachOptions{
cfg: cfg,
}
cmd := &cobra.Command{
Use: "attach",
Aliases: []string{"a", "att", "at"},
Short: queriesAttachShort,
Long: queriesAttachLong,
Example: queriesAttachExamples,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
utils.CheckErr(o.Complete(args), cmd)
utils.CheckErr(o.Validate())
utils.CheckErr(o.Run(ctx))
},
}
cmd.PersistentFlags().StringArrayVarP(&o.include, "include", "i", []string{}, "set (regex) strings to include")
cmd.PersistentFlags().StringArrayVarP(&o.exclude, "exclude", "e", []string{}, "set (regex) strings to exclude")
return cmd
}
func (o *AttachOptions) Complete(args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing channel argument")
}
for _, a := range args {
rsc := fmt.Sprintf("commands/%s", a)
o.resources = append(o.resources, rsc)
utils.Printlnf("adding '%s' to attach list", a)
}
return nil
}
func (o *AttachOptions) Validate() error {
return nil
}
func (o *AttachOptions) Run(ctx context.Context) error {
err := utils.Run(ctx, o.cfg, o.resources, o.include, o.exclude)
if err != nil {
return err
}
<-ctx.Done()
return nil
}