2016-08-05 18:12:27 +02:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
2018-06-15 17:22:59 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/version"
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-06-15 17:22:59 +02:00
|
|
|
var (
|
|
|
|
check = false
|
|
|
|
)
|
|
|
|
|
2016-08-05 18:12:27 +02:00
|
|
|
func init() {
|
2018-03-17 12:36:30 +01:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2018-06-15 17:22:59 +02:00
|
|
|
flags := commandDefinition.Flags()
|
|
|
|
flags.BoolVarP(&check, "check", "", false, "Check for new version.")
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-17 12:36:30 +01:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-05 18:12:27 +02:00
|
|
|
Use: "version",
|
|
|
|
Short: `Show the version number.`,
|
2018-06-15 17:22:59 +02:00
|
|
|
Long: `
|
|
|
|
Show the version number, the go version and the architecture.
|
|
|
|
|
|
|
|
Eg
|
|
|
|
|
|
|
|
$ rclone version
|
|
|
|
rclone v1.41
|
|
|
|
- os/arch: linux/amd64
|
|
|
|
- go version: go1.10
|
|
|
|
|
|
|
|
If you supply the --check flag, then it will do an online check to
|
|
|
|
compare your version with the latest release and the latest beta.
|
|
|
|
|
|
|
|
$ rclone version --check
|
|
|
|
yours: 1.42.0.6
|
|
|
|
latest: 1.42 (released 2018-06-16)
|
|
|
|
beta: 1.42.0.5 (released 2018-06-17)
|
|
|
|
|
|
|
|
Or
|
|
|
|
|
|
|
|
$ rclone version --check
|
|
|
|
yours: 1.41
|
|
|
|
latest: 1.42 (released 2018-06-16)
|
|
|
|
upgrade: https://downloads.rclone.org/v1.42
|
|
|
|
beta: 1.42.0.5 (released 2018-06-17)
|
|
|
|
upgrade: https://beta.rclone.org/v1.42-005-g56e1e820
|
|
|
|
|
|
|
|
`,
|
2016-08-05 18:12:27 +02:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
2018-06-15 17:22:59 +02:00
|
|
|
if check {
|
|
|
|
checkVersion()
|
|
|
|
} else {
|
|
|
|
cmd.ShowVersion()
|
|
|
|
}
|
2016-08-05 18:12:27 +02:00
|
|
|
},
|
|
|
|
}
|
2018-06-15 17:22:59 +02:00
|
|
|
|
|
|
|
// getVersion gets the version by checking the download repository passed in
|
2018-11-04 17:16:05 +01:00
|
|
|
func getVersion(url string) (v version.Version, vs string, date time.Time, err error) {
|
2018-06-15 17:22:59 +02:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return v, vs, date, err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return v, vs, date, errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return v, vs, date, err
|
|
|
|
}
|
|
|
|
vs = strings.TrimSpace(string(bodyBytes))
|
|
|
|
if strings.HasPrefix(vs, "rclone ") {
|
|
|
|
vs = vs[7:]
|
|
|
|
}
|
|
|
|
vs = strings.TrimRight(vs, "β")
|
|
|
|
date, err = http.ParseTime(resp.Header.Get("Last-Modified"))
|
|
|
|
if err != nil {
|
|
|
|
return v, vs, date, err
|
|
|
|
}
|
2018-11-04 17:16:05 +01:00
|
|
|
v, err = version.New(vs)
|
2018-06-15 17:22:59 +02:00
|
|
|
return v, vs, date, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the current version against available versions
|
|
|
|
func checkVersion() {
|
|
|
|
// Get Current version
|
2018-11-04 17:16:05 +01:00
|
|
|
vCurrent, err := version.New(fs.Version)
|
2018-06-15 17:22:59 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to get parse version: %v", err)
|
|
|
|
}
|
|
|
|
const timeFormat = "2006-01-02"
|
|
|
|
|
|
|
|
printVersion := func(what, url string) {
|
|
|
|
v, vs, t, err := getVersion(url + "version.txt")
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to get rclone %s version: %v", what, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("%-8s%-13v %20s\n",
|
|
|
|
what+":",
|
|
|
|
v,
|
|
|
|
"(released "+t.Format(timeFormat)+")",
|
|
|
|
)
|
2018-11-04 17:16:05 +01:00
|
|
|
if v.Cmp(vCurrent) > 0 {
|
2018-06-15 17:22:59 +02:00
|
|
|
fmt.Printf(" upgrade: %s\n", url+vs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("yours: %-13s\n", vCurrent)
|
|
|
|
printVersion(
|
|
|
|
"latest",
|
|
|
|
"https://downloads.rclone.org/",
|
|
|
|
)
|
|
|
|
printVersion(
|
|
|
|
"beta",
|
|
|
|
"https://beta.rclone.org/",
|
|
|
|
)
|
2018-11-04 17:16:05 +01:00
|
|
|
if vCurrent.IsGit() {
|
2018-06-15 17:22:59 +02:00
|
|
|
fmt.Println("Your version is compiled from git so comparisons may be wrong.")
|
|
|
|
}
|
|
|
|
}
|