Move command execution logic to seperate package

This commit is contained in:
Prasad Ghangal 2018-12-31 12:27:09 +05:30
parent 0271a60ca0
commit 81aae709c1
2 changed files with 57 additions and 35 deletions

View File

@ -1,4 +1,4 @@
package slack
package execute
import (
"io/ioutil"
@ -20,7 +20,6 @@ var validKubectlCommands = map[string]bool{
"explain": true,
"get": true,
"logs": true,
"set": true,
"top": true,
"version": true,
"auth": true,
@ -32,8 +31,29 @@ var validNotifierCommands = map[string]bool{
"ping": true,
}
func parseAndRunCommand(msg string) string {
args := strings.Split(msg, " ")
//var kubectlBinary = "/usr/local/bin/kubectl"
var kubectlBinary = "/snap/bin/kubectl"
// Executor is an interface for processes to execute commands
type Executor interface {
Execute() string
}
// DefaultExecutor is a default implementations of Executor
type DefaultExecutor struct {
Message string
}
// NewDefaultExecutor returns new Executor object
func NewDefaultExecutor(msg string) Executor {
return &DefaultExecutor{
Message: msg,
}
}
// Execute executes commands and returns output
func (e *DefaultExecutor) Execute() string {
args := strings.Split(e.Message, " ")
if validKubectlCommands[args[0]] {
return runKubectlCommand(args)
}
@ -43,6 +63,35 @@ func parseAndRunCommand(msg string) string {
return "Command not supported. Please run '@kubeops help' to see supported commands"
}
func printHelp() string {
allowedKubectl := ""
for k := range validKubectlCommands {
allowedKubectl = allowedKubectl + k + ", "
}
helpMsg := "kubeops executes kubectl commands on k8s cluster and returns output.\n" +
"Usages:\n" +
" @kubeops <kubectl command without `kubectl` prefix>\n" +
"e.g:\n" +
" @kubeops get pods\n" +
" @kubeops logs podname -n namespace\n" +
"Allowed kubectl commands:\n" +
" " + allowedKubectl + "\n\n" +
"Commands to manage notifier:\n" +
"notifier stop Stop sending k8s event notifications to slack (started by default)\n" +
"notifier start Start sending k8s event notifications to slack\n" +
"notifier status Show running status of event notifier\n" +
"notifier showconfig Show kubeops configuration for event notifier\n\n" +
"Other Commands:\n" +
"help Show help\n" +
"ping Check connection health\n"
return helpMsg
}
func printDefaultMsg() string {
return "Command not supported. Please run '@kubeops help' to see supported commands"
}
func runKubectlCommand(args []string) string {
// Use 'default' as a default namespace
args = append([]string{"-n", "default"}, args...)
@ -59,7 +108,7 @@ func runKubectlCommand(args []string) string {
finalArgs = append(finalArgs, a)
}
cmd := exec.Command("/usr/local/bin/kubectl", finalArgs...)
cmd := exec.Command(kubectlBinary, finalArgs...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Logger.Error("Error in executing kubectl command: ", err)
@ -108,35 +157,6 @@ func runNotifierCommand(args []string) string {
return printDefaultMsg()
}
func printHelp() string {
allowedKubectl := ""
for k := range validKubectlCommands {
allowedKubectl = allowedKubectl + k + ", "
}
helpMsg := "kubeops executes kubectl commands on k8s cluster and returns output.\n" +
"Usages:\n" +
" @kubeops <kubectl command without `kubectl` prefix>\n" +
"e.g:\n" +
" @kubeops get pods\n" +
" @kubeops logs podname -n namespace\n" +
"Allowed kubectl commands:\n" +
" " + allowedKubectl + "\n\n" +
"Commands to manage notifier:\n" +
"notifier stop Stop sending k8s event notifications to slack (started by default)\n" +
"notifier start Start sending k8s event notifications to slack\n" +
"notifier status Show running status of event notifier\n" +
"notifier showconfig Show kubeops configuration for event notifier\n\n" +
"Other Commands:\n" +
"help Show help\n" +
"ping Check connection health\n"
return helpMsg
}
func printDefaultMsg() string {
return "Command not supported. Please run '@kubeops help' to see supported commands"
}
func showControllerConfig() (string, error) {
configPath := os.Getenv("KUBEOPS_CONFIG_PATH")
configFile := filepath.Join(configPath, config.ConfigFileName)

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/infracloudio/kubeops/pkg/config"
"github.com/infracloudio/kubeops/pkg/execute"
"github.com/infracloudio/kubeops/pkg/logging"
"github.com/nlopes/slack"
)
@ -79,7 +80,8 @@ func (s *Bot) Start() {
}
func (sm *slackMessage) HandleMessage() {
sm.OutMessage = parseAndRunCommand(sm.InMessage)
e := execute.NewDefaultExecutor(sm.InMessage)
sm.OutMessage = e.Execute()
sm.OutMsgLength = len(sm.OutMessage)
sm.Send()
}