From 66bdef9e3f043637958301333b86d4a2cdc20ec8 Mon Sep 17 00:00:00 2001 From: derailed Date: Thu, 28 Feb 2019 22:47:45 -0700 Subject: [PATCH] Hoping resolved on cmd lock out issue + bugs --- .goreleaser.yml | 40 +++++++++++------------ .travis.yml | 4 +-- go.mod | 2 +- go.sum | 2 ++ internal/cmd/root.go | 11 ++++++- internal/resource/custom.go | 9 ++++-- internal/resource/pod.go | 7 +++-- internal/views/app.go | 7 +---- internal/views/details.go | 1 - internal/views/exec.go | 2 +- internal/views/info.go | 3 +- internal/views/log.go | 2 ++ internal/views/pod.go | 63 +++++++++++++++++++++++++++++-------- internal/views/utils.go | 3 +- main.go | 1 + 15 files changed, 104 insertions(+), 53 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index c8ff664d..0aacff31 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,44 +1,42 @@ project_name: k9s before: hooks: - # you may remove this if you don't use vgo - go mod download - # you may remove this if you don't need go generate - go generate ./... release: prerelease: true builds: -- env: - - CGO_ENABLED=0 - goos: - - linux - - darwin - - windows - goarch: - - 386 - - amd64 - ldflags: - - -s -w -X github.com/derailed/k9s/internal/cmd.version={{.Version}} -X github.com/derailed/k9s/internal/cmd.commit={{.Commit}} -X github.com/derailed/k9s/internal/cmd.date={{.Date}} + - env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + - windows + goarch: + - 386 + - amd64 + ldflags: + - -s -w -X github.com/derailed/k9s/internal/cmd.version={{.Version}} -X github.com/derailed/k9s/internal/cmd.commit={{.Commit}} -X github.com/derailed/k9s/internal/cmd.date={{.Date}} archive: replacements: - darwin: Darwin - linux: Linux + darwin: Darwin + linux: Linux windows: Windows - 386: i386 - amd64: x86_64 + 386: i386 + amd64: x86_64 checksum: - name_template: 'checksums.txt' + name_template: "checksums.txt" snapshot: name_template: "{{ .Tag }}-next" changelog: sort: asc filters: exclude: - - '^docs:' - - '^test:' + - "^docs:" + - "^test:" # Homebrew brew: @@ -53,4 +51,4 @@ brew: homepage: https://k9ss.io description: Kubernetes CLI To Manage Your Clusters In Style! test: | - system "k9s version" \ No newline at end of file + system "k9s version" diff --git a/.travis.yml b/.travis.yml index c0ffccf9..b01eaca3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go_import_path: github.com/derailed/k9s go: - - 1.11.5 + - "1.12" # - master os: @@ -18,4 +18,4 @@ env: script: - go build - - go test ./... \ No newline at end of file + - go test ./... diff --git a/go.mod b/go.mod index 3920750b..c3478793 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/derailed/k9s require ( contrib.go.opencensus.io/exporter/ocagent v0.4.3 // indirect github.com/Azure/go-autorest v11.4.0+incompatible // indirect - github.com/derailed/tview v0.1.1 + github.com/derailed/tview v0.1.2 github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/evanphx/json-patch v4.1.0+incompatible // indirect github.com/fatih/camelcase v1.0.0 // indirect diff --git a/go.sum b/go.sum index 5219b220..1b49466b 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/derailed/tview v0.1.1 h1:s9rGoufUkZYsIFCfV3M23yHxTKa7Rt6n+QFaOgnirpk= github.com/derailed/tview v0.1.1/go.mod h1:WRYVfgb2PBMLZ/muaSpOc/4H4fYsOPnHOaGnBoJ+hGE= +github.com/derailed/tview v0.1.2 h1:T/XDdjvWrSdhi68bKE0O87O7oHD/JxmfU4IQ13y4fmc= +github.com/derailed/tview v0.1.2/go.mod h1:WRYVfgb2PBMLZ/muaSpOc/4H4fYsOPnHOaGnBoJ+hGE= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/evanphx/json-patch v4.1.0+incompatible h1:K1MDoo4AZ4wU0GIU/fPmtZg7VpzLjCxu+UwBD1FvwOc= diff --git a/internal/cmd/root.go b/internal/cmd/root.go index ab002724..40b0796a 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "runtime/debug" "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/k8s" @@ -126,14 +127,22 @@ func run(cmd *cobra.Command, args []string) { level = log.DebugLevel } log.SetLevel(level) - log.SetFormatter(&log.TextFormatter{FullTimestamp: true, ForceColors: true}) initK9s() + app := views.NewApp() { app.Init(version, refreshRate, k8sFlags) + defer func() { + if err := recover(); err != nil { + app.Stop() + fmt.Println(err) + debug.PrintStack() + } + }() app.Run() } + } func initK8sFlags() { diff --git a/internal/resource/custom.go b/internal/resource/custom.go index 7f5b5126..6c85eee0 100644 --- a/internal/resource/custom.go +++ b/internal/resource/custom.go @@ -68,8 +68,13 @@ func (*Custom) NewInstance(i interface{}) Columnar { log.Error(err) } meta := obj["metadata"].(map[string]interface{}) - cr.path = path.Join(meta["namespace"].(string), meta["name"].(string)) - cr.group, cr.version, cr.name = obj["kind"].(string), obj["apiVersion"].(string), meta["name"].(string) + ns := "" + if n, ok := meta["namespace"]; ok { + ns = n.(string) + } + name := meta["name"].(string) + cr.path = path.Join(ns, name) + cr.group, cr.version, cr.name = obj["kind"].(string), obj["apiVersion"].(string), name return cr } diff --git a/internal/resource/pod.go b/internal/resource/pod.go index 2b2a4538..ac32773c 100644 --- a/internal/resource/pod.go +++ b/internal/resource/pod.go @@ -12,7 +12,10 @@ import ( v1 "k8s.io/api/core/v1" ) -const defaultTimeout = 1 * time.Second +const ( + defaultTimeout = 1 * time.Second + podNameSize = 42 +) type ( // Container represents a resource that encompass multiple containers. @@ -211,7 +214,7 @@ func (r *Pod) Fields(ns string) Row { cr, _, rc, cc := r.statuses() return append(ff, - i.ObjectMeta.Name, + Pad(i.ObjectMeta.Name, podNameSize), strconv.Itoa(cr)+"/"+strconv.Itoa(len(cc)), r.phase(i.Status), strconv.Itoa(rc), diff --git a/internal/views/app.go b/internal/views/app.go index 07725b63..acbd394e 100644 --- a/internal/views/app.go +++ b/internal/views/app.go @@ -214,18 +214,13 @@ func (a *appView) inject(p igniter) { a.cancel() } a.content.RemovePage("main") - a.content.AddPage("main", p, true, true) var ctx context.Context { ctx, a.cancel = context.WithCancel(context.TODO()) p.init(ctx, config.Root.ActiveNamespace()) } - - go func() { - <-time.After(100 * time.Millisecond) - a.Draw() - }() + a.content.AddPage("main", p, true, true) a.focusGroup = append([]tview.Primitive{}, p) a.focusCurrent = 0 diff --git a/internal/views/details.go b/internal/views/details.go index bc9e75f0..2eff8e95 100644 --- a/internal/views/details.go +++ b/internal/views/details.go @@ -77,7 +77,6 @@ func (v *detailsView) keyboard(evt *tcell.EventKey) *tcell.EventKey { log.Debug(">> DetailsView handled ", tcell.KeyNames[key]) return a.action(evt) } - log.Debug("Doh! DetailsView got no registered action for key ", tcell.KeyNames[key]) return evt } diff --git a/internal/views/exec.go b/internal/views/exec.go index e6776c53..5cf519d3 100644 --- a/internal/views/exec.go +++ b/internal/views/exec.go @@ -13,7 +13,7 @@ func run(app *appView, args ...string) bool { log.Error("Command failed:", err, args) app.flash(flashErr, "Doh! command failed", err.Error()) } - log.Debug("Command Done Running..") + log.Debug("Command exec sucessfully!") }) } diff --git a/internal/views/info.go b/internal/views/info.go index 9e78a773..2144820c 100644 --- a/internal/views/info.go +++ b/internal/views/info.go @@ -18,10 +18,9 @@ func newInfoView(app *appView) *infoView { } func (v *infoView) init() { - var row int - cluster := resource.NewCluster() + var row int v.SetCell(row, 0, v.sectionCell("Context")) v.SetCell(row, 1, v.infoCell(cluster.ContextName())) row++ diff --git a/internal/views/log.go b/internal/views/log.go index 1d257602..0035f952 100644 --- a/internal/views/log.go +++ b/internal/views/log.go @@ -16,6 +16,8 @@ func newLogView(title string, parent loggable) *logView { { v.SetBorderPadding(0, 0, 1, 1) v.setCategory("Logs") + v.SetDynamicColors(false) + v.SetWrap(true) v.setTitle(parent.getSelection()) } return &v diff --git a/internal/views/pod.go b/internal/views/pod.go index d6d3a37a..2f7afef1 100644 --- a/internal/views/pod.go +++ b/internal/views/pod.go @@ -28,15 +28,11 @@ func newPodView(t string, app *appView, list resource.List, c colorerFn) resourc picker := newSelectList() { - picker.SetSelectedFunc(func(i int, t, d string, r rune) { - v.sshInto(v.selectedItem, t) - }) picker.setActions(keyActions{ tcell.KeyEscape: {description: "Back", action: v.backCmd}, }) v.AddPage("choose", picker, true, false) } - v.switchPage("po") return &v } @@ -81,6 +77,29 @@ func (v *podView) logsCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } +// func (v *podView) logsCmd(evt *tcell.EventKey) *tcell.EventKey { +// if !v.rowSelected() { +// return evt +// } +// cc, err := fetchContainers(v.list, v.selectedItem, true) +// if err != nil { +// v.app.flash(flashErr, err.Error()) +// log.Error("Error fetching containers", err) +// return evt +// } +// if len(cc) == 1 { +// v.showLogs(v.selectedItem, "") +// } else { +// p := v.GetPrimitive("choose").(*selectList) +// p.populate(cc) +// p.SetSelectedFunc(func(i int, t, d string, r rune) { +// v.showLogs(v.selectedItem, t) +// }) +// v.switchPage("choose") +// } +// return evt +// } + func (v *podView) shellCmd(evt *tcell.EventKey) *tcell.EventKey { if !v.rowSelected() { return evt @@ -92,11 +111,17 @@ func (v *podView) shellCmd(evt *tcell.EventKey) *tcell.EventKey { return evt } if len(cc) == 1 { - v.sshInto(v.selectedItem, "") - return nil + v.shellIn(v.selectedItem, "") + } else { + // v.showPicker(cc) + p := v.GetPrimitive("choose").(*selectList) + p.populate(cc) + p.SetSelectedFunc(func(i int, t, d string, r rune) { + v.shellIn(v.selectedItem, t) + }) + v.switchPage("choose") } - v.showPicker(cc) - return nil + return evt } func (v *podView) showPicker(cc []string) { @@ -105,13 +130,25 @@ func (v *podView) showPicker(cc []string) { v.switchPage("choose") } -func (v *podView) sshInto(path, co string) { +func (v *podView) shellIn(path, co string) { ns, po := namespaced(path) - if len(co) == 0 { - run(v.app, "exec", "-it", "-n", ns, po, "--", "sh") - } else { - run(v.app, "exec", "-it", "-n", ns, po, "-c", co, "--", "sh") + args := []string{"exec", "-it", "-n", ns, po} + if len(co) != 0 { + args = append(args, "-c", co) } + args = append(args, "--", "sh") + log.Debug("Shell args", args) + run(v.app, args...) +} + +func (v *podView) showLogs(path, co string) { + ns, po := namespaced(path) + args := []string{"logs", "-f", "-n", ns, po} + if len(co) != 0 { + args = append(args, "-c", co) + } + log.Debug("Logs Args", args) + run(v.app, args...) } func (v *podView) extraActions(aa keyActions) { diff --git a/internal/views/utils.go b/internal/views/utils.go index 8d3780f8..57f0db06 100644 --- a/internal/views/utils.go +++ b/internal/views/utils.go @@ -65,7 +65,8 @@ func (b *logBuffer) length() int { } func (*logBuffer) decorateLine(l string) string { - return "[" + newLogColor + "::]" + l + "[::]" + return l + // return "[" + newLogColor + "::]" + l + "[::]" } func (b *logBuffer) trimLine(l string) string { diff --git a/main.go b/main.go index e76f8e3d..b75950f6 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ func init() { mod := os.O_CREATE | os.O_APPEND | os.O_WRONLY if file, err := os.OpenFile(config.K9sLogs, mod, config.DefaultFileMod); err == nil { log.SetOutput(file) + log.SetFormatter(&log.TextFormatter{FullTimestamp: true, ForceColors: true}) } else { panic(err) }