github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/pin_resource.go (about)

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"runtime"
     7  
     8  	"github.com/pf-qiu/concourse/v6/atc"
     9  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
    10  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
    11  	"github.com/pf-qiu/concourse/v6/fly/rc"
    12  	"github.com/jessevdk/go-flags"
    13  )
    14  
    15  type PinResourceCommand struct {
    16  	Resource flaghelpers.ResourceFlag `short:"r" long:"resource" required:"true" value-name:"PIPELINE/RESOURCE" description:"Name of the resource"`
    17  	Version  *atc.Version             `short:"v" long:"version" description:"Version of the resource to pin. The given key value pair(s) has to be an exact match but not all fields are needed. In the case of multiple resource versions matched, it will pin the latest one."`
    18  	Comment  string                   `short:"c" long:"comment" description:"Message to be saved to the pinned resource. Resource has to be pinned otherwise --version should be specified to pin the resource first."`
    19  }
    20  
    21  func (command *PinResourceCommand) Execute([]string) error {
    22  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	err = target.Validate()
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	team := target.Team()
    33  
    34  	pipelineRef := command.Resource.PipelineRef
    35  
    36  	if command.Version == nil && command.Comment == "" {
    37  		shortDelim := "-"
    38  		longDelim := "--"
    39  		if runtime.GOOS == "windows" {
    40  			shortDelim = "/"
    41  			longDelim = "/"
    42  		}
    43  		return &flags.Error{
    44  			Type: flags.ErrRequired,
    45  			Message: fmt.Sprintf(
    46  				"the required flag `%sv, %sversion' was not specified",
    47  				shortDelim,
    48  				longDelim,
    49  			),
    50  		}
    51  	}
    52  
    53  	if command.Version != nil {
    54  		latestResourceVersion, err := GetLatestResourceVersion(team, command.Resource, *command.Version)
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  		pinned, err := team.PinResourceVersion(pipelineRef, command.Resource.ResourceName, latestResourceVersion.ID)
    60  
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		if pinned {
    66  			versionBytes, err := json.Marshal(latestResourceVersion.Version)
    67  			if err != nil {
    68  				return err
    69  			}
    70  
    71  			fmt.Printf("pinned '%s/%s' with version %s\n", pipelineRef.String(), command.Resource.ResourceName, string(versionBytes))
    72  		} else {
    73  			displayhelpers.Failf("could not pin '%s/%s', make sure the resource exists\n", pipelineRef.String(), command.Resource.ResourceName)
    74  		}
    75  	}
    76  
    77  	if command.Comment != "" {
    78  		saved, err := team.SetPinComment(pipelineRef, command.Resource.ResourceName, command.Comment)
    79  
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		if saved {
    85  			fmt.Printf("pin comment '%s' is saved\n", command.Comment)
    86  		} else {
    87  			displayhelpers.Failf("could not save comment, make sure '%s/%s' is pinned\n", pipelineRef.String(), command.Resource.ResourceName)
    88  		}
    89  	}
    90  
    91  	return nil
    92  }