diff --git a/cmd/tenv/subcmd.go b/cmd/tenv/subcmd.go index a613e6cf..0997a291 100644 --- a/cmd/tenv/subcmd.go +++ b/cmd/tenv/subcmd.go @@ -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()) + } }, } @@ -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 }, } @@ -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()) + } }, } @@ -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() @@ -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 }, } @@ -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 @@ -252,7 +266,7 @@ func newListRemoteCmd(conf *config.Config, versionManager versionmanager.Version } } - return err + return }, } @@ -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()) + } }, } @@ -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()) + } }, } @@ -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()) + } }, } diff --git a/cmd/tenv/tenv.go b/cmd/tenv/tenv.go index a623477e..c396fe8f 100644 --- a/cmd/tenv/tenv.go +++ b/cmd/tenv/tenv.go @@ -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 ( @@ -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) } @@ -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) } } @@ -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)) }, } } @@ -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) @@ -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 } } } @@ -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()) }, } } diff --git a/versionmanager/manager.go b/versionmanager/manager.go index f3c02d7d..32580bac 100644 --- a/versionmanager/manager.go +++ b/versionmanager/manager.go @@ -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) @@ -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 @@ -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 } @@ -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)