Skip to content

Commit

Permalink
feat: adds flag to install version from a go.mod file (#8)
Browse files Browse the repository at this point in the history
* feat: install version from go.mod file

* docs: update readme
  • Loading branch information
VassilisPallas authored Oct 28, 2023
1 parent 27d93e9 commit c446b52
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [See all versions including release candidates (rc)](#see-all-versions-including-release-candidates-rc)
- [Install latest version](#install-latest-version)
- [Install specific version](#install-specific-version)
- [Install from mod file](#install-from-mod-file)
- [Delete unused versions](#delete-unused-versions)
- [Refresh version list](#refresh-version-list)
- [Help](#help)
Expand Down Expand Up @@ -142,7 +143,7 @@ Use the arrow keys to navigate: ↓ ↑ → ←

### Install latest version

In order to install the latest stable version, use the `--install-latest`.
To install the latest stable version, use the `--install-latest`.

```sh
$ gvs --install-latest
Expand All @@ -155,7 +156,7 @@ Installing version...

### Install specific version

In order to install a specific version without using the dropdown, use the `--install-version=value`.
To install a specific version without using the dropdown, use the `--install-version=value`.

```sh
$ gvs --install-version=1.21.3
Expand All @@ -172,6 +173,18 @@ If the `Patch` version is not specified (`--install-version=1.21`), the latest `

You can also pass Release Candidates, like `1.21rc2`.

### Install from mod file

You can also install a version that is specified in a go.mod file. You can use the flag `--from-mod`. This will look for any `go.mod` file under the same path `gvs` was executed on the terminal.

```sh
$ gvs --from-mod
Downloading...
Compare Checksums...
Unzipping...
Installing version...
1.21.3 version is installed!
```

### Delete unused versions

Expand Down
46 changes: 44 additions & 2 deletions cmd/gvs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"github.com/VassilisPallas/gvs/pkg/unzip"
"github.com/VassilisPallas/gvs/version"
"github.com/manifoldco/promptui"
"golang.org/x/mod/modfile"
)

var (
refreshVersions = false
installLatest = false
deleteUnused = false
showAllVersions = false
fromModFile = false
specificVersion = ""
)

Expand All @@ -33,6 +35,7 @@ func parseFlags() {
set.FlagBool(&deleteUnused, "delete-unused", false, "Delete all unused versions that were installed before.")
set.FlagBool(&refreshVersions, "refresh-versions", false, "Fetch again go versions in case the cached ones are stale.")
set.FlagStr(&specificVersion, "install-version", "", "Pass the version you want to install instead of selecting from the dropdown. If you do not specify the minor or the patch version, the latest one will be selected.")
set.FlagBool(&fromModFile, "from-mod", false, "Install the version that will be found on the go.mod file. The go.mod file should be on the same path you run gvs. If the version in the go.mod file do not specify the minor or the patch version, the latest one will be selected.")

set.Parse()
}
Expand Down Expand Up @@ -77,7 +80,47 @@ func main() {
}

switch {
case fromModFile:
log.Info("install version from go.mod file option selected")

buf, err := fs.ReadFile("./go.mod")
if err != nil {
log.PrintError(err.Error())
os.Exit(1)
return
}

f, err := modfile.Parse("go.mod", buf, nil)
if err != nil {
log.PrintError(err.Error())
os.Exit(1)
return
}

semver := &version.Semver{}
err = version.ParseSemver(f.Go.Version, semver)
if err != nil {
log.PrintError(err.Error())
os.Exit(1)
return
}

selectedVersion := versioner.FindVersionBasedOnSemverName(versions, semver)
if selectedVersion == nil {
log.PrintError("%s is not a valid version.", semver.GetVersion())
os.Exit(1)
return
}

err = versioner.Install(selectedVersion, runtime.GOOS, runtime.GOARCH)
if err != nil {
log.PrintError(err.Error())
os.Exit(1)
return
}
case specificVersion != "":
log.Info("install specific version option selected")

semver := &version.Semver{}
err := version.ParseSemver(specificVersion, semver)
if err != nil {
Expand All @@ -94,7 +137,6 @@ func main() {
}

err = versioner.Install(selectedVersion, runtime.GOOS, runtime.GOARCH)

if err != nil {
log.PrintError(err.Error())
os.Exit(1)
Expand All @@ -116,7 +158,7 @@ func main() {
log.PrintMessage("Nothing to delete")
}
case installLatest:
log.Info("installLatest option selected")
log.Info("install latest option selected")

selectedIndex := versioner.GetLatestVersion(versions)
if selectedIndex == -1 {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
golang.org/x/mod v0.13.0
golang.org/x/sys v0.6.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
Expand Down

0 comments on commit c446b52

Please sign in to comment.