Skip to content

Commit

Permalink
Add custom help message and upgrade notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Listener430 committed Nov 27, 2024
1 parent 4b30800 commit d331347
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
62 changes: 55 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 All @@ -27,6 +29,8 @@ type ValidateConfig struct {

type AtmosValidateOption func(*ValidateConfig)

var originalHelpFunc func(*cobra.Command, []string)

func WithStackValidation(check bool) AtmosValidateOption {
return func(cfg *ValidateConfig) {
cfg.CheckStack = check
Expand Down Expand Up @@ -423,17 +427,61 @@ func printMessageForMissingAtmosConfig(cliConfig schema.CliConfiguration) {

// printMessageToUpgradeToAtmosLatestRelease prints info on how to upgrade Atmos to the latest version
func printMessageToUpgradeToAtmosLatestRelease(latestVersion string) {
c1 := color.New(color.FgCyan)
c2 := color.New(color.FgGreen)

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")
message := fmt.Sprintf("Atmos update available: %s. Enjoy the latest updates and improvements!", c2.Sprint(latestVersion))
links := []string{
fmt.Sprintf("%s: https://github.com/cloudposse/atmos/releases", c2.Sprint("Atmos Releases")),
fmt.Sprintf("%s: https://atmos.tools/install", c2.Sprint("Install Atmos")),
}
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

// 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 := len(stripANSI(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, "")
}

// 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)
}
}
}

// Check Atmos is version command
Expand Down
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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()

// 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

0 comments on commit d331347

Please sign in to comment.