selfupdate: obey --no-check-certificate flag

This patch makes sure we use our own HTTP transport when fetching the
current rclone version.

This allows it to use --no-check-certificate (and any other features
of our own transport).

See: https://forum.rclone.org/t/rclone-selfupdate-no-check-certificate-flag-not-work/37501
This commit is contained in:
Nick Craig-Wood 2023-04-11 10:22:18 +01:00
parent e82db0b7d5
commit 4471e6f258
2 changed files with 11 additions and 7 deletions

View File

@ -68,13 +68,14 @@ var cmdSelfUpdate = &cobra.Command{
"versionIntroduced": "v1.55", "versionIntroduced": "v1.55",
}, },
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
ctx := context.Background()
cmd.CheckArgs(0, 0, command, args) cmd.CheckArgs(0, 0, command, args)
if Opt.Package == "" { if Opt.Package == "" {
Opt.Package = "zip" Opt.Package = "zip"
} }
gotActionFlags := Opt.Stable || Opt.Beta || Opt.Output != "" || Opt.Version != "" || Opt.Package != "zip" gotActionFlags := Opt.Stable || Opt.Beta || Opt.Output != "" || Opt.Version != "" || Opt.Package != "zip"
if Opt.Check && !gotActionFlags { if Opt.Check && !gotActionFlags {
versionCmd.CheckVersion() versionCmd.CheckVersion(ctx)
return return
} }
if Opt.Package != "zip" { if Opt.Package != "zip" {
@ -108,7 +109,7 @@ func GetVersion(ctx context.Context, beta bool, version string) (newVersion, sit
if version == "" { if version == "" {
// Request the latest release number from the download site // Request the latest release number from the download site
_, newVersion, _, err = versionCmd.GetVersion(siteURL + "/version.txt") _, newVersion, _, err = versionCmd.GetVersion(ctx, siteURL+"/version.txt")
return return
} }

View File

@ -2,6 +2,7 @@
package version package version
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -13,6 +14,7 @@ import (
"github.com/rclone/rclone/cmd" "github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/fshttp"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -71,9 +73,10 @@ Or
"versionIntroduced": "v1.33", "versionIntroduced": "v1.33",
}, },
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
ctx := context.Background()
cmd.CheckArgs(0, 0, command, args) cmd.CheckArgs(0, 0, command, args)
if check { if check {
CheckVersion() CheckVersion(ctx)
} else { } else {
cmd.ShowVersion() cmd.ShowVersion()
} }
@ -89,8 +92,8 @@ func stripV(s string) string {
} }
// GetVersion gets the version available for download // GetVersion gets the version available for download
func GetVersion(url string) (v *semver.Version, vs string, date time.Time, err error) { func GetVersion(ctx context.Context, url string) (v *semver.Version, vs string, date time.Time, err error) {
resp, err := http.Get(url) resp, err := fshttp.NewClient(ctx).Get(url)
if err != nil { if err != nil {
return v, vs, date, err return v, vs, date, err
} }
@ -114,7 +117,7 @@ func GetVersion(url string) (v *semver.Version, vs string, date time.Time, err e
} }
// CheckVersion checks the installed version against available downloads // CheckVersion checks the installed version against available downloads
func CheckVersion() { func CheckVersion(ctx context.Context) {
vCurrent, err := semver.NewVersion(stripV(fs.Version)) vCurrent, err := semver.NewVersion(stripV(fs.Version))
if err != nil { if err != nil {
fs.Errorf(nil, "Failed to parse version: %v", err) fs.Errorf(nil, "Failed to parse version: %v", err)
@ -122,7 +125,7 @@ func CheckVersion() {
const timeFormat = "2006-01-02" const timeFormat = "2006-01-02"
printVersion := func(what, url string) { printVersion := func(what, url string) {
v, vs, t, err := GetVersion(url + "version.txt") v, vs, t, err := GetVersion(ctx, url+"version.txt")
if err != nil { if err != nil {
fs.Errorf(nil, "Failed to get rclone %s version: %v", what, err) fs.Errorf(nil, "Failed to get rclone %s version: %v", what, err)
return return