github.com/spinnaker/spin@v1.30.0/cmd/application/list.go (about)

     1  // Copyright (c) 2018, Google, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  
    15  package application
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  
    21  	gate "github.com/spinnaker/spin/gateapi"
    22  
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  type listOptions struct {
    27  	*applicationOptions
    28  }
    29  
    30  var (
    31  	listApplicationShort   = "List the all applications"
    32  	listApplicationLong    = "List the all applications"
    33  	listApplicationExample = "usage: spin application list [options]"
    34  )
    35  
    36  func NewListCmd(appOptions *applicationOptions) *cobra.Command {
    37  	options := &listOptions{
    38  		applicationOptions: appOptions,
    39  	}
    40  	cmd := &cobra.Command{
    41  		Use:     "list",
    42  		Aliases: []string{"ls"},
    43  		Short:   listApplicationShort,
    44  		Long:    listApplicationLong,
    45  		Example: listApplicationExample,
    46  		RunE: func(cmd *cobra.Command, args []string) error {
    47  			return listApplication(cmd, options, args)
    48  		},
    49  	}
    50  	return cmd
    51  }
    52  
    53  func listApplication(cmd *cobra.Command, options *listOptions, args []string) error {
    54  	appList, resp, err := options.GateClient.ApplicationControllerApi.GetAllApplicationsUsingGET(options.GateClient.Context, &gate.ApplicationControllerApiGetAllApplicationsUsingGETOpts{})
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	if resp.StatusCode != http.StatusOK {
    60  		return fmt.Errorf("Encountered an error saving application, status code: %d\n", resp.StatusCode)
    61  	}
    62  
    63  	options.Ui.JsonOutput(appList)
    64  	return nil
    65  }