From c91af16c654e6092627bcc9f1e2393cd3c6047ae Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Mon, 27 May 2024 11:15:22 -0400 Subject: [PATCH] added automatic upgrade, additonal notice cta --- README.md | 9 ++++++++- cli/meta/cta.go | 26 +++++++++++++++++++++++++- cli/settings.go | 1 + cli/upgrade.go | 14 +++++++------- main.go | 45 +++++++++++++++++++++++++++++++++------------ 5 files changed, 74 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9f9f6e8..4eda995 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ zvm clean Use `clean` to remove build artifacts (Good if you're on Windows). -### Version Map Source +## Set Version Map Source ```sh vmu "https://validurl.local/vmu.json" # Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. @@ -252,6 +252,13 @@ Enable or disable colored ZVM output. No value toggles colors. --color # Toggle ANSI color printing on or off for ZVM's output, i.e. --color=true ``` +## Environment Variables + +- `ZVM_DEBG` enables DEBUG logging for your executable. This is meant for + contributors and developers. +- `ZVM_SET_CU` Toggle the automatic upgrade checker. If you want to reenable the + checker, just `uset ZVM_SET_CU`. + ## Please Consider Giving the Repo a Star ⭐ diff --git a/cli/meta/cta.go b/cli/meta/cta.go index 8b47b61..861e702 100644 --- a/cli/meta/cta.go +++ b/cli/meta/cta.go @@ -11,7 +11,7 @@ import ( "github.com/charmbracelet/log" ) -// Prints a nice CTA and exits with an error +// CtaFatal prints an aesthetic CTA and exits with an error. func CtaFatal(err error) { var style = lipgloss.NewStyle(). @@ -37,3 +37,27 @@ func CtaFatal(err error) { fmt.Printf("Otherwise, please report this error as a GitHub issue.\n%s\n", blueLink.Render("https://github.com/tristanisham/zvm/issues/\n")) os.Exit(1) } + +// CtaUpgradeAvailable prints an aesthetic notice. +func CtaUpgradeAvailable(tag string) { + var style = lipgloss.NewStyle(). + Bold(true). + Foreground(lipgloss.Color("#FAFAFA")). + Background(lipgloss.Color("#6FA8DC")). + Width(10). + MarginTop(1). + MarginBottom(1). + Align(lipgloss.Center) + fmt.Println(style.Render("Notice")) + + blueLink := lipgloss.NewStyle(). + Foreground(lipgloss.Color("#0000EE")). + Bold(true). + Underline(true) + + yellowText := lipgloss.NewStyle(). + Foreground(lipgloss.Color("#fee12b")) + + fmt.Printf("\nZVM %s is available. You are currently on %s.\n\nRun %s or download the latest release at\n%s\n\n", blueLink.Render(tag), blueLink.Render(VERSION), yellowText.Render("zvm upgrade"), blueLink.Render("https://github.com/tristanisham/zvm/releases/latest")) + +} diff --git a/cli/settings.go b/cli/settings.go index 372fee1..e6847f9 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -19,6 +19,7 @@ type Settings struct { path string UseColor bool `json:"useColor"` VersionMapUrl string `json:"versionMapUrl,omitempty"` + // CheckForUpgrade bool `json:"checkForUpgrade"` } func (s *Settings) ToggleColor() { diff --git a/cli/upgrade.go b/cli/upgrade.go index a25ea84..577a108 100644 --- a/cli/upgrade.go +++ b/cli/upgrade.go @@ -38,7 +38,7 @@ func (z *ZVM) Upgrade() error { } }() - upgradable, tagName, err := CanIUpgrade() + tagName, upgradable, err := CanIUpgrade() if err != nil { return errors.Join(ErrFailedUpgrade, err) } @@ -152,7 +152,7 @@ func (z *ZVM) Upgrade() error { return nil } -// Replaces one file with another on Windows. +// Replaces one file with another on Windows. func replaceExe(from, to string) error { if runtime.GOOS == "windows" { if err := os.Rename(to, fmt.Sprintf("%s.old", to)); err != nil { @@ -177,7 +177,6 @@ func replaceExe(from, to string) error { } defer to_io.Close() - if _, err := io.Copy(to_io, from_io); err != nil { return nil } @@ -291,17 +290,18 @@ func isSymlink(path string) (bool, error) { return fileInfo.Mode()&os.ModeSymlink != 0, nil } -func CanIUpgrade() (bool, string, error) { +func CanIUpgrade() (string, bool, error) { release, err := getLatestGitHubRelease("tristanisham", "zvm") if err != nil { - return false, "", err + return "", false, err } if semver.Compare(meta.VERSION, release.TagName) == -1 { - return true, release.TagName, nil + return release.TagName, true, nil } - return false, release.TagName, nil + return release.TagName, false, nil + } // func getGitHubReleases(owner, repo string) ([]GithubRelease, error) { diff --git a/main.go b/main.go index b9c1c03..b06ae8f 100644 --- a/main.go +++ b/main.go @@ -18,20 +18,18 @@ import ( opts "github.com/urfave/cli/v2" "github.com/charmbracelet/log" - ) - var zvm cli.ZVM var zvmApp = &opts.App{ - Name: "ZVM", - Usage: "Zig Version Manager", + Name: "ZVM", + Usage: "Zig Version Manager", Description: "zvm lets you easily install, upgrade, and switch between different versions of Zig.", - HelpName: "zvm", - Version: meta.VerCopy, - Copyright: "Copyright © 2022 Tristan Isham", - Suggest: true, + HelpName: "zvm", + Version: meta.VerCopy, + Copyright: "Copyright © 2022 Tristan Isham", + Suggest: true, Before: func(ctx *opts.Context) error { zvm = *cli.Initialize() return nil @@ -65,9 +63,9 @@ var zvmApp = &opts.App{ Aliases: []string{"i"}, Flags: []opts.Flag{ &opts.BoolFlag{ - Name: "zls", + Name: "zls", // Aliases: []string{"z"}, - Usage: "install ZLS", + Usage: "install ZLS", }, }, Description: "To install the latest version, use `master`", @@ -116,7 +114,7 @@ var zvmApp = &opts.App{ Args: true, Flags: []opts.Flag{ &opts.BoolFlag{ - Name: "sync", + Name: "sync", Usage: "sync your current version of Zig with the repository", }, }, @@ -127,7 +125,7 @@ var zvmApp = &opts.App{ versionArg := strings.TrimPrefix(ctx.Args().First(), "v") return zvm.Use(versionArg) } - + }, }, { @@ -211,8 +209,31 @@ func main() { log.SetLevel(log.DebugLevel) } + _, checkUpgradeDisabled := os.LookupEnv("ZVM_SET_CU") + log.Debug("Automatic Upgrade Checker", "disabled", checkUpgradeDisabled) + + // Upgrade + upSig := make(chan string, 1) + + if !checkUpgradeDisabled { + go func(out chan<- string) { + if tag, ok, _ := cli.CanIUpgrade(); ok { + out <- tag + } else { + out <- "" + } + }(upSig) + } else { + upSig <- "" + } + // run and report errors if err := zvmApp.Run(os.Args); err != nil { meta.CtaFatal(err) } + + if tag := <-upSig; tag != "" { + meta.CtaUpgradeAvailable(tag) + } + }