Skip to content

Commit

Permalink
Fix use cmd when auto install is disabled (#190)
Browse files Browse the repository at this point in the history
* tenv error no longer trigger cobra help
* use cmd write when no locally compatible
* improve auto install help message

Signed-off-by: Denis Vaumoron <[email protected]>
  • Loading branch information
dvaumoron authored Jun 26, 2024
1 parent 7a46ebb commit afe6698
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 41 deletions.
68 changes: 44 additions & 24 deletions cmd/tenv/subcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ The default constraint is added while using latest-allowed, min-required or cust
Short: loghelper.Concat("Set a default constraint expression for ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.MaximumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
Run: func(_ *cobra.Command, args []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

if len(args) == 0 || args[0] == "" {
return versionManager.ResetConstraint()
if err := versionManager.ResetConstraint(); err != nil {
loghelper.StdDisplay(err.Error())
}

return
}

return versionManager.SetConstraint(args[0])
if err := versionManager.SetConstraint(args[0]); err != nil {
loghelper.StdDisplay(err.Error())
}
},
}

Expand All @@ -80,17 +86,15 @@ func newDetectCmd(conf *config.Config, versionManager versionmanager.VersionMana
Short: loghelper.Concat("Display ", versionManager.FolderName, " current version."),
Long: descBuilder.String(),
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
Run: func(_ *cobra.Command, _ []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

detectedVersion, err := versionManager.Detect(false)
if err != nil {
return err
loghelper.StdDisplay(err.Error())
}
loghelper.StdDisplay(loghelper.Concat(versionManager.FolderName, " ", detectedVersion, " will be run from this directory."))

return nil
},
}

Expand Down Expand Up @@ -130,20 +134,28 @@ If a parameter is passed, available options:
Short: loghelper.Concat("Install a specific version of ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.MaximumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
Run: func(_ *cobra.Command, args []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

if len(args) == 0 {
version, err := versionManager.Resolve(semantic.LatestKey)
if err != nil {
return err
loghelper.StdDisplay(err.Error())

return
}

if err = versionManager.Install(version); err != nil {
loghelper.StdDisplay(err.Error())
}

return versionManager.Install(version)
return
}

return versionManager.Install(args[0])
if err := versionManager.Install(args[0]); err != nil {
loghelper.StdDisplay(err.Error())
}
},
}

Expand All @@ -168,13 +180,15 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage
Short: loghelper.Concat("List installed ", versionManager.FolderName, " versions."),
Long: descBuilder.String(),
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
Run: func(_ *cobra.Command, _ []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

versions, err := versionManager.ListLocal(reverseOrder)
if err != nil {
return err
loghelper.StdDisplay(err.Error())

return
}

filePath := versionManager.RootVersionFilePath()
Expand All @@ -194,8 +208,6 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage
if conf.DisplayVerbose {
loghelper.StdDisplay(loghelper.Concat("found ", strconv.Itoa(len(versions)), " ", versionManager.FolderName, " version(s) managed by tenv."))
}

return nil
},
}

Expand All @@ -221,13 +233,15 @@ func newListRemoteCmd(conf *config.Config, versionManager versionmanager.Version
Short: loghelper.Concat("List installable ", versionManager.FolderName, " versions."),
Long: descBuilder.String(),
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
Run: func(_ *cobra.Command, _ []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

versions, err := versionManager.ListRemote(reverseOrder)
if err != nil {
return err
loghelper.StdDisplay(err.Error())

return
}

countSkipped := 0
Expand All @@ -252,7 +266,7 @@ func newListRemoteCmd(conf *config.Config, versionManager versionmanager.Version
}
}

return err
return
},
}

Expand All @@ -278,11 +292,13 @@ func newResetCmd(conf *config.Config, versionManager versionmanager.VersionManag
Short: loghelper.Concat("Reset used version of ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
Run: func(_ *cobra.Command, _ []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

return versionManager.ResetVersion()
if err := versionManager.ResetVersion(); err != nil {
loghelper.StdDisplay(err.Error())
}
},
}

Expand All @@ -301,11 +317,13 @@ func newUninstallCmd(conf *config.Config, versionManager versionmanager.VersionM
Short: loghelper.Concat("Uninstall a specific version of ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
Run: func(_ *cobra.Command, args []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

return versionManager.Uninstall(args[0])
if err := versionManager.Uninstall(args[0]); err != nil {
loghelper.StdDisplay(err.Error())
}
},
}

Expand Down Expand Up @@ -337,11 +355,13 @@ Available parameter options:
Short: loghelper.Concat("Switch the default ", versionManager.FolderName, " version to use."),
Long: descBuilder.String(),
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
Run: func(_ *cobra.Command, args []string) {
conf.InitDisplayer(false)
addDeprecationMsg(conf, params)

return versionManager.Use(args[0], workingDir)
if err := versionManager.Use(args[0], workingDir); err != nil {
loghelper.StdDisplay(err.Error())
}
},
}

Expand Down
26 changes: 13 additions & 13 deletions cmd/tenv/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/config/cmdconst"
"github.com/tofuutils/tenv/v2/pkg/loghelper"
"github.com/tofuutils/tenv/v2/versionmanager"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
terragruntparser "github.com/tofuutils/tenv/v2/versionmanager/semantic/parser/terragrunt"

"github.com/spf13/cobra"
)

