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

Fix help flag error, remove color codes #1231

Merged
merged 3 commits into from
Sep 7, 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
20 changes: 17 additions & 3 deletions internal/executor/flux/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package flux

import (
"context"
"errors"
"fmt"
"strings"

"github.com/gookit/color"

"github.com/kubeshop/botkube/pkg/formatx"
"github.com/kubeshop/botkube/pkg/pluginx"
)

// deleteConfirmPhase represent a confirmation phase for deletion. Taken from flux: v2.0.1.
const deleteConfirmPhase = "Are you sure you want to delete"

var deleteConfirmErr = errors.New("To delete the resource, please explicitly include the -s or --silent flag in your command")

// escapePositionals add '--' after known keyword. Example:
//
// old: `flux gh pr view 2`
Expand All @@ -35,9 +39,19 @@ func normalize(in string) string {

// ExecuteCommand is a syntax sugar for running CLI commands.
func ExecuteCommand(ctx context.Context, in string, opts ...pluginx.ExecuteCommandMutation) (string, error) {
opts = append(opts, pluginx.ExecuteClearColorCodes())
out, err := pluginx.ExecuteCommand(ctx, in, opts...)
if err != nil {
return "", err
}
return color.ClearCode(out.CombinedOutput()), nil
return out.CombinedOutput(), nil
}

// isDeleteConfirmationErr uses string contains in order to detect if a user was asked to confirm deletion.
// For now, there is no better way as we use terminal output not Go SDK.
func isDeleteConfirmationErr(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), deleteConfirmPhase)
}
4 changes: 4 additions & 0 deletions internal/executor/flux/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (d *Executor) Execute(ctx context.Context, in executor.ExecuteInput) (execu
"KUBECONFIG": kubeConfigPath,
}))
if err != nil {
if isDeleteConfirmationErr(err) {
return "", deleteConfirmErr
}

log.WithError(err).WithField("command", command.ToExecute).Error("failed to run command")
return "", fmt.Errorf("while running command: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion internal/executor/flux/forbidden.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func detectNotSupportedGlobalFlags(normalizedCmd string) error {
issues := multierror.New()

f := pflag.NewFlagSet("detect-not-supported-flags", pflag.ContinueOnError)
f.BoolP("help", "h", false, "to make sure that parsing is ignoring the --help,-h flags")

f.ParseErrorsWhitelist.UnknownFlags = true

for key := range notSupportedGlobalFlags {
Expand All @@ -50,7 +52,7 @@ func detectNotSupportedGlobalFlags(normalizedCmd string) error {

// visit ONLY flags which have been defined by f.String and explicitly set in the command:
f.Visit(func(f *pflag.Flag) {
if f == nil {
if f == nil || f.Name == "help" {
return
}
issues = multierror.Append(issues, fmt.Errorf("The %q flag is not supported by the Botkube flux plugin. Please remove it.", f.Name))
Expand Down
10 changes: 8 additions & 2 deletions pkg/pluginx/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/alexflint/go-arg"
"github.com/gookit/color"
"github.com/mattn/go-shellwords"

"github.com/kubeshop/botkube/internal/plugin"
Expand Down Expand Up @@ -126,11 +127,16 @@ func ExecuteCommand(ctx context.Context, rawCmd string, mutators ...ExecuteComma
Stderr: stderr.String(),
ExitCode: cmd.ProcessState.ExitCode(),
}
if opts.ClearColorCodes {
out.Stdout = color.ClearCode(out.Stdout)
out.Stderr = color.ClearCode(out.Stderr)
}

if err != nil {
return out, runErr(stdout.String(), stderr.String(), err)
return out, runErr(out.Stdout, out.Stderr, err)
}
if out.ExitCode != 0 {
return out, fmt.Errorf("got non-zero exit code, stdout [%q], stderr [%q]", stdout.String(), stderr.String())
return out, fmt.Errorf("got non-zero exit code, stdout [%q], stderr [%q]", out.Stdout, out.Stderr)
}
return out, nil
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/pluginx/command_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import "io"

// ExecuteCommandOptions represents the options for executing a command.
type ExecuteCommandOptions struct {
Envs map[string]string
DependencyDir string
WorkDir string
Stdin io.Reader
Envs map[string]string
DependencyDir string
WorkDir string
Stdin io.Reader
ClearColorCodes bool
}

// ExecuteCommandMutation is a function type that can be used to modify ExecuteCommandOptions.
Expand Down Expand Up @@ -40,3 +41,10 @@ func ExecuteCommandStdin(in io.Reader) ExecuteCommandMutation {
options.Stdin = in
}
}

// ExecuteClearColorCodes is a function that enables removing color codes.
func ExecuteClearColorCodes() ExecuteCommandMutation {
return func(options *ExecuteCommandOptions) {
options.ClearColorCodes = true
}
}
Loading