github.com/chenbh/concourse/v6@v6.4.2/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/chenbh/concourse/v6" 13 "github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers" 14 "github.com/chenbh/concourse/v6/fly/rc" 15 "github.com/chenbh/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 } 22 23 func (command *SyncCommand) Execute(args []string) error { 24 var target rc.Target 25 var err error 26 27 if Fly.Target != "" { 28 target, err = rc.LoadTarget(Fly.Target, Fly.Verbose) 29 } else { 30 target, err = rc.NewUnauthenticatedTarget( 31 "dummy", 32 command.ATCURL, 33 "", 34 false, 35 "", 36 Fly.Verbose, 37 ) 38 } 39 if err != nil { 40 return err 41 } 42 43 info, err := target.Client().GetInfo() 44 if err != nil { 45 return err 46 } 47 48 if !command.Force && info.Version == concourse.Version { 49 fmt.Printf("version %s already matches; skipping\n", info.Version) 50 return nil 51 } 52 53 updateOptions := update.Options{} 54 err = updateOptions.CheckPermissions() 55 if err != nil { 56 displayhelpers.FailWithErrorf("update failed", err) 57 } 58 59 client := target.Client() 60 body, headers, err := client.GetCLIReader(runtime.GOARCH, runtime.GOOS) 61 if err != nil { 62 return err 63 } 64 65 fmt.Printf("downloading fly from %s...\n", client.URL()) 66 fmt.Println() 67 68 size, err := strconv.ParseInt(headers.Get("Content-Length"), 10, 64) 69 if err != nil { 70 fmt.Printf("warning: failed to parse Content-Length: %s\n", err) 71 size = 0 72 } 73 74 progress := mpb.New(mpb.WithWidth(50)) 75 76 progressBar := progress.AddBar( 77 size, 78 mpb.PrependDecorators(decor.Name("fly "+ui.Embolden("v"+info.Version))), 79 mpb.AppendDecorators(decor.CountersKibiByte("%.1f/%.1f")), 80 ) 81 82 err = update.Apply(progressBar.ProxyReader(body), updateOptions) 83 if err != nil { 84 displayhelpers.FailWithErrorf("failed to apply update", err) 85 } 86 87 if size == 0 { 88 progressBar.SetTotal(progressBar.Current(), true) 89 } 90 91 progress.Wait() 92 93 fmt.Println("done") 94 95 return nil 96 }