Skip to content

Commit

Permalink
Add support show the pipeline and pipelinerun as YAML format (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Sep 30, 2021
1 parent 7791358 commit a883a7b
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion kubectl-plugin/pipeline/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pipeline

import (
"bytes"
"context"
"fmt"
"github.com/Pallinder/go-randomdata"
Expand All @@ -17,7 +18,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -134,7 +137,7 @@ func (o *dashboardOption) createPipelineList() (listView tview.Primitive) {
listView = table
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
o.header.Clear()
o.header.SetText("(r) Run a Pipeline and go to PipelineRun, (R) Run a Pipeline, (v) List the PipelineRuns, (c) Create a Pipeline")
o.header.SetText(`(r) Run a Pipeline and go to PipelineRun, (R) Run a Pipeline, (v) List the PipelineRuns, (c) Create a Pipeline, (y) View the as YAML`)
switch key := event.Rune(); key {
case 'v':
o.listPipelineRuns(0, o.namespace, "", 0)
Expand All @@ -157,6 +160,15 @@ func (o *dashboardOption) createPipelineList() (listView tview.Primitive) {
_ = run.triggerPipeline(o.namespace, pipeline, nil)
case 'c':
o.pipelineCreationForm()
case 'y':
row, col := table.GetSelection()
cell := table.GetCell(row, col)
pipeline := cell.Text
if strings.HasPrefix(table.GetTitle(), "pipelinerun") {
o.resourceYAMLView(types.GetPipelineRunSchema(), o.namespace, pipeline)
} else if strings.HasPrefix(table.GetTitle(), "pipeline") {
o.resourceYAMLView(types.GetPipelineSchema(), o.namespace, pipeline)
}
}
if event.Key() == tcell.KeyESC {
o.listPipelines(0, o.namespace, "", 0)
Expand Down Expand Up @@ -198,6 +210,30 @@ func (o *dashboardOption) getTable(ns, kind string, table *tview.Table) (err err
return
}

func (o *dashboardOption) resourceYAMLView(groupVer schema.GroupVersionResource, ns, name string) {
textView := tview.NewTextView()
textView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyESC {
o.stack.Pop()
}
return event
})
textView.SetBorder(true)
textView.SetTitle(fmt.Sprintf("%s(%s/%s)", groupVer.Resource, ns, name))
var data *unstructured.Unstructured
var err error
if data, err = o.client.Resource(groupVer).Namespace(ns).Get(context.TODO(), name, metav1.GetOptions{}); err == nil {
data.SetManagedFields(nil)

buffer := bytes.NewBuffer([]byte{})
printer := &printers.YAMLPrinter{}
if err = printer.PrintObj(data.DeepCopyObject(), buffer); err == nil {
textView.SetText(buffer.String())
}
}
o.stack.Push(textView)
}

func (o *dashboardOption) pipelineCreationForm() {
form := tview.NewForm()
form.AddButton("OK", func() {
Expand Down

0 comments on commit a883a7b

Please sign in to comment.