1
1
package main
2
2
3
3
import (
4
- "context"
4
+ "errors"
5
+ "flag"
5
6
"fmt"
6
- "io"
7
7
"os"
8
8
"os/exec"
9
9
10
10
"github.com/axetroy/nodapt/internal/command"
11
- "github.com/pkg/errors"
12
- "github.com/urfave/cli/v3"
13
11
)
14
12
15
13
var (
@@ -18,13 +16,28 @@ var (
18
16
date = "unknown"
19
17
)
20
18
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
+
21
34
func printHelp () {
22
35
fmt .Println (`nodapt - A virtual node environment for node.js, node version manager for projects.
23
36
24
37
USAGE:
25
38
nodapt [OPTIONS] <ARGS...>
26
39
nodapt [OPTIONS] run <ARGS...>
27
- nodapt [OPTIONS] use <CONSTRAINT> [ARGS...]
40
+ nodapt [OPTIONS] use <CONSTRAINT> [ARGS...>
28
41
nodapt [OPTIONS] rm <CONSTRAINT>
29
42
nodapt [OPTIONS] clean
30
43
nodapt [OPTIONS] ls
@@ -60,134 +73,92 @@ SOURCE CODE:
60
73
}
61
74
62
75
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" )
68
81
69
- cli .VersionFlag = & cli.BoolFlag {
70
- Name : "version" ,
71
- Aliases : []string {"v" },
72
- Usage : "Print version information" ,
73
- }
82
+ flag .Parse ()
74
83
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
165
86
166
- cli .VersionPrinter = func (c * cli.Command ) {
167
- fmt .Printf ("%s %s %s\n " , version , commit , date )
168
- }
87
+ args := flag .Args ()
169
88
170
- cli .HelpPrinter = func (w io.Writer , templ string , data any ) {
171
- printHelp ()
89
+ if len (args ) == 0 {
90
+ defaultBehaviorHandler (showHelp , showVersion , "" )
91
+ return
172
92
}
173
93
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
+ }
178
107
} 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 )
181
143
}
144
+ }
145
+ }
182
146
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
+ }
184
154
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 ())
191
160
}
192
161
}
162
+
163
+ os .Exit (1 )
193
164
}
0 commit comments