add 'mc update' progress bar support (#3782)

This commit is contained in:
Harshavardhana
2021-08-21 21:01:31 -07:00
committed by GitHub
parent a88acb58d4
commit c19d58bb88
5 changed files with 145 additions and 24 deletions
+3
View File
@@ -455,6 +455,9 @@ func deleteFile(deletePath string) error {
if isSysErrNotEmpty(e) {
return nil
}
if os.IsNotExist(e) {
return nil
}
return e
}
+48
View File
@@ -18,6 +18,7 @@
package cmd
import (
"io"
"runtime"
"strings"
"time"
@@ -33,6 +34,53 @@ type progressBar struct {
*pb.ProgressBar
}
func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader {
// Progress bar specific theme customization.
console.SetColor("Bar", color.New(color.FgGreen, color.Bold))
// get the new original progress bar.
bar := pb.New64(total)
// Set new human friendly print units.
bar.SetUnits(pb.U_BYTES)
// Refresh rate for progress bar is set to 125 milliseconds.
bar.SetRefreshRate(time.Millisecond * 125)
// Do not print a newline by default handled, it is handled manually.
bar.NotPrint = true
// Show current speed is true.
bar.ShowSpeed = true
// Custom callback with colorized bar.
bar.Callback = func(s string) {
console.Print(console.Colorize("Bar", "\r"+s))
}
// Use different unicodes for Linux, OS X and Windows.
switch runtime.GOOS {
case "linux":
// Need to add '\x00' as delimiter for unicode characters.
bar.Format("┃\x00▓\x00█\x00░\x00┃")
case "darwin":
// Need to add '\x00' as delimiter for unicode characters.
bar.Format(" \x00▓\x00 \x00░\x00 ")
default:
// Default to non unicode characters.
bar.Format("[=> ]")
}
// Start the progress bar.
bar.Start()
if caption != "" {
bar.Prefix(caption)
}
return bar.NewProxyReader(r)
}
// newProgressBar - instantiate a progress bar.
func newProgressBar(total int64) *progressBar {
// Progress bar speific theme customization.
+84 -20
View File
@@ -19,22 +19,29 @@ package cmd
import (
"crypto"
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/fatih/color"
"github.com/inconshreveable/go-update"
isatty "github.com/mattn/go-isatty"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/env"
"github.com/minio/selfupdate"
_ "github.com/minio/sha256-simd" // Needed for sha256 hash verifier.
)
@@ -74,6 +81,8 @@ const (
mcReleaseTagTimeLayout = "2006-01-02T15-04-05Z"
mcOSARCH = runtime.GOOS + "-" + runtime.GOARCH
mcReleaseURL = "https://dl.min.io/client/mc/release/" + mcOSARCH + "/"
envMinisignPubKey = "MC_UPDATE_MINISIGN_PUBKEY"
)
var (
@@ -320,12 +329,7 @@ func getDownloadURL(releaseTag string) (downloadURL string) {
return fmt.Sprintf("docker pull minio/mc:%s", releaseTag)
}
// For binary only installations, we return link to the latest binary.
if runtime.GOOS == "windows" {
return mcReleaseURL + "mc.exe"
}
return mcReleaseURL + "mc"
return mcReleaseURL + "archive/" + "mc." + releaseTag
}
func getUpdateInfo(timeout time.Duration) (updateMsg string, sha256Hex string, currentReleaseTime, latestReleaseTime time.Time, err *probe.Error) {
@@ -378,33 +382,93 @@ var (
}()
)
func getUpdateTransport(timeout time.Duration) http.RoundTripper {
var updateTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
DualStack: true,
}).DialContext,
IdleConnTimeout: timeout,
TLSHandshakeTimeout: timeout,
ExpectContinueTimeout: timeout,
TLSClientConfig: &tls.Config{
RootCAs: globalRootCAs,
},
DisableCompression: true,
}
return updateTransport
}
func getUpdateReaderFromURL(u *url.URL, transport http.RoundTripper) (io.ReadCloser, error) {
clnt := &http.Client{
Transport: transport,
}
req, e := http.NewRequest(http.MethodGet, u.String(), nil)
if e != nil {
return nil, e
}
req.Header.Set("User-Agent", getUserAgent())
resp, e := clnt.Do(req)
if e != nil {
return nil, e
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
return newProgressReader(resp.Body, "mc", resp.ContentLength), nil
}
func doUpdate(sha256Hex string, latestReleaseTime time.Time, ok bool) (updateStatusMsg string, err *probe.Error) {
if !ok {
updateStatusMsg = colorGreenBold("mc update to version RELEASE.%s canceled.",
latestReleaseTime.Format(mcReleaseTagTimeLayout))
return updateStatusMsg, nil
}
var sha256Sum []byte
var e error
sha256Sum, e = hex.DecodeString(sha256Hex)
sha256Sum, e := hex.DecodeString(sha256Hex)
if e != nil {
return updateStatusMsg, probe.NewError(e)
}
resp, e := http.Get(getDownloadURL(releaseTimeToReleaseTag(latestReleaseTime)))
releaseTag := releaseTimeToReleaseTag(latestReleaseTime)
u, e := url.Parse(getDownloadURL(releaseTag))
if err != nil {
return updateStatusMsg, probe.NewError(e)
}
transport := getUpdateTransport(30 * time.Second)
rc, e := getUpdateReaderFromURL(u, transport)
if e != nil {
return updateStatusMsg, probe.NewError(e)
}
defer resp.Body.Close()
defer rc.Close()
// FIXME: add support for gpg verification as well.
if e = update.Apply(resp.Body,
update.Options{
Hash: crypto.SHA256,
Checksum: sha256Sum,
},
); e != nil {
return updateStatusMsg, probe.NewError(e)
opts := selfupdate.Options{
Hash: crypto.SHA256,
Checksum: sha256Sum,
}
minisignPubkey := env.Get(envMinisignPubKey, "")
if minisignPubkey != "" {
v := selfupdate.NewVerifier()
u.Path = path.Dir(u.Path) + "/mc." + releaseTag + ".minisig"
if e = v.LoadFromURL(u.String(), minisignPubkey, transport); e != nil {
return updateStatusMsg, probe.NewError(e)
}
opts.Verifier = v
}
if e = selfupdate.Apply(rc, opts); e != nil {
if re := selfupdate.RollbackError(e); re != nil {
return updateStatusMsg, probe.NewError(e)
}
}
return colorGreenBold("mc updated to version RELEASE.%s successfully.",