github.com/chenbh/concourse/v6@v6.4.2/fly/commands/enable_resource_version.go (about)

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/chenbh/concourse/v6/atc"
     8  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     9  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
    10  	"github.com/chenbh/concourse/v6/fly/rc"
    11  )
    12  
    13  type EnableResourceVersionCommand 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 enable. 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 enable the latest one."`
    16  }
    17  
    18  func (command *EnableResourceVersionCommand) 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  		enabled := latestResourceVer.Enabled
    38  
    39  		if !enabled {
    40  			enabled, err = team.EnableResourceVersion(command.Resource.PipelineName, command.Resource.ResourceName, latestResourceVer.ID)
    41  			if err != nil {
    42  				return err
    43  			}
    44  		}
    45  
    46  		if enabled {
    47  			enableVersionBytes, err := json.Marshal(latestResourceVer.Version)
    48  			if err != nil {
    49  				return err
    50  			}
    51  
    52  			fmt.Printf("enabled '%s/%s' with version %s\n", command.Resource.PipelineName, command.Resource.ResourceName, string(enableVersionBytes))
    53  		} else {
    54  			displayhelpers.Failf("could not enable '%s/%s', make sure the resource version exists\n", command.Resource.PipelineName, command.Resource.ResourceName)
    55  		}
    56  	}
    57  
    58  	return nil
    59  }