github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/set_pipeline.go (about) 1 package commands 2 3 import ( 4 "errors" 5 "strings" 6 7 "github.com/pf-qiu/concourse/v6/atc" 8 "github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers" 9 "github.com/pf-qiu/concourse/v6/fly/commands/internal/setpipelinehelpers" 10 "github.com/pf-qiu/concourse/v6/fly/commands/internal/templatehelpers" 11 "github.com/pf-qiu/concourse/v6/fly/rc" 12 "github.com/pf-qiu/concourse/v6/go-concourse/concourse" 13 "github.com/pf-qiu/concourse/v6/vars" 14 15 "github.com/mgutz/ansi" 16 ) 17 18 type SetPipelineCommand struct { 19 SkipInteractive bool `short:"n" long:"non-interactive" description:"Skips interactions, uses default values"` 20 DisableAnsiColor bool `long:"no-color" description:"Disable color output"` 21 22 CheckCredentials bool `long:"check-creds" description:"Validate credential variables against credential manager"` 23 24 PipelineName string `short:"p" long:"pipeline" required:"true" description:"Pipeline to configure"` 25 Config atc.PathFlag `short:"c" long:"config" required:"true" description:"Pipeline configuration file, \"-\" stands for stdin"` 26 27 Var []flaghelpers.VariablePairFlag `short:"v" long:"var" unquote:"false" value-name:"[NAME=STRING]" description:"Specify a string value to set for a variable in the pipeline"` 28 YAMLVar []flaghelpers.YAMLVariablePairFlag `short:"y" long:"yaml-var" unquote:"false" value-name:"[NAME=YAML]" description:"Specify a YAML value to set for a variable in the pipeline"` 29 InstanceVars []flaghelpers.YAMLVariablePairFlag `short:"i" long:"instance-var" unquote:"false" hidden:"true" value-name:"[NAME=STRING]" description:"Specify a YAML value to set for an instance variable"` 30 31 VarsFrom []atc.PathFlag `short:"l" long:"load-vars-from" description:"Variable flag that can be used for filling in template values in configuration from a YAML file"` 32 33 Team string `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"` 34 } 35 36 func (command *SetPipelineCommand) Validate() ([]concourse.ConfigWarning, error) { 37 var warnings []concourse.ConfigWarning 38 var err error 39 if strings.Contains(command.PipelineName, "/") { 40 err = errors.New("pipeline name cannot contain '/'") 41 } 42 if command.Team != "" { 43 if warning := atc.ValidateIdentifier(command.Team, "team"); warning != nil { 44 warnings = append(warnings, concourse.ConfigWarning{ 45 Type: warning.Type, 46 Message: warning.Message, 47 }) 48 } 49 } 50 return warnings, err 51 } 52 53 func (command *SetPipelineCommand) Execute(args []string) error { 54 warnings, err := command.Validate() 55 if err != nil { 56 return err 57 } 58 configPath := command.Config 59 templateVariablesFiles := command.VarsFrom 60 pipelineName := command.PipelineName 61 62 target, err := rc.LoadTarget(Fly.Target, Fly.Verbose) 63 if err != nil { 64 return err 65 } 66 67 err = target.Validate() 68 if err != nil { 69 return err 70 } 71 72 var team concourse.Team 73 74 if command.Team != "" { 75 team, err = target.FindTeam(command.Team) 76 if err != nil { 77 return err 78 } 79 } else { 80 team = target.Team() 81 } 82 83 ansi.DisableColors(command.DisableAnsiColor) 84 85 var instanceVars atc.InstanceVars 86 if command.InstanceVars != nil { 87 var kvPairs vars.KVPairs 88 for _, iv := range command.InstanceVars { 89 kvPairs = append(kvPairs, vars.KVPair(iv)) 90 } 91 instanceVars = atc.InstanceVars(kvPairs.Expand()) 92 } 93 94 atcConfig := setpipelinehelpers.ATCConfig{ 95 Team: team, 96 PipelineRef: atc.PipelineRef{ 97 Name: pipelineName, 98 InstanceVars: instanceVars, 99 }, 100 TargetName: Fly.Target, 101 Target: target.Client().URL(), 102 SkipInteraction: command.SkipInteractive || command.Config.FromStdin(), 103 CheckCredentials: command.CheckCredentials, 104 CommandWarnings: warnings, 105 } 106 107 yamlTemplateWithParams := templatehelpers.NewYamlTemplateWithParams(configPath, templateVariablesFiles, command.Var, command.YAMLVar, instanceVars) 108 return atcConfig.Set(yamlTemplateWithParams) 109 }