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

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
    10  	"github.com/pf-qiu/concourse/v6/fly/rc"
    11  )
    12  
    13  type DisableResourceVersionCommand struct {
    14  	Resource flaghelpers.ResourceFlag `short:"r" long:"resource" required:"true" value-name:"PIPELINE/RESOURCE" description:"Name of the resource"`
    15  	Version  *atc.Version             `short:"v" long:"version" required:"true" value-name:"KEY:VALUE" description:"Version of the resource to disable. 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 disable the latest one."`
    16  }
    17  
    18  func (command *DisableResourceVersionCommand) Execute([]string) error {
    19  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	err = target.Validate()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	team := target.Team()
    30  
    31  	if command.Version != nil {
    32  		latestResourceVer, err := GetLatestResourceVersion(team, command.Resource, *command.Version)
    33  		if err != nil {
    34  			return err
    35  		}
    36  
    37  		disabled := !latestResourceVer.Enabled
    38  
    39  		if !disabled {
    40  			disabled, err = team.DisableResourceVersion(command.Resource.PipelineRef, command.Resource.ResourceName, latestResourceVer.ID)
    41  			if err != nil {
    42  				return err
    43  			}
    44  		}
    45  
    46  		if disabled {
    47  			disableVersionBytes, err := json.Marshal(latestResourceVer.Version)
    48  			if err != nil {
    49  				return err
    50  			}
    51  
    52  			fmt.Printf("disabled '%s/%s' with version %s\n", command.Resource.PipelineRef.String(), command.Resource.ResourceName, string(disableVersionBytes))
    53  		} else {
    54  			displayhelpers.Failf("could not disable '%s/%s', make sure the resource version exists\n", command.Resource.PipelineRef.String(), command.Resource.ResourceName)
    55  		}
    56  	}
    57  
    58  	return nil
    59  }