Skip to content

Commit

Permalink
added automatic upgrade, additonal notice cta
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanisham committed May 27, 2024
1 parent c4a078c commit c91af16
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ⭐

<!-- https://star-history.com/#tristanisham/zvm&Timeline -->
Expand Down
26 changes: 25 additions & 1 deletion cli/meta/cta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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"))

}
1 change: 1 addition & 0 deletions cli/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 7 additions & 7 deletions cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 33 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`",
Expand Down Expand Up @@ -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",
},
},
Expand All @@ -127,7 +125,7 @@ var zvmApp = &opts.App{
versionArg := strings.TrimPrefix(ctx.Args().First(), "v")
return zvm.Use(versionArg)
}

},
},
{
Expand Down Expand Up @@ -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)
}

}

0 comments on commit c91af16

Please sign in to comment.