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

     1  // Copyright (c) 2019, 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 execution
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  
    23  	"github.com/antihax/optional"
    24  	"github.com/spf13/cobra"
    25  
    26  	gate "github.com/spinnaker/spin/gateapi"
    27  )
    28  
    29  type listOptions struct {
    30  	*executionOptions
    31  	output           string
    32  	pipelineConfigId string
    33  	limit            int32
    34  	running          bool
    35  	succeeded        bool
    36  	failed           bool
    37  	canceled         bool
    38  }
    39  
    40  var (
    41  	listExecutionShort = "List the executions for the provided pipeline id"
    42  	listExecutionLong  = "List the executions for the provided pipeline id"
    43  )
    44  
    45  func NewListCmd(executionOptions *executionOptions) *cobra.Command {
    46  	options := &listOptions{
    47  		executionOptions: executionOptions,
    48  	}
    49  	cmd := &cobra.Command{
    50  		Use:     "list",
    51  		Aliases: []string{"ls"},
    52  		Short:   listExecutionShort,
    53  		Long:    listExecutionLong,
    54  		RunE: func(cmd *cobra.Command, args []string) error {
    55  			return listExecution(cmd, options)
    56  		},
    57  	}
    58  
    59  	cmd.PersistentFlags().StringVarP(&options.pipelineConfigId, "pipeline-id", "i", "", "Spinnaker pipeline id to list executions for")
    60  	cmd.PersistentFlags().Int32VarP(&options.limit, "limit", "l", -1, "number of executions to return")
    61  	cmd.PersistentFlags().BoolVar(&options.running, "running", false, "add filter for running executions")
    62  	cmd.PersistentFlags().BoolVar(&options.succeeded, "succeeded", false, "add filter for succeeded executions")
    63  	cmd.PersistentFlags().BoolVar(&options.failed, "failed", false, "add filter for failed executions")
    64  	cmd.PersistentFlags().BoolVar(&options.canceled, "canceled", false, "add filter for canceled executions")
    65  
    66  	return cmd
    67  }
    68  
    69  func listExecution(cmd *cobra.Command, options *listOptions) error {
    70  	if options.pipelineConfigId == "" {
    71  		return errors.New("required parameter 'pipeline-id' not set")
    72  	}
    73  
    74  	query := &gate.ExecutionsControllerApiGetLatestExecutionsByConfigIdsUsingGETOpts{
    75  		PipelineConfigIds: optional.NewString(options.pipelineConfigId),
    76  	}
    77  
    78  	var statuses []string
    79  	if options.running {
    80  		statuses = append(statuses, "RUNNING")
    81  	}
    82  	if options.succeeded {
    83  		statuses = append(statuses, "SUCCEEDED", "STOPPED", "SKIPPED")
    84  	}
    85  	if options.failed {
    86  		statuses = append(statuses, "TERMINAL", "STOPPED", "FAILED_CONTINUE")
    87  	}
    88  	if options.canceled {
    89  		statuses = append(statuses, "CANCELED")
    90  	}
    91  	if len(statuses) > 0 {
    92  		query.Statuses = optional.NewString(strings.Join(statuses, ","))
    93  	}
    94  
    95  	if options.limit > 0 {
    96  		query.Limit = optional.NewInt32(options.limit)
    97  	}
    98  
    99  	successPayload, resp, err := options.GateClient.ExecutionsControllerApi.GetLatestExecutionsByConfigIdsUsingGET(
   100  		options.GateClient.Context, query)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	if resp.StatusCode != http.StatusOK {
   106  		return fmt.Errorf("Encountered an error listing executions for pipeline id %s, status code: %d\n",
   107  			options.pipelineConfigId,
   108  			resp.StatusCode)
   109  	}
   110  
   111  	options.Ui.JsonOutput(successPayload)
   112  	return nil
   113  }