github.com/spinnaker/spin@v1.30.0/cmd/pipeline/execution/get.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  	"fmt"
    19  	"net/http"
    20  
    21  	"github.com/antihax/optional"
    22  	"github.com/spf13/cobra"
    23  
    24  	gate "github.com/spinnaker/spin/gateapi"
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  var (
    29  	getExecutionShort = "Get the specified execution"
    30  	getExecutionLong  = "Get the execution with the provided id "
    31  )
    32  
    33  type getOptions struct {
    34  	*executionOptions
    35  }
    36  
    37  func NewGetCmd(executionOptions *executionOptions) *cobra.Command {
    38  	options := &getOptions{
    39  		executionOptions: executionOptions,
    40  	}
    41  	cmd := &cobra.Command{
    42  		Use:   "get",
    43  		Short: getExecutionShort,
    44  		Long:  getExecutionLong,
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return getExecution(cmd, options, args)
    47  		},
    48  	}
    49  	return cmd
    50  }
    51  
    52  func getExecution(cmd *cobra.Command, options *getOptions, args []string) error {
    53  	id, err := util.ReadArgsOrStdin(args)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	query := &gate.ExecutionsControllerApiGetLatestExecutionsByConfigIdsUsingGETOpts{
    59  		ExecutionIds: optional.NewString(id), // Status filtering is ignored when executionId is supplied
    60  		Limit:        optional.NewInt32(1),
    61  	}
    62  
    63  	successPayload, resp, err := options.GateClient.ExecutionsControllerApi.GetLatestExecutionsByConfigIdsUsingGET(
    64  		options.GateClient.Context, query)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	if resp.StatusCode != http.StatusOK {
    70  		return fmt.Errorf("Encountered an error getting execution %s, status code: %d\n",
    71  			id,
    72  			resp.StatusCode)
    73  	}
    74  
    75  	options.Ui.JsonOutput(successPayload)
    76  	return nil
    77  }