const (
Expand Down Expand Up @@ -62,7 +62,7 @@ type subCmdParams struct {
func main() {
conf, err := config.InitConfigFromEnv()
if err != nil {
fmt.Println("Configuration error :", err) //nolint
loghelper.StdDisplay(loghelper.Concat("Configuration error : ", err.Error()))
os.Exit(1)
}

Expand All @@ -77,7 +77,7 @@ func main() {
manageHiddenCallCmd(&conf, builders, gruntParser) // proxy call use os.Exit when called

if err = initRootCmd(&conf, builders, gruntParser).Execute(); err != nil {
fmt.Println(err) //nolint
loghelper.StdDisplay(err.Error())
os.Exit(1)
}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func newVersionCmd() *cobra.Command {
Long: rootVersionHelp,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(cmdconst.TenvName, versionName, version) //nolint
loghelper.StdDisplay(loghelper.Concat(cmdconst.TenvName, " ", versionName, " ", version))
},
}
}
Expand All @@ -192,10 +192,12 @@ func newUpdatePathCmd(gha bool) *cobra.Command {
Short: updatePathHelp,
Long: updatePathHelp,
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
Run: func(_ *cobra.Command, _ []string) {
execPath, err := os.Executable()
if err != nil {
return nil
loghelper.StdDisplay(err.Error())

return
}

execDirPath := filepath.Dir(execPath)
Expand All @@ -204,13 +206,13 @@ func newUpdatePathCmd(gha bool) *cobra.Command {
if pathfilePath != "" {
pathfile, err := os.OpenFile(pathfilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
return
}
defer pathfile.Close()

_, err = pathfile.Write(append([]byte(execDirPath), '\n'))
if err != nil {
return err
return
}
}
}
Expand All @@ -219,9 +221,7 @@ func newUpdatePathCmd(gha bool) *cobra.Command {
pathBuilder.WriteString(execDirPath)
pathBuilder.WriteRune(os.PathListSeparator)
pathBuilder.WriteString(os.Getenv(pathEnvName))
fmt.Println(pathBuilder.String()) //nolint

return nil
loghelper.StdDisplay(pathBuilder.String())
},
}
}
Expand Down
12 changes: 8 additions & 4 deletions versionmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (m VersionManager) Evaluate(requestedVersion string, proxyCall bool) (strin
}

if !installed {
return "", m.autoInstallDisabledMsg(cleanedVersion)
return cleanedVersion, m.autoInstallDisabledMsg(cleanedVersion)
}
m.conf.Displayer.Flush(proxyCall)

Expand Down Expand Up @@ -314,7 +314,11 @@ func (m VersionManager) Uninstall(requestedVersion string) error {
func (m VersionManager) Use(requestedVersion string, workingDir bool) error {
detectedVersion, err := m.Evaluate(requestedVersion, false)
if err != nil {
return err
if err != errNoCompatibleLocally {
return err
}

m.conf.Displayer.Display(err.Error())
}

targetFilePath := m.VersionFiles[0].Name
Expand All @@ -328,7 +332,7 @@ func (m VersionManager) Use(requestedVersion string, workingDir bool) error {
func (m VersionManager) autoInstallDisabledMsg(version string) error {
cmdName := strings.ToLower(m.FolderName)
m.conf.Displayer.Flush(false) // Always normal display when installation is missing
m.conf.Displayer.Display(loghelper.Concat("Auto-install is disabled. To install ", version, " version you can set environment variable TENV_AUTO_INSTALL=true, or install it via following command: 'tenv ", cmdName, " install ", version, "'"))
m.conf.Displayer.Display(loghelper.Concat("Auto-install is disabled. To install ", m.FolderName, " version ", version, ", you can set environment variable TENV_AUTO_INSTALL=true, or install it via any of the following command: 'tenv ", cmdName, " install', 'tenv ", cmdName, " install ", version, "'"))

return errNoCompatibleLocally
}
Expand Down Expand Up @@ -405,7 +409,7 @@ func (m VersionManager) searchInstallRemote(predicateInfo types.PredicateInfo, n
if predicateInfo.Predicate(version) {
m.conf.Displayer.Display("Found compatible version remotely : " + version)
if noInstall {
return "", m.autoInstallDisabledMsg(version)
return version, m.autoInstallDisabledMsg(version)
}

return version, m.installSpecificVersion(version, proxyCall)
Expand Down

0 comments on commit afe6698

Please sign in to comment.