Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SourceBindings built-in alias and fix filtering commands #967

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/execute/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func (e *ActionExecutor) ActionsTabularOutput() string {

buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 5, 0, 1, ' ', 0)
fmt.Fprintln(w, "ACTION\tENABLED \tDISPLAY NAME")
fmt.Fprintf(w, "ACTION\tENABLED \tDISPLAY NAME")
for _, name := range keys {
fmt.Fprintf(w, "%s\t%v \t%s\n", name, e.actions[name].Enabled, e.actions[name].DisplayName)
fmt.Fprintf(w, "\n%s\t%v \t%s", name, e.actions[name].Enabled, e.actions[name].DisplayName)
}
w.Flush()
return buf.String()
Expand Down
4 changes: 2 additions & 2 deletions pkg/execute/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ func (e *AliasExecutor) getTabularOutput(bindings []string) string {

buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 5, 0, 1, ' ', 0)
fmt.Fprintln(w, "ALIAS\tCOMMAND\tDISPLAY NAME")
fmt.Fprintf(w, "ALIAS\tCOMMAND\tDISPLAY NAME")
for _, aliasName := range maputil.SortKeys(aliasesToDisplay) {
aliasCfg := aliasesCfg[aliasName]
fmt.Fprintf(w, "%s\t%s\t%s\n", aliasName, aliasCfg.Command, aliasCfg.DisplayName)
fmt.Fprintf(w, "\n%s\t%s\t%s", aliasName, aliasCfg.Command, aliasCfg.DisplayName)
}

w.Flush()
Expand Down
2 changes: 1 addition & 1 deletion pkg/execute/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (e *ConfigExecutor) Commands() map[command.Verb]CommandFn {
}

// Show returns Config in yaml format
func (e *ConfigExecutor) Show(ctx context.Context, cmdCtx CommandContext) (interactive.Message, error) {
func (e *ConfigExecutor) Show(_ context.Context, cmdCtx CommandContext) (interactive.Message, error) {
cmdVerb, cmdRes := parseCmdVerb(cmdCtx.Args)
defer e.reportCommand(cmdVerb, cmdRes, cmdCtx.Conversation.CommandOrigin, cmdCtx.Platform)

Expand Down
5 changes: 3 additions & 2 deletions pkg/execute/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ func TestConfigExecutorShowConfig(t *testing.T) {
tmpDir: ""
plugins:
cacheDir: ""
repositories: {}`),
repositories: {}
`),
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
e := NewConfigExecutor(loggerx.NewNoop(), &fakeAnalyticsReporter{}, tc.Cfg)
msg, err := e.Show(context.Background(), tc.CmdCtx)
require.NoError(t, err)
assert.Equal(t, msg.Body.CodeBlock, tc.ExpectedResult)
assert.Equal(t, tc.ExpectedResult, msg.Body.CodeBlock)
})
}
}
4 changes: 2 additions & 2 deletions pkg/execute/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (e *ExecExecutor) TabularOutput(bindings []string) string {

buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 5, 0, 1, ' ', 0)
fmt.Fprintln(w, "EXECUTOR\tENABLED\tALIASES")
fmt.Fprintf(w, "EXECUTOR\tENABLED\tALIASES")
for _, name := range maputil.SortKeys(executors) {
enabled := executors[name]
aliases := alias.ListExactForExecutor(name, e.cfg.Aliases)
fmt.Fprintf(w, "%s\t%t\t%s\n", name, enabled, strings.Join(aliases, ", "))
fmt.Fprintf(w, "\n%s\t%t\t%s", name, enabled, strings.Join(aliases, ", "))
}

