Skip to content

Commit f516904

Browse files
committed
refactor: remove github.com/pkg/errors"
"github.com/urfave/cli
1 parent c973efe commit f516904

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+94
-6922
lines changed

cmd/nodapt/main.go

Lines changed: 94 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package main
22

33
import (
4-
"context"
4+
"errors"
5+
"flag"
56
"fmt"
6-
"io"
77
"os"
88
"os/exec"
99

1010
"github.com/axetroy/nodapt/internal/command"
11-
"github.com/pkg/errors"
12-
"github.com/urfave/cli/v3"
1311
)
1412

1513
var (
@@ -18,13 +16,28 @@ var (
1816
date = "unknown"
1917
)
2018

19+
func defaultBehaviorHandler(helpFlag, versionFlag bool, commandName string) {
20+
if helpFlag {
21+
printHelp()
22+
return
23+
}
24+
25+
if versionFlag {
26+
fmt.Printf("%s %s %s\n", version, commit, date)
27+
return
28+
}
29+
30+
fmt.Printf("Unknown command: %s\n", commandName)
31+
printHelp()
32+
}
33+
2134
func printHelp() {
2235
fmt.Println(`nodapt - A virtual node environment for node.js, node version manager for projects.
2336
2437
USAGE:
2538
nodapt [OPTIONS] <ARGS...>
2639
nodapt [OPTIONS] run <ARGS...>
27-
nodapt [OPTIONS] use <CONSTRAINT> [ARGS...]
40+
nodapt [OPTIONS] use <CONSTRAINT> [ARGS...>
2841
nodapt [OPTIONS] rm <CONSTRAINT>
2942
nodapt [OPTIONS] clean
3043
nodapt [OPTIONS] ls
@@ -60,134 +73,92 @@ SOURCE CODE:
6073
}
6174

6275
func main() {
63-
cli.HelpFlag = &cli.BoolFlag{
64-
Name: "help",
65-
Aliases: []string{"h"},
66-
Usage: "Print help information",
67-
}
76+
// Define global flags
77+
helpLongFlag := flag.Bool("help", false, "Print help information")
78+
helpShortFlag := flag.Bool("h", false, "Print help information")
79+
versionLongFlag := flag.Bool("version", false, "Print version information")
80+
versionShortFlag := flag.Bool("v", false, "Print version information")
6881

69-
cli.VersionFlag = &cli.BoolFlag{
70-
Name: "version",
71-
Aliases: []string{"v"},
72-
Usage: "Print version information",
73-
}
82+
flag.Parse()
7483

75-
app := &cli.Command{
76-
Name: "nodapt",
77-
Usage: "A virtual node environment for node.js, node version manager for projects.",
78-
Version: version,
79-
Suggest: true,
80-
Commands: []*cli.Command{
81-
{
82-
Name: "run",
83-
Usage: `Automatically select node version to run commands`,
84-
Description: `Automatically select node version to run commands`,
85-
Arguments: cli.AnyArguments,
86-
ArgsUsage: `<COMMANDS...>`,
87-
Action: func(ctx context.Context, cmd *cli.Command) error {
88-
return command.Run(cmd.Args().Slice())
89-
},
90-
},
91-
{
92-
Name: "use",
93-
Usage: "Use the specified version of node to run the command",
94-
Description: "Use the specified version of node to run the command",
95-
Arguments: cli.AnyArguments,
96-
ArgsUsage: `<CONSTRAINT> [COMMANDS...]`,
97-
Action: func(ctx context.Context, cmd *cli.Command) error {
98-
args := cmd.Args().Slice()
99-
100-
length := len(args)
101-
102-
switch length {
103-
case 0:
104-
return command.Use(nil)
105-
case 1:
106-
constraint := args[0]
107-
108-
return command.Use(&constraint)
109-
default:
110-
constraint := args[0]
111-
commands := args[1:]
112-
113-
return command.RunWithConstraint(constraint, commands)
114-
}
115-
},
116-
},
117-
{
118-
Name: "remove",
119-
Usage: "Remove the specified version of node that installed by nodapt",
120-
Description: "Remove the specified version of node that installed by nodapt",
121-
Aliases: []string{"rm"},
122-
Arguments: cli.AnyArguments,
123-
ArgsUsage: `<CONSTRAINT...>`,
124-
Action: func(ctx context.Context, cmd *cli.Command) error {
125-
for _, constraint := range cmd.Args().Slice() {
126-
if err := command.Remove(constraint); err != nil {
127-
return errors.WithStack(err)
128-
}
129-
}
130-
131-
return nil
132-
},
133-
},
134-
{
135-
Name: "clean",
136-
Usage: "Remove all the node version that installed by nodapt",
137-
Description: "Remove all the node version that installed by nodapt",
138-
Action: func(ctx context.Context, cmd *cli.Command) error {
139-
return command.Clean()
140-
},
141-
},
142-
{
143-
Name: "list",
144-
Usage: "List all the installed node version",
145-
Description: "List all the installed node version",
146-
Aliases: []string{"ls"},
147-
Action: func(ctx context.Context, cmd *cli.Command) error {
148-
return command.List()
149-
},
150-
},
151-
{
152-
Name: "list-remote",
153-
Usage: "List all the available node version",
154-
Description: "List all the available node version",
155-
Aliases: []string{"ls-remote"},
156-
Action: func(ctx context.Context, cmd *cli.Command) error {
157-
return command.ListRemote()
158-
},
159-
},
160-
},
161-
Action: func(ctx context.Context, cmd *cli.Command) error {
162-
return command.Run(cmd.Args().Slice())
163-
},
164-
}
84+
showHelp := *helpLongFlag || *helpShortFlag
85+
showVersion := *versionLongFlag || *versionShortFlag
16586

166-
cli.VersionPrinter = func(c *cli.Command) {
167-
fmt.Printf("%s %s %s\n", version, commit, date)
168-
}
87+
args := flag.Args()
16988

170-
cli.HelpPrinter = func(w io.Writer, templ string, data any) {
171-
printHelp()
89+
if len(args) == 0 {
90+
defaultBehaviorHandler(showHelp, showVersion, "")
91+
return
17292
}
17393

174-
if err := app.Run(context.Background(), os.Args); err != nil {
175-
if os.Getenv("DEBUG") == "1" {
176-
fmt.Fprintf(os.Stderr, "%+v\n", err)
177-
fmt.Fprintf(os.Stderr, "current commit hash %s\n", commit)
94+
commandName := args[0]
95+
switch commandName {
96+
case "use":
97+
if len(args) < 2 {
98+
fmt.Println("Error: 'use' command requires a version constraint and optional commands.")
99+
return
100+
}
101+
constraint := args[1]
102+
commands := args[2:]
103+
if len(commands) == 0 {
104+
if err := command.Use(&constraint); err != nil {
105+
handleError(err)
106+
}
178107
} else {
179-
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
180-
fmt.Fprintln(os.Stderr, "Print debug information when set DEBUG=1")
108+
if err := command.RunWithConstraint(constraint, commands); err != nil {
109+
handleError(err)
110+
}
111+
}
112+
case "remove", "rm":
113+
if len(args) < 2 {
114+
fmt.Println("Error: 'remove' command requires at least one version constraint.")
115+
return
116+
}
117+
for _, constraint := range args[1:] {
118+
if err := command.Remove(constraint); err != nil {
119+
handleError(err)
120+
}
121+
}
122+
case "clean":
123+
if err := command.Clean(); err != nil {
124+
handleError(err)
125+
}
126+
case "list", "ls":
127+
if err := command.List(); err != nil {
128+
handleError(err)
129+
}
130+
case "list-remote", "ls-remote":
131+
if err := command.ListRemote(); err != nil {
132+
handleError(err)
133+
}
134+
case "help":
135+
printHelp()
136+
case "run":
137+
if err := command.Run(args[1:]); err != nil {
138+
handleError(err)
139+
}
140+
default:
141+
if err := command.Run(args[0:]); err != nil {
142+
handleError(err)
181143
}
144+
}
145+
}
182146

183-
unwrapError := errors.Unwrap(err)
147+
func handleError(err error) {
148+
if os.Getenv("DEBUG") == "1" {
149+
fmt.Fprintf(os.Stderr, "%+v\n", err)
150+
} else {
151+
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
152+
fmt.Fprintln(os.Stderr, "Print debug information when set DEBUG=1")
153+
}
184154

185-
if err, ok := err.(*exec.ExitError); ok {
186-
os.Exit(err.ExitCode())
187-
} else if err, ok := unwrapError.(*exec.ExitError); ok {
188-
os.Exit(err.ExitCode())
189-
} else {
190-
os.Exit(1)
155+
if exitErr, ok := err.(*exec.ExitError); ok {
156+
os.Exit(exitErr.ExitCode())
157+
} else if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
158+
if exitErr, ok := unwrappedErr.(*exec.ExitError); ok {
159+
os.Exit(exitErr.ExitCode())
191160
}
192161
}
162+
163+
os.Exit(1)
193164
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ require (
1010
github.com/pkg/errors v0.9.1
1111
github.com/stretchr/testify v1.10.0
1212
github.com/ulikunitz/xz v0.5.12
13-
github.com/urfave/cli/v3 v3.1.1
1413
golang.org/x/sys v0.32.0
1514
golang.org/x/term v0.31.0
1615
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ github.com/u-root/u-root v0.11.0 h1:6gCZLOeRyevw7gbTwMj3fKxnr9+yHFlgF3N7udUVNO8=
127127
github.com/u-root/u-root v0.11.0/go.mod h1:DBkDtiZyONk9hzVEdB/PWI9B4TxDkElWlVTHseglrZY=
128128
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
129129
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
130-
github.com/urfave/cli/v3 v3.1.1 h1:bNnl8pFI5dxPOjeONvFCDFoECLQsceDG4ejahs4Jtxk=
131-
github.com/urfave/cli/v3 v3.1.1/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
132130
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
133131
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
134132
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

vendor/github.com/urfave/cli/v3/.gitignore

Lines changed: 0 additions & 11 deletions
This file was deleted.

vendor/github.com/urfave/cli/v3/.golangci.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

vendor/github.com/urfave/cli/v3/CODE_OF_CONDUCT.md

Lines changed: 0 additions & 75 deletions
This file was deleted.

vendor/github.com/urfave/cli/v3/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)