Skip to content

Commit

Permalink
Added first version of kubernetes chat plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dgil committed Jun 2, 2016
1 parent 47a2b0c commit 944f82d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
38 changes: 38 additions & 0 deletions execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
)

func execute(name string, arg ...string) string {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

cmd := exec.Command(name, arg...)

stdin, err := cmd.StdinPipe()

if err != nil {
fmt.Println(err)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err = cmd.Start(); err != nil {
fmt.Println("An error occured: ", err)
}

stdin.Close()
cmd.Wait()

w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout

return string(out)
}
35 changes: 35 additions & 0 deletions kubebot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"github.com/go-chat-bot/bot"
"regexp"
"strings"
)

const (
pattern = "(?i)\\b(k|kubectl)\\b"
)

var (
re = regexp.MustCompile(pattern)
)

func kubernetes(command *bot.PassiveCmd) (string, error) {
if !re.MatchString(command.Raw) {
return "", nil
}

kubecmd := re.ReplaceAllString(command.Raw, "")
kubecmd = strings.Trim(kubecmd, " ")
params := strings.Split(kubecmd, " ")
output := execute("kubectl", params...)

return fmt.Sprintf("```\n%s\n```", output), nil
}

func init() {
bot.RegisterPassiveCommand(
"kubernetes",
kubernetes)
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/go-chat-bot/bot/slack"
"os"
)

func main() {
slack.Run(os.Getenv("KUBEBOT_SLACK_TOKEN"))
}

0 comments on commit 944f82d

Please sign in to comment.