Skip to content

Commit

Permalink
Refactor solcates' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Oct 10, 2017
1 parent f28fe08 commit 6f52ee1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
12 changes: 4 additions & 8 deletions kube/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,10 @@ func argumentsCompleter(args []string) []prompt.Suggest {
if len(args) == 2 {
return prompt.FilterHasPrefix(resourceTypes, args[1], true)
}
second := args[1]

// TODO: Add suggestions for Deployments
third := args[2]
if len(args) == 3 {
switch second {
third := args[2]
switch args[1] {
case "componentstatuses", "cs":
return prompt.FilterContains(getComponentStatusCompletions(), third, true)
case "configmaps", "cm":
Expand Down Expand Up @@ -411,14 +409,12 @@ func argumentsCompleter(args []string) []prompt.Suggest {
{Text: "use-context", Description: "Sets the current-context in a kubeconfig file"},
{Text: "view", Description: "Display merged kubeconfig settings or a specified kubeconfig file"},
}
second := args[1]

if len(args) == 2 {
return prompt.FilterHasPrefix(subCommands, args[1], true)
}
third := args[2]
if len(args) == 3 {
switch second {
third := args[2]
switch args[1] {
case "use-context":
return prompt.FilterContains(getContextSuggestions(), third, true)
}
Expand Down
15 changes: 7 additions & 8 deletions kube/executor.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package kube

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"bytes"
)

func Executor(s string) {
Expand All @@ -28,21 +29,19 @@ func Executor(s string) {
return
}

func ExecuteAndReturnList(s string) (list []string) {
func ExecuteAndGetResult(s string) (string, error) {
s = strings.TrimSpace(s)
if s == "" {
return
return "", errors.New("you need to pass the something arguments")
}

out := &bytes.Buffer{}
cmd := exec.Command("/bin/sh", "-c", "kubectl "+s)
cmd.Stdin = os.Stdin
cmd.Stdout = out
if err := cmd.Run(); err != nil {
fmt.Printf("Got error: %s\n", err.Error())
return "", err
}

ss := string(out.Bytes())
return strings.Split(ss, "\n")

r := string(out.Bytes())
return r, nil
}
14 changes: 8 additions & 6 deletions kube/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kube

import (
"fmt"
"log"
"sort"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -139,10 +141,11 @@ func fetchContextList() {
if time.Since(contextLastFetchAt) < thresholdFetchInterval {
return
}
// TODO: Get a list of config get-context
list := ExecuteAndReturnList("config get-contexts --no-headers -o name")

contextList.Store(list)
r, err := ExecuteAndGetResult("config get-contexts --no-headers -o name")
if err != nil {
log.Printf("[WARN] Got Error when fetchContextList: %s", err.Error())
}
contextList.Store(strings.Split(r, "\n"))
}

func getContextSuggestions() []prompt.Suggest {
Expand All @@ -154,8 +157,7 @@ func getContextSuggestions() []prompt.Suggest {
s := make([]prompt.Suggest, len(l))
for i := range l {
s[i] = prompt.Suggest{
Text: l[i],
Description: string(l[i]),
Text: l[i],
}
}
return s
Expand Down

0 comments on commit 6f52ee1

Please sign in to comment.