Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
history feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tedteng committed Apr 28, 2021
1 parent f2a963d commit 04c12ea
Show file tree
Hide file tree
Showing 105 changed files with 12,492 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gardener/gardener v1.5.0
github.com/gardener/gardener-extension-provider-openstack v1.3.1-0.20200327120628-280d268ce96f
github.com/gardener/machine-controller-manager v0.27.0
github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9
github.com/golang/mock v1.4.3
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e
github.com/manifoldco/promptui v0.8.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/cloudflare-go v0.11.4/go.mod h1:ZB+hp7VycxPLpp0aiozQQezat46npDXhzHi1DVtRCn4=
Expand Down Expand Up @@ -259,6 +261,8 @@ github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d h1:3PaI8p3seN09Vjb
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9 h1:wqckanyE9qc/XnvnybC6SHOb8Nyd62QXAZOzA8twFig=
github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9/go.mod h1:64ikIrMv84B+raz7akXOqbF7cK3/OQQ/6cClY10oy7A=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func Execute() {
CreateDir(pathSessionID, 0751)
pathTarget = filepath.Join(pathSessionID, "target")
CreateFileIfNotExists(pathTarget, 0644)
pathHistory = filepath.Join(pathSessionID, "history")
CreateFileIfNotExists(pathHistory, 0644)
gardenConfig = os.Getenv("GARDENCONFIG")
if gardenConfig != "" {
if _, err := os.Stat(gardenConfig); err != nil {
Expand Down Expand Up @@ -159,6 +161,7 @@ func init() {
RootCmd.AddCommand(NewInfoCmd(targetReader, ioStreams))
RootCmd.AddCommand(NewVersionCmd(), NewUpdateCheckCmd())
RootCmd.AddCommand(NewDiagCmd(targetReader, ioStreams))
RootCmd.AddCommand(NewHistoryCmd())

RootCmd.SuggestionsMinimumDistance = suggestionsMinimumDistance
RootCmd.BashCompletionFunction = bashCompletionFunc
Expand Down
134 changes: 116 additions & 18 deletions pkg/internal/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package history

import (
"bufio"
Expand All @@ -25,20 +25,21 @@ import (
"strings"
"syscall"

"github.com/goinggo/mapstructure"
"github.com/manifoldco/promptui"
)

//History struct
type History struct {
ConfigPath string
binary string
Binary string
}

//Settings History
func Settings(path string, binary string) *History {
h := &History{
ConfigPath: path, //Config Path
binary: binary, //executable Binary file
Binary: binary, //executable Binary file
}
return h
}
Expand Down Expand Up @@ -100,18 +101,45 @@ func (h *History) Previous() {
if err != nil {
log.Fatalln("Prompt failed: \n", err)
}
h.Write(result)
ExecuteItem(h.binary, result)

Execute(h.Binary, result)
}

//List all HIstory records and execute the select one
func (h *History) List() {
reverse := reverse(h.Load())
load := reverse[:10]
load := reverse(h.Load())

if len(load) > 1000 {
load = load[:1000]
}

items := factoryMyBoxItem(load)
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F4CC {{ .Cmd | cyan }} ",
Inactive: " {{ .Cmd | cyan }}",
Selected: "\U0001F4CC {{ .Cmd | red | cyan }} ",
Details: `
--------- Cluster Info ----------
{{ "Garden:" | faint }} {{ .Garden }}
{{ "Project:" | faint }} {{ .Project }}
{{ "Seed:" | faint }} {{ .Seed }}
{{ "Control Plane:" | faint }} {{ .ControlPlane }}
{{ "Shoot:" | faint }} {{ .Shoot }}`,
}
searcher := func(input string, index int) bool {
myboxitem := items[index]
name := strings.Replace(strings.ToLower(myboxitem.Cmd), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}

prompt := promptui.Select{
Label: "Target hisotry",
Items: load,
Size: 10,
Label: "Target hisotry",
Items: items,
Templates: templates,
Size: 10,
Searcher: searcher,
}

i, _, err := prompt.Run()
Expand All @@ -121,12 +149,26 @@ func (h *History) List() {
}

item := load[i]
h.Write(item)
ExecuteItem(h.binary, item)

Execute(h.Binary, item)
}

//ExecuteItem execute item
func ExecuteItem(binary string, result string) {
func factoryMyBoxItem(load []string) []MyBoxItem {
items := []MyBoxItem{}
box := Mybox{items}
for _, i := range load {
mapInstance := toMap(i)
var myBoxItem MyBoxItem
if err := mapstructure.Decode(mapInstance, &myBoxItem); err != nil {
fmt.Println(err)
}
box.addItem(myBoxItem)
}
return box.Items
}

//Execute binary eg gardenctl subcommand
func Execute(binary string, result string) {
binary, lookErr := exec.LookPath(binary)
if lookErr != nil {
panic(lookErr)
Expand All @@ -149,10 +191,6 @@ func convert(i interface{}) string {
str = string(x[:])
case string:
str = x
/*
add more convert if need
*/

default:
log.Fatalln("convert type unknown")
}
Expand All @@ -174,3 +212,63 @@ func reverse(s []string) []string {
}
return s
}

func toMap(str string) map[string]string {
c := strings.Fields(str)
a := make(map[string]string)
a["Cmd"] = str
for i, k := range c {
k = format(k)
if i%2 == 0 {
a[k] = c[i+1]
}
}
return a
}

func format(str string) string {
for {
if strings.HasPrefix(str, "-") {
str = strings.TrimPrefix(str, "-")
} else {
break
}
}

switch strings.ToUpper(str) {

case "G", "GARDEN", "SERVER":
return "Garden"
case "S", "SEED":
return "Seed"
case "P", "Project":
return "Project"
case "t", "SHOOT":
return "Shoot"
case "n", "NAMESPACE":
return "ControlPlane"
default:
return str
}
}

//MyBoxItem struct
type MyBoxItem struct {
Cmd string
Seed string
Server string
Garden string
Project string
ControlPlane string
Shoot string
}

//Mybox struct
type Mybox struct {
Items []MyBoxItem
}

func (box *Mybox) addItem(item MyBoxItem) []MyBoxItem {
box.Items = append(box.Items, item)
return box.Items
}
1 change: 1 addition & 0 deletions vendor/github.com/chzyer/readline/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/chzyer/readline/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions vendor/github.com/chzyer/readline/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/chzyer/readline/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 04c12ea

Please sign in to comment.