github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/cluster_view.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package view 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/gdamore/tcell/v2" 24 25 "github.com/oam-dev/kubevela/references/cli/top/component" 26 "github.com/oam-dev/kubevela/references/cli/top/model" 27 ) 28 29 // ClusterView is the cluster view, this view display info of cluster where selected application deployed 30 type ClusterView struct { 31 *CommonResourceView 32 ctx context.Context 33 } 34 35 // Init cluster view init 36 func (v *ClusterView) Init() { 37 v.CommonResourceView.Init() 38 v.SetTitle(fmt.Sprintf("[ %s ]", v.Name())).SetTitleColor(v.app.config.Theme.Table.Title.Color()) 39 v.bindKeys() 40 } 41 42 // Name return cluster view name 43 func (v *ClusterView) Name() string { 44 return "Cluster" 45 } 46 47 // Start the cluster view 48 func (v *ClusterView) Start() { 49 v.Clear() 50 v.Update(func() {}) 51 v.CommonResourceView.AutoRefresh(v.Update) 52 } 53 54 // Stop the cluster view 55 func (v *ClusterView) Stop() { 56 v.CommonResourceView.Stop() 57 } 58 59 // Hint return key action menu hints of the cluster view 60 func (v *ClusterView) Hint() []model.MenuHint { 61 return v.Actions().Hint() 62 } 63 64 // InitView init a new cluster view 65 func (v *ClusterView) InitView(ctx context.Context, app *App) { 66 v.ctx = ctx 67 if v.CommonResourceView == nil { 68 v.CommonResourceView = NewCommonView(app) 69 } 70 } 71 72 // Refresh the view content 73 func (v *ClusterView) Refresh(_ *tcell.EventKey) *tcell.EventKey { 74 v.CommonResourceView.Refresh(true, v.Update) 75 return nil 76 } 77 78 // Update refresh the content of body of view 79 func (v *ClusterView) Update(timeoutCancel func()) { 80 v.BuildHeader() 81 v.BuildBody() 82 timeoutCancel() 83 } 84 85 // BuildHeader render the header of table 86 func (v *ClusterView) BuildHeader() { 87 header := []string{"Name", "Alias", "Type", "EndPoint", "Labels"} 88 v.CommonResourceView.BuildHeader(header) 89 } 90 91 // BuildBody render the body of table 92 func (v *ClusterView) BuildBody() { 93 clusterList, err := model.ListClusters(v.ctx, v.app.client) 94 if err != nil { 95 return 96 } 97 clusterInfos := clusterList.ToTableBody() 98 v.CommonResourceView.BuildBody(clusterInfos) 99 } 100 101 func (v *ClusterView) bindKeys() { 102 v.Actions().Delete([]tcell.Key{tcell.KeyEnter}) 103 v.Actions().Add(model.KeyActions{ 104 tcell.KeyEnter: model.KeyAction{Description: "Goto", Action: v.managedResourceView, Visible: true, Shared: true}, 105 component.KeyR: model.KeyAction{Description: "Refresh", Action: v.Refresh, Visible: true, Shared: true}, 106 }) 107 } 108 109 // managedResourceView switch cluster view to managed resource view 110 func (v *ClusterView) managedResourceView(event *tcell.EventKey) *tcell.EventKey { 111 row, _ := v.GetSelection() 112 if row == 0 { 113 return event 114 } 115 clusterName := v.GetCell(row, 0).Text 116 if clusterName == model.AllCluster { 117 clusterName = "" 118 } 119 v.app.content.PopView() 120 v.app.content.PopView() 121 v.ctx = context.WithValue(v.ctx, &model.CtxKeyCluster, clusterName) 122 v.app.command.run(v.ctx, "resource") 123 return event 124 }