github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/namespace_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  	v1 "k8s.io/api/core/v1"
    25  
    26  	"github.com/oam-dev/kubevela/references/cli/top/component"
    27  	"github.com/oam-dev/kubevela/references/cli/top/model"
    28  )
    29  
    30  // NamespaceView is namespace view struct
    31  type NamespaceView struct {
    32  	*CommonResourceView
    33  	ctx context.Context
    34  }
    35  
    36  // Init a namespace view
    37  func (v *NamespaceView) Init() {
    38  	v.CommonResourceView.Init()
    39  	v.SetTitle(fmt.Sprintf("[ %s ]", v.Name())).SetTitleColor(v.app.config.Theme.Table.Title.Color())
    40  	v.bindKeys()
    41  }
    42  
    43  // Name return k8s view name
    44  func (v *NamespaceView) Name() string {
    45  	return "Namespace"
    46  }
    47  
    48  // Start the managed namespace view
    49  func (v *NamespaceView) Start() {
    50  	v.Clear()
    51  	v.Update(func() {})
    52  	v.CommonResourceView.AutoRefresh(v.Update)
    53  }
    54  
    55  // Stop the managed namespace view
    56  func (v *NamespaceView) Stop() {
    57  	v.CommonResourceView.Stop()
    58  }
    59  
    60  // Hint return key action menu hints of the k8s view
    61  func (v *NamespaceView) Hint() []model.MenuHint {
    62  	return v.Actions().Hint()
    63  }
    64  
    65  // InitView init a new namespace view
    66  func (v *NamespaceView) InitView(ctx context.Context, app *App) {
    67  	v.ctx = ctx
    68  	if v.CommonResourceView == nil {
    69  		v.CommonResourceView = NewCommonView(app)
    70  	}
    71  }
    72  
    73  // Refresh the view content
    74  func (v *NamespaceView) Refresh(_ *tcell.EventKey) *tcell.EventKey {
    75  	v.CommonResourceView.Refresh(true, v.Update)
    76  	return nil
    77  }
    78  
    79  // Update refresh the content of body of view
    80  func (v *NamespaceView) Update(timeoutCancel func()) {
    81  	v.BuildHeader()
    82  	v.BuildBody()
    83  	timeoutCancel()
    84  }
    85  
    86  // BuildHeader render the header of table
    87  func (v *NamespaceView) BuildHeader() {
    88  	header := []string{"Name", "Status", "Age"}
    89  	v.CommonResourceView.BuildHeader(header)
    90  }
    91  
    92  // BuildBody render the body of table
    93  func (v *NamespaceView) BuildBody() {
    94  	nsList, err := model.ListNamespaces(v.ctx, v.app.client)
    95  	if err != nil {
    96  		return
    97  	}
    98  	nsInfos := nsList.ToTableBody()
    99  	v.CommonResourceView.BuildBody(nsInfos)
   100  	rowNum := len(nsInfos)
   101  	v.ColorizeStatusText(rowNum)
   102  }
   103  
   104  // ColorizeStatusText colorize the status column text
   105  func (v *NamespaceView) ColorizeStatusText(rowNum int) {
   106  	for i := 0; i < rowNum; i++ {
   107  		status := v.Table.GetCell(i+1, 1).Text
   108  		highlightColor := v.app.config.Theme.Table.Body.String()
   109  		switch v1.NamespacePhase(status) {
   110  		case v1.NamespaceActive:
   111  			highlightColor = v.app.config.Theme.Status.Healthy.String()
   112  		case v1.NamespaceTerminating:
   113  			highlightColor = v.app.config.Theme.Status.UnHealthy.String()
   114  		}
   115  		v.Table.GetCell(i+1, 1).SetText(fmt.Sprintf("[%s::]%s", highlightColor, status))
   116  	}
   117  }
   118  
   119  func (v *NamespaceView) bindKeys() {
   120  	v.Actions().Delete([]tcell.Key{tcell.KeyEnter})
   121  	v.Actions().Add(model.KeyActions{
   122  		tcell.KeyEnter: model.KeyAction{Description: "Select", Action: v.applicationView, Visible: true, Shared: true},
   123  		component.KeyR: model.KeyAction{Description: "Refresh", Action: v.Refresh, Visible: true, Shared: true},
   124  	})
   125  }
   126  
   127  func (v *NamespaceView) applicationView(event *tcell.EventKey) *tcell.EventKey {
   128  	row, _ := v.GetSelection()
   129  	if row == 0 {
   130  		return event
   131  	}
   132  	ns := v.Table.GetCell(row, 0).Text
   133  	if ns == model.AllNamespace {
   134  		ns = ""
   135  	}
   136  	v.app.content.PopView()
   137  	ctx := context.WithValue(v.ctx, &model.CtxKeyNamespace, ns)
   138  	v.app.command.run(ctx, "app")
   139  	return event
   140  }