github.com/chenbh/concourse/v6@v6.4.2/fly/commands/internal/setpipelinehelpers/atc_config.go (about) 1 package setpipelinehelpers 2 3 import ( 4 "fmt" 5 "net/url" 6 "os" 7 "sigs.k8s.io/yaml" 8 9 "github.com/vito/go-interact/interact" 10 11 "github.com/chenbh/concourse/v6/atc" 12 "github.com/chenbh/concourse/v6/atc/configvalidate" 13 "github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers" 14 "github.com/chenbh/concourse/v6/fly/commands/internal/templatehelpers" 15 "github.com/chenbh/concourse/v6/fly/rc" 16 "github.com/chenbh/concourse/v6/fly/ui" 17 "github.com/chenbh/concourse/v6/go-concourse/concourse" 18 ) 19 20 type ATCConfig struct { 21 PipelineName string 22 Team concourse.Team 23 TargetName rc.TargetName 24 Target string 25 SkipInteraction bool 26 CheckCredentials bool 27 CommandWarnings []concourse.ConfigWarning 28 } 29 30 func (atcConfig ATCConfig) ApplyConfigInteraction() bool { 31 if atcConfig.SkipInteraction { 32 return true 33 } 34 35 confirm := false 36 err := interact.NewInteraction("apply configuration?").Resolve(&confirm) 37 if err != nil { 38 return false 39 } 40 41 return confirm 42 } 43 44 func (atcConfig ATCConfig) Set(yamlTemplateWithParams templatehelpers.YamlTemplateWithParams) error { 45 evaluatedTemplate, err := yamlTemplateWithParams.Evaluate(false, false) 46 if err != nil { 47 return err 48 } 49 50 existingConfig, existingConfigVersion, _, err := atcConfig.Team.PipelineConfig(atcConfig.PipelineName) 51 if err != nil { 52 return err 53 } 54 55 var newConfig atc.Config 56 err = yaml.Unmarshal([]byte(evaluatedTemplate), &newConfig) 57 if err != nil { 58 return err 59 } 60 61 configWarnings, _ := configvalidate.Validate(newConfig) 62 for _, w := range configWarnings { 63 atcConfig.CommandWarnings = append(atcConfig.CommandWarnings, concourse.ConfigWarning{ 64 Type: w.Type, 65 Message: w.Message, 66 }) 67 } 68 69 diffExists := diff(existingConfig, newConfig) 70 71 if len(atcConfig.CommandWarnings) > 0 { 72 displayhelpers.ShowWarnings(atcConfig.CommandWarnings) 73 } 74 75 if !diffExists { 76 fmt.Println("no changes to apply") 77 return nil 78 } 79 80 if !atcConfig.ApplyConfigInteraction() { 81 fmt.Println("bailing out") 82 return nil 83 } 84 85 created, updated, warnings, err := atcConfig.Team.CreateOrUpdatePipelineConfig( 86 atcConfig.PipelineName, 87 existingConfigVersion, 88 evaluatedTemplate, 89 atcConfig.CheckCredentials, 90 ) 91 if err != nil { 92 return err 93 } 94 95 updatedConfig, found, err := atcConfig.Team.Pipeline(atcConfig.PipelineName) 96 if err != nil { 97 return err 98 } 99 100 paused := found && updatedConfig.Paused 101 102 if len(warnings) > 0 { 103 displayhelpers.ShowWarnings(warnings) 104 } 105 106 atcConfig.showPipelineUpdateResult(created, updated, paused) 107 return nil 108 } 109 110 func (atcConfig ATCConfig) UnpausePipelineCommand() string { 111 return fmt.Sprintf("%s -t %s unpause-pipeline -p %s", os.Args[0], atcConfig.TargetName, atcConfig.PipelineName) 112 } 113 114 func (atcConfig ATCConfig) showPipelineUpdateResult(created bool, updated bool, paused bool) { 115 if updated { 116 fmt.Println("configuration updated") 117 } else if created { 118 119 targetURL, err := url.Parse(atcConfig.Target) 120 if err != nil { 121 fmt.Println("Could not parse targetURL") 122 } 123 124 pipelineURL, err := url.Parse("/teams/" + atcConfig.Team.Name() + "/pipelines/" + atcConfig.PipelineName) 125 if err != nil { 126 fmt.Println("Could not parse pipelineURL") 127 } 128 129 fmt.Println("pipeline created!") 130 fmt.Printf("you can view your pipeline here: %s\n", targetURL.ResolveReference(pipelineURL)) 131 } else { 132 panic("Something really went wrong!") 133 } 134 135 if paused { 136 fmt.Println("") 137 fmt.Println("the pipeline is currently paused. to unpause, either:") 138 fmt.Println(" - run the unpause-pipeline command:") 139 fmt.Println(" " + atcConfig.UnpausePipelineCommand()) 140 fmt.Println(" - click play next to the pipeline in the web ui") 141 } 142 } 143 144 func diff(existingConfig atc.Config, newConfig atc.Config) bool { 145 stdout, _ := ui.ForTTY(os.Stdout) 146 return existingConfig.Diff(stdout, newConfig) 147 }