github.com/oam-dev/kubevela@v1.9.11/references/cli/workloads.go (about)

     1  /*
     2   Copyright 2021. 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 cli
    18  
    19  import (
    20  	"github.com/spf13/cobra"
    21  
    22  	"github.com/oam-dev/kubevela/apis/types"
    23  	common2 "github.com/oam-dev/kubevela/pkg/utils/common"
    24  	cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
    25  	"github.com/oam-dev/kubevela/references/common"
    26  	"github.com/oam-dev/kubevela/references/docgen"
    27  )
    28  
    29  // NewWorkloadsCommand creates `workloads` command
    30  func NewWorkloadsCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
    31  	cmd := &cobra.Command{
    32  		Use:                   "workloads",
    33  		DisableFlagsInUseLine: true,
    34  		Short:                 "List workloads",
    35  		Long:                  "List workloads",
    36  		Example:               `vela workloads`,
    37  		Hidden:                true,
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			namespace, err := GetFlagNamespaceOrEnv(cmd, c)
    40  			if err != nil {
    41  				return err
    42  			}
    43  			return printWorkloadList(namespace, c, ioStreams)
    44  		},
    45  		Annotations: map[string]string{
    46  			types.TagCommandType: types.TypeLegacy,
    47  		},
    48  	}
    49  	cmd.SetOut(ioStreams.Out)
    50  	addNamespaceAndEnvArg(cmd)
    51  	return cmd
    52  }
    53  
    54  func printWorkloadList(userNamespace string, c common2.Args, ioStreams cmdutil.IOStreams) error {
    55  	def, err := common.ListRawWorkloadDefinitions(userNamespace, c)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	table := newUITable()
    60  	table.AddRow("NAME", "NAMESPACE", "WORKLOAD", "DESCRIPTION")
    61  	for _, r := range def {
    62  		table.AddRow(r.Name, r.Namespace, r.Spec.Reference.Name, docgen.GetDescription(r.Annotations))
    63  	}
    64  	ioStreams.Info(table.String())
    65  	return nil
    66  }