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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     7  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
     8  	"github.com/chenbh/concourse/v6/fly/rc"
     9  )
    10  
    11  type RenamePipelineCommand struct {
    12  	Pipeline flaghelpers.PipelineFlag `short:"o"  long:"old-name" required:"true"  description:"Pipeline to rename"`
    13  	Name     flaghelpers.PipelineFlag `short:"n"  long:"new-name" required:"true"  description:"Name to set as pipeline name"`
    14  }
    15  
    16  func (command *RenamePipelineCommand) Validate() error {
    17  	_, err := command.Pipeline.Validate()
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	_, err = command.Name.Validate()
    23  	return err
    24  }
    25  
    26  func (command *RenamePipelineCommand) Execute([]string) error {
    27  	err := command.Validate()
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	err = target.Validate()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	oldName := string(command.Pipeline)
    43  	newName := string(command.Name)
    44  
    45  	found, warnings, err := target.Team().RenamePipeline(oldName, newName)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	if len(warnings) > 0 {
    51  		displayhelpers.ShowWarnings(warnings)
    52  	}
    53  
    54  	if !found {
    55  		displayhelpers.Failf("pipeline '%s' not found\n", oldName)
    56  		return nil
    57  	}
    58  
    59  	fmt.Printf("pipeline successfully renamed to %s\n", newName)
    60  
    61  	return nil
    62  }