forked from version-fox/vfox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
105 lines (94 loc) · 2.53 KB
/
cmd.go
File metadata and controls
105 lines (94 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2025 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"github.com/urfave/cli/v2"
"github.com/version-fox/vfox/cmd/commands"
"github.com/version-fox/vfox/internal"
"github.com/version-fox/vfox/internal/logger"
)
func Execute(args []string) {
newCmd().Execute(args)
}
type cmd struct {
app *cli.App
version string
}
func (c *cmd) Execute(args []string) {
if err := c.app.Run(args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func newCmd() *cmd {
version := internal.RuntimeVersion
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v", "V"},
Usage: "print version",
Action: func(ctx *cli.Context, b bool) error {
println(version)
return nil
},
}
app := &cli.App{}
app.EnableBashCompletion = true
app.Name = "vfox"
app.Usage = "vfox is a tool for runtime version management."
app.UsageText = "vfox [command] [command options]"
app.Copyright = "Copyright 2025 Han Li. All rights reserved."
app.Version = version
app.Description = "vfox is a cross-platform version manager, extendable via plugins. It allows you to quickly install and switch between different environment you need via the command line."
app.Suggest = true
app.BashComplete = func(ctx *cli.Context) {
for _, command := range ctx.App.Commands {
_, _ = fmt.Fprintln(ctx.App.Writer, command.Name)
}
}
debugFlags := &cli.BoolFlag{
Name: "debug",
Usage: "show debug information",
Action: func(ctx *cli.Context, b bool) error {
logger.SetLevel(logger.DebugLevel)
return nil
},
}
app.Flags = []cli.Flag{
debugFlags,
}
app.Commands = []*cli.Command{
commands.Info,
commands.Install,
commands.Current,
commands.Use,
commands.List,
commands.Uninstall,
commands.Available,
commands.Search,
commands.Update,
commands.Upgrade,
commands.Remove,
commands.Add,
commands.Activate,
commands.Env,
commands.Config,
commands.Cd,
commands.Path,
}
return &cmd{app: app, version: version}
}