Skip to content

Commit

Permalink
Avoid duplicate error message display (#256)
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Vaumoron <dvaumoron@gmail.com>
  • Loading branch information
dvaumoron authored Sep 28, 2024
1 parent 1e081be commit 63f3e3e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 87 deletions.
91 changes: 28 additions & 63 deletions cmd/tenv/subcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,19 @@ Without expression reset the default constraint.
The default constraint is added while using latest-allowed, min-required or custom constraint.`)

constraintCmd := &cobra.Command{
Use: "constraint [expression]",
Short: loghelper.Concat("Set a default constraint expression for ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
Use: "constraint [expression]",
Short: loghelper.Concat("Set a default constraint expression for ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
RunE: func(_ *cobra.Command, args []string) error {
conf.InitDisplayer(false)

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

return
return versionManager.ResetConstraint()
}

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

Expand All @@ -81,24 +76,26 @@ func newDetectCmd(conf *config.Config, versionManager versionmanager.VersionMana
forceInstall, forceNoInstall := false, false

detectCmd := &cobra.Command{
Use: "detect",
Short: loghelper.Concat("Display ", versionManager.FolderName, " current version."),
Long: descBuilder.String(),
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
Use: "detect",
Short: loghelper.Concat("Display ", versionManager.FolderName, " current version."),
Long: descBuilder.String(),
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(_ *cobra.Command, _ []string) error {
conf.InitDisplayer(false)
conf.InitInstall(forceInstall, forceNoInstall)

ctx := context.Background()
detectedVersion, err := versionManager.Detect(ctx, false)
detectedVersion, err := versionManager.Detect(context.Background(), false)
if err != nil {
loghelper.StdDisplay(err.Error())

if err != versionmanager.ErrNoCompatibleLocally {
return
if err == versionmanager.ErrNoCompatibleLocally {
loghelper.StdDisplay(err.Error())
} else {
return err
}
}
loghelper.StdDisplay(loghelper.Concat(versionManager.FolderName, " ", detectedVersion, " will be run from this directory."))

return nil
},
}

Expand Down Expand Up @@ -145,21 +142,13 @@ If a parameter is passed, available options:
if len(args) == 0 {
version, err := versionManager.Resolve(semantic.LatestKey)
if err != nil {
loghelper.StdDisplay(err.Error())
return err
}

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

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

Expand Down Expand Up @@ -189,8 +178,6 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage

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

return err
}

Expand Down Expand Up @@ -252,11 +239,8 @@ func newListRemoteCmd(conf *config.Config, versionManager versionmanager.Version
RunE: func(_ *cobra.Command, _ []string) error {
conf.InitDisplayer(false)

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

return err
}

Expand Down Expand Up @@ -311,13 +295,7 @@ func newResetCmd(conf *config.Config, versionManager versionmanager.VersionManag
RunE: func(_ *cobra.Command, _ []string) error {
conf.InitDisplayer(false)

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

return err
}

return nil
return versionManager.ResetVersion()
},
}

Expand Down Expand Up @@ -349,18 +327,11 @@ If a parameter is passed, available parameter options:
RunE: func(_ *cobra.Command, args []string) error {
conf.InitDisplayer(false)

var err error
if len(args) == 0 {
err = uninstallUI(versionManager)
return uninstallUI(versionManager)
} else {
err = versionManager.Uninstall(args[0])
}

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

Expand Down Expand Up @@ -396,13 +367,7 @@ Available parameter options:
conf.InitDisplayer(false)
conf.InitInstall(forceInstall, forceNoInstall)

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

return nil
return versionManager.Use(context.Background(), args[0], workingDir)
},
}

Expand Down
31 changes: 16 additions & 15 deletions cmd/tenv/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func main() {
manageHiddenCallCmd(&conf, hclParser) // proxy call use os.Exit when called

if err = initRootCmd(&conf, hclParser).Execute(); err != nil {
loghelper.StdDisplay(err.Error())
os.Exit(1)
}
}
Expand Down Expand Up @@ -183,10 +182,11 @@ func manageHiddenCallCmd(conf *config.Config, hclParser *hclparse.Parser) {

func newVersionCmd() *cobra.Command {
return &cobra.Command{
Use: versionName,
Short: rootVersionHelp,
Long: rootVersionHelp,
Args: cobra.NoArgs,
Use: versionName,
Short: rootVersionHelp,
Long: rootVersionHelp,
Args: cobra.NoArgs,
SilenceUsage: true,
Run: func(_ *cobra.Command, _ []string) {
loghelper.StdDisplay(loghelper.Concat(cmdconst.TenvName, " ", versionName, " ", version))
},
Expand All @@ -195,16 +195,15 @@ func newVersionCmd() *cobra.Command {

func newUpdatePathCmd(gha bool) *cobra.Command {
return &cobra.Command{
Use: "update-path",
Short: updatePathHelp,
Long: updatePathHelp,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
Use: "update-path",
Short: updatePathHelp,
Long: updatePathHelp,
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(_ *cobra.Command, _ []string) error {
execPath, err := os.Executable()
if err != nil {
loghelper.StdDisplay(err.Error())

return
return err
}

execDirPath := filepath.Dir(execPath)
Expand All @@ -213,13 +212,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
return err
}
defer pathfile.Close()

_, err = pathfile.Write(append([]byte(execDirPath), '\n'))
if err != nil {
return
return err
}
}
}
Expand All @@ -229,6 +228,8 @@ func newUpdatePathCmd(gha bool) *cobra.Command {
pathBuilder.WriteRune(os.PathListSeparator)
pathBuilder.WriteString(os.Getenv(pathEnvName))
loghelper.StdDisplay(pathBuilder.String())

return nil
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/tenv/textui.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ func manageUI(ctx context.Context, versionManager versionmanager.VersionManager)
slices.SortFunc(toInstall, semantic.CmpVersion)
slices.SortFunc(toUninstall, semantic.CmpVersion)

err = versionManager.UninstallMultiple(toUninstall)
if err != nil {
return nil
if err = versionManager.UninstallMultiple(toUninstall); err != nil {
return err
}

return versionManager.InstallMultiple(ctx, toInstall)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/ansi v0.3.2 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/cloudflare/circl v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand Down Expand Up @@ -55,6 +55,6 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/tools v0.25.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqK
github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=
github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.3.2 h1:wsEwgAN+C9U06l9dCVMX0/L3x7ptvY1qmjMwyfE6USY=
github.com/charmbracelet/x/ansi v0.3.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
Expand Down Expand Up @@ -172,8 +172,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down

0 comments on commit 63f3e3e

Please sign in to comment.