diff --git a/README.md b/README.md index b10c455..d97c799 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/cmd/gvs/main.go b/cmd/gvs/main.go index 8989a67..3e65e17 100644 --- a/cmd/gvs/main.go +++ b/cmd/gvs/main.go @@ -16,6 +16,7 @@ import ( "github.com/VassilisPallas/gvs/pkg/unzip" "github.com/VassilisPallas/gvs/version" "github.com/manifoldco/promptui" + "golang.org/x/mod/modfile" ) var ( @@ -23,6 +24,7 @@ var ( installLatest = false deleteUnused = false showAllVersions = false + fromModFile = false specificVersion = "" ) @@ -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() } @@ -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 { @@ -94,7 +137,6 @@ func main() { } err = versioner.Install(selectedVersion, runtime.GOOS, runtime.GOARCH) - if err != nil { log.PrintError(err.Error()) os.Exit(1) @@ -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 { diff --git a/go.mod b/go.mod index 2c22fb2..3b2a0ed 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index e661a97..84c022d 100644 --- a/go.sum +++ b/go.sum @@ -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=