github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/sync.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "runtime" 6 "strconv" 7 8 update "github.com/inconshreveable/go-update" 9 "github.com/vbauerster/mpb/v4" 10 "github.com/vbauerster/mpb/v4/decor" 11 12 "github.com/pf-qiu/concourse/v6/atc" 13 "github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers" 14 "github.com/pf-qiu/concourse/v6/fly/rc" 15 "github.com/pf-qiu/concourse/v6/fly/ui" 16 ) 17 18 type SyncCommand struct { 19 Force bool `long:"force" short:"f" description:"Sync even if versions already match."` 20 ATCURL string `long:"concourse-url" short:"c" description:"Concourse URL to sync with"` 21 Insecure bool `short:"k" long:"insecure" description:"Skip verification of the endpoint's SSL certificate"` 22 CACert atc.PathFlag `long:"ca-cert" description:"Path to Concourse PEM-encoded CA certificate file."` 23 ClientCertPath atc.PathFlag `long:"client-cert" description:"Path to a PEM-encoded client certificate file."` 24 ClientKeyPath atc.PathFlag `long:"client-key" description:"Path to a PEM-encoded client key file."` 25 } 26 27 func (command *SyncCommand) Execute(args []string) error { 28 var target rc.Target 29 var err error 30 31 if Fly.Target != "" { 32 target, err = rc.LoadTarget(Fly.Target, Fly.Verbose) 33 } else { 34 target, err = rc.NewUnauthenticatedTarget( 35 "dummy", 36 command.ATCURL, 37 "", 38 command.Insecure, 39 string(command.CACert), 40 string(command.ClientCertPath), 41 string(command.ClientKeyPath), 42 Fly.Verbose, 43 ) 44 } 45 if err != nil { 46 return err 47 } 48 49 info, err := target.Client().GetInfo() 50 if err != nil { 51 return err 52 } 53 54 if !command.Force && info.Version == rc.LocalVersion { 55 fmt.Printf("version %s already matches; skipping\n", info.Version) 56 return nil 57 } 58 59 updateOptions := update.Options{} 60 err = updateOptions.CheckPermissions() 61 if err != nil { 62 displayhelpers.FailWithErrorf("update failed", err) 63 } 64 65 client := target.Client() 66 body, headers, err := client.GetCLIReader(runtime.GOARCH, runtime.GOOS) 67 if err != nil { 68 return err 69 } 70 71 fmt.Printf("downloading fly from %s...\n", client.URL()) 72 fmt.Println() 73 74 size, err := strconv.ParseInt(headers.Get("Content-Length"), 10, 64) 75 if err != nil { 76 fmt.Printf("warning: failed to parse Content-Length: %s\n", err) 77 size = 0 78 } 79 80 progress := mpb.New(mpb.WithWidth(50)) 81 82 progressBar := progress.AddBar( 83 size, 84 mpb.PrependDecorators(decor.Name("fly "+ui.Embolden("v"+info.Version))), 85 mpb.AppendDecorators(decor.CountersKibiByte("%.1f/%.1f")), 86 ) 87 88 err = update.Apply(progressBar.ProxyReader(body), updateOptions) 89 if err != nil { 90 displayhelpers.FailWithErrorf("failed to apply update", err) 91 } 92 93 if size == 0 { 94 progressBar.SetTotal(progressBar.Current(), true) 95 } 96 97 progress.Wait() 98 99 fmt.Println("done") 100 101 return nil 102 }