github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/cmd/version/version.go (about) 1 package version 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "time" 9 10 "github.com/ncw/rclone/cmd" 11 "github.com/ncw/rclone/fs" 12 "github.com/ncw/rclone/fs/version" 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 ) 16 17 var ( 18 check = false 19 ) 20 21 func init() { 22 cmd.Root.AddCommand(commandDefinition) 23 flags := commandDefinition.Flags() 24 flags.BoolVarP(&check, "check", "", false, "Check for new version.") 25 } 26 27 var commandDefinition = &cobra.Command{ 28 Use: "version", 29 Short: `Show the version number.`, 30 Long: ` 31 Show the version number, the go version and the architecture. 32 33 Eg 34 35 $ rclone version 36 rclone v1.41 37 - os/arch: linux/amd64 38 - go version: go1.10 39 40 If you supply the --check flag, then it will do an online check to 41 compare your version with the latest release and the latest beta. 42 43 $ rclone version --check 44 yours: 1.42.0.6 45 latest: 1.42 (released 2018-06-16) 46 beta: 1.42.0.5 (released 2018-06-17) 47 48 Or 49 50 $ rclone version --check 51 yours: 1.41 52 latest: 1.42 (released 2018-06-16) 53 upgrade: https://downloads.rclone.org/v1.42 54 beta: 1.42.0.5 (released 2018-06-17) 55 upgrade: https://beta.rclone.org/v1.42-005-g56e1e820 56 57 `, 58 Run: func(command *cobra.Command, args []string) { 59 cmd.CheckArgs(0, 0, command, args) 60 if check { 61 checkVersion() 62 } else { 63 cmd.ShowVersion() 64 } 65 }, 66 } 67 68 // getVersion gets the version by checking the download repository passed in 69 func getVersion(url string) (v version.Version, vs string, date time.Time, err error) { 70 resp, err := http.Get(url) 71 if err != nil { 72 return v, vs, date, err 73 } 74 defer fs.CheckClose(resp.Body, &err) 75 if resp.StatusCode != http.StatusOK { 76 return v, vs, date, errors.New(resp.Status) 77 } 78 bodyBytes, err := ioutil.ReadAll(resp.Body) 79 if err != nil { 80 return v, vs, date, err 81 } 82 vs = strings.TrimSpace(string(bodyBytes)) 83 if strings.HasPrefix(vs, "rclone ") { 84 vs = vs[7:] 85 } 86 vs = strings.TrimRight(vs, "β") 87 date, err = http.ParseTime(resp.Header.Get("Last-Modified")) 88 if err != nil { 89 return v, vs, date, err 90 } 91 v, err = version.New(vs) 92 return v, vs, date, err 93 } 94 95 // check the current version against available versions 96 func checkVersion() { 97 // Get Current version 98 vCurrent, err := version.New(fs.Version) 99 if err != nil { 100 fs.Errorf(nil, "Failed to get parse version: %v", err) 101 } 102 const timeFormat = "2006-01-02" 103 104 printVersion := func(what, url string) { 105 v, vs, t, err := getVersion(url + "version.txt") 106 if err != nil { 107 fs.Errorf(nil, "Failed to get rclone %s version: %v", what, err) 108 return 109 } 110 fmt.Printf("%-8s%-13v %20s\n", 111 what+":", 112 v, 113 "(released "+t.Format(timeFormat)+")", 114 ) 115 if v.Cmp(vCurrent) > 0 { 116 fmt.Printf(" upgrade: %s\n", url+vs) 117 } 118 } 119 fmt.Printf("yours: %-13s\n", vCurrent) 120 printVersion( 121 "latest", 122 "https://downloads.rclone.org/", 123 ) 124 printVersion( 125 "beta", 126 "https://beta.rclone.org/", 127 ) 128 if vCurrent.IsGit() { 129 fmt.Println("Your version is compiled from git so comparisons may be wrong.") 130 } 131 }