Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom help message and upgrade notification #809

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path"
"regexp"
"strings"

"github.com/fatih/color"
Expand All @@ -16,6 +17,7 @@ import (
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/version"
)

// ValidateConfig holds configuration options for Atmos validation.
Expand Down Expand Up @@ -423,17 +425,80 @@ func printMessageForMissingAtmosConfig(cliConfig schema.CliConfiguration) {

// printMessageToUpgradeToAtmosLatestRelease prints info on how to upgrade Atmos to the latest version
func printMessageToUpgradeToAtmosLatestRelease(latestVersion string) {
Listener430 marked this conversation as resolved.
Show resolved Hide resolved
c1 := color.New(color.FgCyan)
//RGB values to define dark grey color
r1, g1, b1 := 105, 105, 105

c1 := color.RGB(r1, g1, b1)
c2 := color.New(color.FgGreen)
c3 := color.New(color.FgCyan)

message := fmt.Sprintf("Update available! %s » %s", c1.Sprint(version.Version), c2.Sprint(latestVersion))
links := []string{
fmt.Sprintf("Atmos Releases: %s", c3.Sprint("https://github.com/cloudposse/atmos/releases")),
fmt.Sprintf("Install Atmos: %s", c3.Sprint("https://atmos.tools/install")),
}
messageLines := append([]string{message}, links...)
maxWidth := 0
for _, line := range messageLines {
lineLength := len(stripANSI(line))
if lineLength > maxWidth {
maxWidth = lineLength
}
}

// Calculate border width
padding := 2
borderWidth := maxWidth + padding*2

u.PrintMessageInColor(fmt.Sprintf("\nYour version of Atmos is out of date. The latest version is %s\n\n", latestVersion), c1)
u.PrintMessage("To upgrade Atmos, refer to the following links and documents:\n")
// Print top border
c2.Println("┌" + strings.Repeat("─", borderWidth) + "┐")

u.PrintMessageInColor("Atmos Releases:\n", c2)
u.PrintMessage("https://github.com/cloudposse/atmos/releases\n")
// Print each line with padding
for _, line := range messageLines {
lineLength := calculateDisplayWidth(line)
spaces := borderWidth - lineLength
leftPadding := spaces / 2
rightPadding := spaces - leftPadding
fmt.Printf("%s%s%s%s%s\n", c2.Sprint("│"), strings.Repeat(" ", leftPadding), line, strings.Repeat(" ", rightPadding), c2.Sprint("│"))
}

// Print bottom border
c2.Println("└" + strings.Repeat("─", borderWidth) + "┘")
}

u.PrintMessageInColor("Install Atmos:\n", c2)
u.PrintMessage("https://atmos.tools/install\n")
// Function to strip ANSI color codes
func stripANSI(str string) string {
ansi := "\033\\[[0-9;]*m"
re := regexp.MustCompile(ansi)
return re.ReplaceAllString(str, "")
}

// Function to calculate display width, treating '»' as width 1
func calculateDisplayWidth(str string) int {
stripped := stripANSI(str)
width := 0
for _, r := range stripped {
if r == '»' {
width += 1
} else {
width += 1
}
}
return width
}

// customHelpMessageToUpgradeToAtmosLatestRelease adds Atmos version info at the end of each help commnad
func customHelpMessageToUpgradeToAtmosLatestRelease(cmd *cobra.Command, args []string) {
originalHelpFunc(cmd, args)
// Check for the latest Atmos release on GitHub
latestReleaseTag, err := u.GetLatestGitHubRepoRelease("cloudposse", "atmos")
if err == nil && latestReleaseTag != "" {
latestRelease := strings.TrimPrefix(latestReleaseTag, "v")
currentRelease := strings.TrimPrefix(version.Version, "v")
if latestRelease != currentRelease {
printMessageToUpgradeToAtmosLatestRelease(latestRelease)
}
}
aknysh marked this conversation as resolved.
Show resolved Hide resolved
}

// Check Atmos is version command
Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
u "github.com/cloudposse/atmos/pkg/utils"
)

// originalHelpFunc holds Cobra's original help function to avoid recursion.
var originalHelpFunc func(*cobra.Command, []string)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "atmos",
Expand Down Expand Up @@ -69,6 +72,14 @@ func Execute() error {
Flags: cc.Bold,
})

// Save the original help function to prevent infinite recursion when overriding it.
// This allows us to call the original help functionality within our custom help function.
originalHelpFunc = RootCmd.HelpFunc()
Listener430 marked this conversation as resolved.
Show resolved Hide resolved

// Override the help function with a custom one that adds an upgrade message after displaying help.
// This custom help function will call the original help function and then display the bordered message.
RootCmd.SetHelpFunc(customHelpMessageToUpgradeToAtmosLatestRelease)

// Check if the `help` flag is passed and print a styled Atmos logo to the terminal before printing the help
err := RootCmd.ParseFlags(os.Args)
if err != nil && errors.Is(err, pflag.ErrHelp) {
Expand Down