storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/update-notifier.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2015, 2016, 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"fmt"
    21  	"math"
    22  	"runtime"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/cheggaaa/pb"
    27  	humanize "github.com/dustin/go-humanize"
    28  
    29  	"storj.io/minio/pkg/color"
    30  )
    31  
    32  // prepareUpdateMessage - prepares the update message, only if a
    33  // newer version is available.
    34  func prepareUpdateMessage(downloadURL string, older time.Duration) string {
    35  	if downloadURL == "" || older <= 0 {
    36  		return ""
    37  	}
    38  
    39  	// Compute friendly duration string to indicate time
    40  	// difference between newer and current release.
    41  	t := time.Time{}
    42  	newerThan := humanize.RelTime(t, t.Add(older), "ago", "")
    43  
    44  	// Return the nicely colored and formatted update message.
    45  	return colorizeUpdateMessage(downloadURL, newerThan)
    46  }
    47  
    48  // colorizeUpdateMessage - inspired from Yeoman project npm package https://github.com/yeoman/update-notifier
    49  func colorizeUpdateMessage(updateString string, newerThan string) string {
    50  	msgLine1Fmt := " You are running an older version of MinIO released %s "
    51  	msgLine2Fmt := " Update: %s "
    52  
    53  	// Calculate length *without* color coding: with ANSI terminal
    54  	// color characters, the result is incorrect.
    55  	line1Length := len(fmt.Sprintf(msgLine1Fmt, newerThan))
    56  	line2Length := len(fmt.Sprintf(msgLine2Fmt, updateString))
    57  
    58  	// Populate lines with color coding.
    59  	line1InColor := fmt.Sprintf(msgLine1Fmt, color.YellowBold(newerThan))
    60  	line2InColor := fmt.Sprintf(msgLine2Fmt, color.CyanBold(updateString))
    61  
    62  	// calculate the rectangular box size.
    63  	maxContentWidth := int(math.Max(float64(line1Length), float64(line2Length)))
    64  
    65  	// termWidth is set to a default one to use when we are
    66  	// not able to calculate terminal width via OS syscalls
    67  	termWidth := 25
    68  	if width, err := pb.GetTerminalWidth(); err == nil {
    69  		termWidth = width
    70  	}
    71  
    72  	// Box cannot be printed if terminal width is small than maxContentWidth
    73  	if maxContentWidth > termWidth {
    74  		return "\n" + line1InColor + "\n" + line2InColor + "\n\n"
    75  	}
    76  
    77  	topLeftChar := "┏"
    78  	topRightChar := "┓"
    79  	bottomLeftChar := "┗"
    80  	bottomRightChar := "┛"
    81  	horizBarChar := "━"
    82  	vertBarChar := "┃"
    83  	// on windows terminal turn off unicode characters.
    84  	if runtime.GOOS == globalWindowsOSName {
    85  		topLeftChar = "+"
    86  		topRightChar = "+"
    87  		bottomLeftChar = "+"
    88  		bottomRightChar = "+"
    89  		horizBarChar = "-"
    90  		vertBarChar = "|"
    91  	}
    92  
    93  	lines := []string{
    94  		color.YellowBold(topLeftChar + strings.Repeat(horizBarChar, maxContentWidth) + topRightChar),
    95  		vertBarChar + line1InColor + strings.Repeat(" ", maxContentWidth-line1Length) + vertBarChar,
    96  		vertBarChar + line2InColor + strings.Repeat(" ", maxContentWidth-line2Length) + vertBarChar,
    97  		color.YellowBold(bottomLeftChar + strings.Repeat(horizBarChar, maxContentWidth) + bottomRightChar),
    98  	}
    99  	return "\n" + strings.Join(lines, "\n") + "\n"
   100  }