github.com/oam-dev/kubevela@v1.9.11/references/cli/common.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/pkg/utils/common"
    23  )
    24  
    25  // constants used in `svc` command
    26  const (
    27  	App       = "app"
    28  	Namespace = "namespace"
    29  
    30  	// FlagDescription command flag to specify the description of the definition
    31  	FlagDescription = "desc"
    32  	// FlagAlias command flag to specify the alias of the definition
    33  	FlagAlias = "alias"
    34  	// FlagDryRun command flag to disable actual changes and only display intend changes
    35  	FlagDryRun = "dry-run"
    36  	// FlagTemplateYAML command flag to specify which existing template YAML file to use
    37  	FlagTemplateYAML = "template-yaml"
    38  	// FlagOutput command flag to specify which file to save
    39  	FlagOutput = "output"
    40  	// FlagMessage command flag to specify which file to save
    41  	FlagMessage = "message"
    42  	// FlagType command flag to specify which definition type to use
    43  	FlagType = "type"
    44  	// FlagProvider command flag to specify which provider the cloud resource definition belongs to. Only `alibaba`, `aws`, `azure` are supported.
    45  	FlagProvider = "provider"
    46  	// FlagGit command flag to specify which git repository the configuration(HCL) is stored in
    47  	FlagGit = "git"
    48  	// FlagLocal command flag to specify the local path of Terraform module or resource HCL file
    49  	FlagLocal = "local"
    50  	// FlagPath command flag to specify which path the configuration(HCL) is stored in the Git repository
    51  	FlagPath = "path"
    52  	// FlagNamespace command flag to specify which namespace to use
    53  	FlagNamespace = "namespace"
    54  	// FlagInteractive command flag to specify the use of interactive process
    55  	FlagInteractive = "interactive"
    56  	// CUEExtension with the expected extension for a CUE file.
    57  	CUEExtension = ".cue"
    58  	// YAMLExtension with the expected extension for a YAML file.
    59  	YAMLExtension = ".yaml"
    60  	// YMLExtension with an alternative extension for a YAML file as .yml.
    61  	YMLExtension = ".yml"
    62  )
    63  
    64  func addNamespaceAndEnvArg(cmd *cobra.Command) {
    65  	cmd.Flags().StringP(Namespace, "n", "", "specify the Kubernetes namespace to use")
    66  
    67  	cmd.PersistentFlags().StringP("env", "e", "", "specify environment name for application")
    68  }
    69  
    70  // GetFlagNamespaceOrEnv will get env and namespace flag, namespace flag takes the priority
    71  func GetFlagNamespaceOrEnv(cmd *cobra.Command, args common.Args) (string, error) {
    72  	namespace, err := cmd.Flags().GetString(Namespace)
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  	if namespace != "" {
    77  		return namespace, nil
    78  	}
    79  	velaEnv, err := GetFlagEnvOrCurrent(cmd, args)
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  	return velaEnv.Namespace, nil
    84  
    85  }