Skip to content

Commit

Permalink
cmd/use: Validate version in dot file against repo
Browse files Browse the repository at this point in the history
When running "hvm use --useVersionInDotFile", validate the version
against the tags in the repository.
  • Loading branch information
jmooring committed Sep 24, 2023
1 parent eaf4190 commit c83cbcf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
5 changes: 1 addition & 4 deletions cmd/hvm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"slices"
"strings"

Expand Down Expand Up @@ -233,9 +232,7 @@ func getVersionFromDotFile(path string) (string, error) {
return "", fmt.Errorf("the %s file in the current directory is empty: %s", App.DotFileName, theFix)
}

re := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
match := re.MatchString(dotHvmContent)
if !match {
if !semver.IsValid(dotHvmContent) {
return "", fmt.Errorf("the %s file in the current directory has an invalid format: %s", App.DotFileName, theFix)
}

Expand Down
14 changes: 14 additions & 0 deletions cmd/hvm/testscripts/status/status_err_3.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
! exec hvm status
stderr 'Error: the version specified in the \.hvm file \(v\d+\.\d+\.\d+\) is not available in the repository: run "hvm use" to select a version, or "hvm disable" to remove the file\n'
-- .hvm --
v9.999.9
18 changes: 12 additions & 6 deletions cmd/hvm/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -93,6 +94,11 @@ func use(useVersionInDotFile bool) error {
fmt.Fprintln(os.Stderr, "Error: the current directory does not contain an "+App.DotFileName+" file")
os.Exit(1)
}
if !slices.Contains(repo.tags, version) {
theFix := fmt.Sprintf("run \"%[1]s use\" to select a version, or \"%[1]s disable\" to remove the file", App.Name)
fmt.Fprintf(os.Stderr, "Error: the version specified in the "+App.DotFileName+" file (%s) is not available in the repository: %s\n", version, theFix)
os.Exit(1)
}
asset.tag = version
} else {
msg := "Select a version to use in the current directory"
Expand Down Expand Up @@ -158,6 +164,12 @@ func newRepository() *repository {
owner: App.RepositoryOwner,
}

err := r.fetchTags()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

return &r
}

Expand Down Expand Up @@ -248,12 +260,6 @@ func (r *repository) fetchTags() error {

// selectTag prompts the user to select a tag from a list of recent tags.
func (r *repository) selectTag(a *asset, msg string) error {

err := r.fetchTags()
if err != nil {
return err
}

// List tags.
fmt.Println()

Expand Down

0 comments on commit c83cbcf

Please sign in to comment.