Skip to content

Commit

Permalink
fix bug if msg finish with space
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Ouvrard committed Aug 10, 2018
1 parent d1b4538 commit 2700ec5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
75 changes: 37 additions & 38 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ func HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {

if matched, _ := regexp.MatchString(KubeWord, post.Message); matched {
words := strings.Fields(post.Message)
cmd := CheckBeforeExec(words, post.Message)
message := strings.TrimSpace(post.Message)
cmd := CheckBeforeExec(words, message)
if len(cmd) > 0 && cmd != "command forbidden" {
fmt.Printf("responding to -> %s", post.Message)

cmdOut := ExecKubectl(cmd)
if cmdOut != "" && len(cmdOut) > 0 {
SendMsgToDebuggingChannel(cmdOut, post.Id)
Expand Down Expand Up @@ -223,55 +225,18 @@ func HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
//SendMsgToDebuggingChannel("I did not understand you!", post.Id)
}

// PrintError print the connexions error
func PrintError(err *model.AppError) {
println("\tError Details:")
println("\t\t" + err.Message)
println("\t\t" + err.Id)
println("\t\t" + err.DetailedError)
}

// SetupGracefulShutdown preapre the graceful shut
func SetupGracefulShutdown(botName string) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
if webSocketClient != nil {
webSocketClient.Close()
}

SendMsgToDebuggingChannel("_"+botName+" has **stopped** running_", "")
os.Exit(0)
}
}()
}

//StringInSlice check if a string is in a []string
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

// CheckBeforeExec - Check stuffs before exec.
func CheckBeforeExec(words []string, lastmsg string) string {
var cmd string
if words[0] == KubeWord && len(words) >= 3 {

confToml := LoadConfig(*configPath)
conf := ParseConfig(confToml)
kubectlAndNs := fmt.Sprintf(conf.kubectlPath + " -n")
cmd = strings.Replace(lastmsg, KubeWord, kubectlAndNs, -1)

// If it contain "all" namespace
if words[1] == "all" {
cmd = cmd + " --all-namespaces"
}

if !StringInSlice(words[2], ValidVerbs) {
fmt.Printf("error -> command unavailable <- %+v \n", lastmsg)
cmd = "command forbidden"
Expand Down Expand Up @@ -300,3 +265,37 @@ func ExecKubectl(cmd string) string {
}
return cl
}

// PrintError print the connexions error
func PrintError(err *model.AppError) {
println("\tError Details:")
println("\t\t" + err.Message)
println("\t\t" + err.Id)
println("\t\t" + err.DetailedError)
}

// SetupGracefulShutdown preapre the graceful shut
func SetupGracefulShutdown(botName string) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
if webSocketClient != nil {
webSocketClient.Close()
}

SendMsgToDebuggingChannel("_"+botName+" has **stopped** running_", "")
os.Exit(0)
}
}()
}

//StringInSlice check if a string is in a []string
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
10 changes: 3 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
// KubeWord - The Word that trigger the bot.
KubeWord = "!k"
// Version is the app version
Version = "0.1.0"
Version = "0.1.1"
)

var (
Expand All @@ -31,12 +31,9 @@ func main() {
client = model.NewAPIv4Client(url)
// Lets test to see if the mattermost server is up and running
MakeSureServerIsRunning()
// lets attempt to login to the Mattermost server as the bot user
// This will set the token required for all future calls
// You can get this token with client.AuthToken
// lets attempt to login to the Mattermost server as the bot user. This will set the token required for all future calls
LoginAsTheBotUser(conf.userLogin, conf.userPassword)
// If the bot user doesn't have the correct information lets update his profile
//UpdateTheBotUserIfNeeded()
// If the bot user doesn't have the correct information lets update his profile UpdateTheBotUserIfNeeded()
// Lets find our bot team
FindBotTeam(conf.teamName)
// Lets create a bot channel for logging debug messages into
Expand All @@ -58,6 +55,5 @@ func main() {
}
}
}()
// You can block forever with
select {}
}

0 comments on commit 2700ec5

Please sign in to comment.