github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/list.go (about)

     1  /*
     2   * Copyright 2018 the original author or 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 cmd
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  	"github.com/projectriff/riff-cli/pkg/kubectl"
    24  	"github.com/projectriff/riff-cli/pkg/ioutils"
    25  	"github.com/projectriff/riff-cli/cmd/utils"
    26  )
    27  
    28  type ListOptions struct {
    29  	namespace string
    30  }
    31  
    32  var listOptions ListOptions
    33  
    34  // listCmd represents the list command
    35  var listCmd = &cobra.Command{
    36  	Use:   "list",
    37  	Short: "List function resources",
    38  	Long: `List the currently defined function resources.`,
    39  	Run: func(cmd *cobra.Command, args []string) {
    40  
    41  		// get the viper value from env var, config file or flag option
    42  		listOptions.namespace = utils.GetStringValueWithOverride("namespace", *cmd.Flags())
    43  
    44  		fmt.Printf("Listing function resources in namespace %v\n\n", listOptions.namespace)
    45  
    46  		cmdArgs := []string{"get", "--namespace", listOptions.namespace, "functions"}
    47  
    48  		output, err := kubectl.ExecForString(cmdArgs)
    49  
    50  		if err != nil {
    51  			ioutils.Errorf("Error: %v\n", err)
    52  			return
    53  		}
    54  
    55  		fmt.Printf("%v\n", output)
    56  
    57  	},
    58  }
    59  
    60  func init() {
    61  	rootCmd.AddCommand(listCmd)
    62  
    63  	listCmd.Flags().StringP("namespace", "", "default", "the namespace used for the deployed resources")
    64  }