w.Flush()
Expand Down
2 changes: 1 addition & 1 deletion pkg/execute/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestExecutorBindingsExecutor(t *testing.T) {
EXECUTOR ENABLED ALIASES
botkube/[email protected] true e
botkube/helm true h
kubectl true `),
kubectl true`),
},
}
for _, tc := range testCases {
Expand Down
8 changes: 6 additions & 2 deletions pkg/execute/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,12 @@ func (e *DefaultExecutor) Execute(ctx context.Context) interactive.Message {
}

if !foundFn {
e.reportCommand(fmt.Sprintf("%s {invalid feature}", cmdVerb), false)
e.log.Infof("received unsupported resource: %q", cmdCtx.CleanCmd)
reportedCmd := string(cmdVerb)
if cmdRes != "" {
e.log.Infof("received unsupported resource: %q", cmdCtx.CleanCmd)
reportedCmd = fmt.Sprintf("%s {invalid feature}", reportedCmd)
}
e.reportCommand(reportedCmd, false)
msg := e.cmdsMapping.HelpMessageForVerb(cmdVerb, cmdCtx.BotName)
return respond(msg, cmdCtx)
}
Expand Down
32 changes: 6 additions & 26 deletions pkg/execute/executor_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,7 @@ import (
"strings"
)

// executorFilter interface to implement to filter executor text based results
type executorFilter interface {
Apply(string) string
IsActive() bool
}

// executorEchoFilter echos given text when asked to filter executor text results.
// Mainly used when executor commands are missing a "--filter=xxx" flag.
type executorEchoFilter struct {
}

// IsActive whether this filter will actually mutate the output or not.
func (f *executorEchoFilter) IsActive() bool {
return false
}

// Apply implements executorFilter to apply filtering.
func (f *executorEchoFilter) Apply(text string) string {
return text
}

// newExecutorEchoFilter creates a new executorEchoFilter.
func newExecutorEchoFilter(command string) *executorEchoFilter {
return &executorEchoFilter{}
}
var _ executorFilter = &executorTextFilter{}

// executorTextFilter filters executor text results by a given text value.
type executorTextFilter struct {
Expand All @@ -39,7 +15,7 @@ type executorTextFilter struct {

// IsActive whether this filter will actually mutate the output or not.
func (f *executorTextFilter) IsActive() bool {
return true
return len(f.value) > 0
}

// newExecutorTextFilter creates a new executorTextFilter.
Expand All @@ -51,6 +27,10 @@ func newExecutorTextFilter(val string) *executorTextFilter {

// Apply implements executorFilter to apply filtering.
func (f *executorTextFilter) Apply(text string) string {
if !f.IsActive() {
return text
}

var out strings.Builder

scanner := bufio.NewScanner(strings.NewReader(text))
Expand Down
5 changes: 3 additions & 2 deletions pkg/execute/executor_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/stretchr/testify/assert"
)

func TestExecutorEchoFilter_Apply(t *testing.T) {
var filter executorFilter = newExecutorEchoFilter("")
func TestExecutorTextFilter_Empty(t *testing.T) {
var filter executorFilter = newExecutorTextFilter("")

text := "Please return this same text."
assert.Equal(t, text, filter.Apply(text))
assert.False(t, filter.IsActive())
}

func TestExecutorTextFilter_Apply(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/execute/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (e *FilterExecutor) TabularOutput() string {
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 5, 0, 1, ' ', 0)

fmt.Fprintln(w, "FILTER\tENABLED\tDESCRIPTION")
fmt.Fprintf(w, "FILTER\tENABLED\tDESCRIPTION")
for _, filter := range e.filterEngine.RegisteredFilters() {
fmt.Fprintf(w, "%s\t%v\t%s\n", filter.Name(), filter.Enabled, filter.Describe())
fmt.Fprintf(w, "\n%s\t%v\t%s", filter.Name(), filter.Enabled, filter.Describe())
}

w.Flush()
Expand Down
6 changes: 6 additions & 0 deletions pkg/execute/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type CommandExecutor interface {
FeatureName() FeatureName
}

// executorFilter interface to implement to filter executor text based results
type executorFilter interface {
Apply(string) string
IsActive() bool
}

// CommandFn is a single command (eg. List())
type CommandFn func(ctx context.Context, cmdCtx CommandContext) (interactive.Message, error)

Expand Down
4 changes: 2 additions & 2 deletions pkg/execute/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func (e *SourceExecutor) TabularOutput(bindings []string) string {

buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 5, 0, 1, ' ', 0)
fmt.Fprintln(w, "SOURCE\tENABLED")
fmt.Fprintf(w, "SOURCE\tENABLED")
for _, key := range maputil.SortKeys(sources) {
enabled := sources[key]
fmt.Fprintf(w, "%s\t%t\n", key, enabled)
fmt.Fprintf(w, "\n%s\t%t", key, enabled)
}
w.Flush()
return buf.String()
Expand Down
2 changes: 1 addition & 1 deletion pkg/execute/sourcebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
var (
sourceBindingFeatureName = FeatureName{
Name: "sourcebinding",
Aliases: []string{"sourcebindings"},
Aliases: []string{"sourcebindings", "sb"},
}
)

Expand Down