diff --git a/global.go b/global.go index 016ca15..33bafe4 100644 --- a/global.go +++ b/global.go @@ -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) @@ -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" @@ -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 +} diff --git a/main.go b/main.go index 1e3b493..82fa5c1 100644 --- a/main.go +++ b/main.go @@ -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 ( @@ -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 @@ -58,6 +55,5 @@ func main() { } } }() - // You can block forever with select {} }