2022-08-28 11:21:57 +00:00
|
|
|
// Package version provides the version command.
|
2016-08-05 16:12:27 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
2023-04-11 09:22:18 +00:00
|
|
|
"context"
|
2021-11-04 10:12:57 +00:00
|
|
|
"errors"
|
2018-06-15 15:22:59 +00:00
|
|
|
"fmt"
|
2022-08-20 14:38:02 +00:00
|
|
|
"io"
|
2018-06-15 15:22:59 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-08-18 20:21:53 +00:00
|
|
|
"github.com/coreos/go-semver/semver"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 15:55:04 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2023-04-11 09:22:18 +00:00
|
|
|
"github.com/rclone/rclone/fs/fshttp"
|
2016-08-05 16:12:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-06-15 15:22:59 +00:00
|
|
|
var (
|
|
|
|
check = false
|
|
|
|
)
|
|
|
|
|
2016-08-05 16:12:27 +00:00
|
|
|
func init() {
|
2018-03-17 11:36:30 +00:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 15:55:04 +00:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2021-08-16 09:30:01 +00:00
|
|
|
flags.BoolVarP(cmdFlags, &check, "check", "", false, "Check for new version")
|
2016-08-05 16:12:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:36:30 +00:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-05 16:12:27 +00:00
|
|
|
Use: "version",
|
|
|
|
Short: `Show the version number.`,
|
2018-06-15 15:22:59 +00:00
|
|
|
Long: `
|
2021-04-05 18:53:09 +00:00
|
|
|
Show the rclone version number, the go version, the build target
|
|
|
|
OS and architecture, the runtime OS and kernel version and bitness,
|
|
|
|
build tags and the type of executable (static or dynamic).
|
2018-06-15 15:22:59 +00:00
|
|
|
|
2021-03-21 12:13:09 +00:00
|
|
|
For example:
|
2018-06-15 15:22:59 +00:00
|
|
|
|
|
|
|
$ rclone version
|
2021-04-05 18:53:09 +00:00
|
|
|
rclone v1.55.0
|
|
|
|
- os/version: ubuntu 18.04 (64 bit)
|
|
|
|
- os/kernel: 4.15.0-136-generic (x86_64)
|
2021-03-21 12:13:09 +00:00
|
|
|
- os/type: linux
|
|
|
|
- os/arch: amd64
|
|
|
|
- go/version: go1.16
|
|
|
|
- go/linking: static
|
|
|
|
- go/tags: none
|
|
|
|
|
|
|
|
Note: before rclone version 1.55 the os/type and os/arch lines were merged,
|
|
|
|
and the "go/version" line was tagged as "go version".
|
2018-06-15 15:22:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
`,
|
2022-11-26 22:40:49 +00:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.33",
|
|
|
|
},
|
2016-08-05 16:12:27 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2023-04-11 09:22:18 +00:00
|
|
|
ctx := context.Background()
|
2016-08-05 16:12:27 +00:00
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
2018-06-15 15:22:59 +00:00
|
|
|
if check {
|
2023-04-11 09:22:18 +00:00
|
|
|
CheckVersion(ctx)
|
2018-06-15 15:22:59 +00:00
|
|
|
} else {
|
|
|
|
cmd.ShowVersion()
|
|
|
|
}
|
2016-08-05 16:12:27 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-15 15:22:59 +00:00
|
|
|
|
2020-08-18 20:21:53 +00:00
|
|
|
// strip a leading v off the string
|
|
|
|
func stripV(s string) string {
|
|
|
|
if len(s) > 0 && s[0] == 'v' {
|
|
|
|
return s[1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:39:30 +00:00
|
|
|
// GetVersion gets the version available for download
|
2023-04-11 09:22:18 +00:00
|
|
|
func GetVersion(ctx context.Context, url string) (v *semver.Version, vs string, date time.Time, err error) {
|
|
|
|
resp, err := fshttp.NewClient(ctx).Get(url)
|
2018-06-15 15:22:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-08-20 14:38:02 +00:00
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
2018-06-15 15:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return v, vs, date, err
|
|
|
|
}
|
|
|
|
vs = strings.TrimSpace(string(bodyBytes))
|
2021-03-21 12:13:09 +00:00
|
|
|
vs = strings.TrimPrefix(vs, "rclone ")
|
2018-06-15 15:22:59 +00:00
|
|
|
vs = strings.TrimRight(vs, "β")
|
|
|
|
date, err = http.ParseTime(resp.Header.Get("Last-Modified"))
|
|
|
|
if err != nil {
|
|
|
|
return v, vs, date, err
|
|
|
|
}
|
2020-08-18 20:21:53 +00:00
|
|
|
v, err = semver.NewVersion(stripV(vs))
|
2018-06-15 15:22:59 +00:00
|
|
|
return v, vs, date, err
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:39:30 +00:00
|
|
|
// CheckVersion checks the installed version against available downloads
|
2023-04-11 09:22:18 +00:00
|
|
|
func CheckVersion(ctx context.Context) {
|
2020-08-18 20:21:53 +00:00
|
|
|
vCurrent, err := semver.NewVersion(stripV(fs.Version))
|
2018-06-15 15:22:59 +00:00
|
|
|
if err != nil {
|
2020-08-18 20:21:53 +00:00
|
|
|
fs.Errorf(nil, "Failed to parse version: %v", err)
|
2018-06-15 15:22:59 +00:00
|
|
|
}
|
|
|
|
const timeFormat = "2006-01-02"
|
|
|
|
|
|
|
|
printVersion := func(what, url string) {
|
2023-04-11 09:22:18 +00:00
|
|
|
v, vs, t, err := GetVersion(ctx, url+"version.txt")
|
2018-06-15 15:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to get rclone %s version: %v", what, err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-18 20:21:53 +00:00
|
|
|
fmt.Printf("%-8s%-40v %20s\n",
|
2018-06-15 15:22:59 +00:00
|
|
|
what+":",
|
|
|
|
v,
|
|
|
|
"(released "+t.Format(timeFormat)+")",
|
|
|
|
)
|
2020-08-18 20:21:53 +00:00
|
|
|
if v.Compare(*vCurrent) > 0 {
|
2018-06-15 15:22:59 +00: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/",
|
|
|
|
)
|
2020-08-18 20:21:53 +00:00
|
|
|
if strings.HasSuffix(fs.Version, "-DEV") {
|
2018-06-15 15:22:59 +00:00
|
|
|
fmt.Println("Your version is compiled from git so comparisons may be wrong.")
|
|
|
|
}
|
|
|
|
}
|