Skip to content

Commit

Permalink
'gowrap project version' implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
xabierlaiseca committed May 20, 2020
1 parent b91e5e7 commit 09759a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
30 changes: 29 additions & 1 deletion cmd/gowrap/commands/project.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package commands

import (
"fmt"
"strings"

"github.com/xabierlaiseca/gowrap/pkg/versions"

"github.com/alecthomas/kingpin"
"github.com/xabierlaiseca/gowrap/pkg/project"
"github.com/xabierlaiseca/gowrap/pkg/semver"
"github.com/xabierlaiseca/gowrap/pkg/util/customerrors"
)

func newProjectCommand(app *kingpin.Application, wd string) {
func newProjectCommand(app *kingpin.Application, gowrapHome, wd string) {
cmd := app.Command("project", "Project operations")
newProjectPinCommand(cmd, wd)
newProjectUnpinCommand(cmd, wd)
newProjectVersionCommand(cmd, gowrapHome, wd)
}

func newProjectPinCommand(parent *kingpin.CmdClause, wd string) {
Expand Down Expand Up @@ -40,3 +44,27 @@ func newProjectUnpinCommand(parent *kingpin.CmdClause, wd string) {
return project.UnpinVersion(wd)
})
}

func newProjectVersionCommand(parent *kingpin.CmdClause, gowrapHome, wd string) {
parent.Command("version", "Show the Go version used by the project").
Action(func(*kingpin.ParseContext) error {
projectVersion, err := project.DetectVersion(gowrapHome, wd)
if err != nil {
return err
}

var additionalMessage string
installedVersion, err := versions.FindLatestInstalledForPrefix(gowrapHome, projectVersion)
switch {
case customerrors.IsNotFound(err):
additionalMessage = " (no compatible installed version found)"
case err != nil:
return err
case projectVersion != installedVersion:
additionalMessage = fmt.Sprintf(" (specific version to use: %s)", installedVersion)
}

fmt.Printf("%s%s\n", projectVersion, additionalMessage)
return nil
})
}
2 changes: 1 addition & 1 deletion cmd/gowrap/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func RunCli(gowrapHome, wd string, args []string) error {
newConfigureCommand(app, gowrapHome)
newInstallCommand(app, gowrapHome)
newListCommand(app, gowrapHome)
newProjectCommand(app, wd)
newProjectCommand(app, gowrapHome, wd)
newUninstallCommand(app, gowrapHome)
newVersionsFileCommand(app)

Expand Down

0 comments on commit 09759a5

Please sign in to comment.