github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/command.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  	"strings"
    22  
    23  	"github.com/oam-dev/kubevela/references/cli/top/model"
    24  )
    25  
    26  // Command is a kind of abstract of user UI operation
    27  type Command struct {
    28  	app *App
    29  }
    30  
    31  // NewCommand return app's command instance
    32  func NewCommand(app *App) *Command {
    33  	command := &Command{
    34  		app: app,
    35  	}
    36  	return command
    37  }
    38  
    39  // Init command instance
    40  func (c *Command) Init() {}
    41  
    42  func (c *Command) exec(_ string, component model.View) {
    43  	c.app.inject(component)
    44  }
    45  
    46  func (c *Command) run(ctx context.Context, cmd string) {
    47  	cmd = strings.ToLower(cmd)
    48  	var component model.View
    49  	switch {
    50  	case cmd == "?" || cmd == "h" || cmd == "help":
    51  		component = NewHelpView(c.app)
    52  	case cmd == "yaml":
    53  		component = NewYamlView(ctx, c.app)
    54  	case cmd == "topology":
    55  		component = NewTopologyView(ctx, c.app)
    56  	case cmd == "log":
    57  		component = NewLogView(ctx, c.app)
    58  	default:
    59  		if resourceView, ok := ResourceViewMap[cmd]; ok {
    60  			resourceView.InitView(ctx, c.app)
    61  			component = resourceView
    62  		} else {
    63  			return
    64  		}
    65  	}
    66  	c.exec(cmd, component)
    67  }