Skip to content

Commit

Permalink
Merge pull request #52 from infracloudio/infracloudio/update-clustern…
Browse files Browse the repository at this point in the history
…ame-flag

Modified cluster flag to support quotes
  • Loading branch information
PrasadG193 authored Mar 15, 2019
2 parents 8c95cfe + 9679763 commit 81553b8
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions pkg/execute/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"unicode"

"github.com/infracloudio/botkube/pkg/config"
log "github.com/infracloudio/botkube/pkg/logging"
Expand Down Expand Up @@ -123,19 +124,26 @@ func printDefaultMsg() string {
return unsupportedCmdMsg
}

// Trim single and double quotes from ends of string
func trimQuotes(clusterValue string) string {
return strings.TrimFunc(clusterValue, func(r rune) bool {
if r == unicode.SimpleFold('\u0027') || r == unicode.SimpleFold('\u0022') {
return true
}
return false
})
}

func runKubectlCommand(args []string, clusterName string, isAuthChannel bool) string {
// Use 'default' as a default namespace
args = append([]string{"-n", "default"}, args...)

// Remove unnecessary flags
finalArgs := []string{}
checkFlag := false
for _, arg := range args {
if checkFlag {
if arg != clusterName {
return ""
}
checkFlag = false
isClusterNameArg := false
for index, arg := range args {
if isClusterNameArg {
isClusterNameArg = false
continue
}
if arg == AbbrFollowFlag.String() || strings.HasPrefix(arg, FollowFlag.String()) {
Expand All @@ -144,11 +152,18 @@ func runKubectlCommand(args []string, clusterName string, isAuthChannel bool) st
if arg == AbbrWatchFlag.String() || strings.HasPrefix(arg, WatchFlag.String()) {
continue
}
// Check --cluster-name flag
if strings.HasPrefix(arg, ClusterFlag.String()) {
// Check if flag value in current or next argument and compare with config.settings.clustername
if arg == ClusterFlag.String() {
checkFlag = true
} else if strings.SplitAfterN(arg, ClusterFlag.String()+"=", 2)[1] != clusterName {
return ""
if index == len(args)-1 || trimQuotes(args[index+1]) != clusterName {
return ""
}
isClusterNameArg = true
} else {
if trimQuotes(strings.SplitAfterN(arg, ClusterFlag.String()+"=", 2)[1]) != clusterName {
return ""
}
}
isAuthChannel = true
continue
Expand Down

0 comments on commit 81553b8

Please sign in to comment.