Skip to content

Commit aee5814

Browse files
authored
feat: enhance info command functionality (#502)
1 parent a9b0c0f commit aee5814

File tree

6 files changed

+497
-11
lines changed

6 files changed

+497
-11
lines changed

cmd/commands/info.go

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,29 @@ package commands
1818

1919
import (
2020
"fmt"
21+
"path/filepath"
22+
"strings"
23+
"text/template"
2124

2225
"github.com/pterm/pterm"
2326
"github.com/urfave/cli/v2"
2427
"github.com/version-fox/vfox/internal"
28+
"github.com/version-fox/vfox/internal/base"
2529
)
2630

2731
var Info = &cli.Command{
28-
Name: "info",
29-
Usage: "Show plugin info",
30-
Action: infoCmd,
31-
Category: CategoryPlugin,
32+
Name: "info",
33+
Usage: "Show plugin info or SDK path",
34+
ArgsUsage: "[<sdk> | <sdk>@<version>]",
35+
Action: infoCmd,
36+
Category: CategoryPlugin,
37+
Flags: []cli.Flag{
38+
&cli.StringFlag{
39+
Name: "format",
40+
Aliases: []string{"f"},
41+
Usage: "Format the output using the given Go template",
42+
},
43+
},
3244
}
3345

3446
func infoCmd(ctx *cli.Context) error {
@@ -38,23 +50,103 @@ func infoCmd(ctx *cli.Context) error {
3850
if args == "" {
3951
return cli.Exit("invalid arguments", 1)
4052
}
53+
54+
// Check if the argument is in the format <sdk>@<version>
55+
if strings.Contains(args, "@") {
56+
argArr := strings.Split(args, "@")
57+
if len(argArr) == 2 {
58+
name := strings.ToLower(argArr[0])
59+
version := base.Version(argArr[1])
60+
61+
// Check for empty SDK name or version
62+
if name != "" && string(version) != "" {
63+
sdk, err := manager.LookupSdk(name)
64+
if err != nil {
65+
if ctx.IsSet("format") {
66+
// For template output, we still need to output something
67+
data := struct {
68+
Name string
69+
Version string
70+
Path string
71+
}{
72+
Name: name,
73+
Version: string(version),
74+
Path: "notfound",
75+
}
76+
return executeTemplate(ctx, data)
77+
}
78+
fmt.Println("notfound")
79+
return nil
80+
}
81+
82+
path := ""
83+
if sdk.CheckExists(version) {
84+
path = filepath.Join(sdk.VersionPath(version), fmt.Sprintf("%s-%s", name, version))
85+
} else {
86+
path = "notfound"
87+
}
88+
89+
// Check if format flag is set
90+
formatValue := ctx.String("format")
91+
if formatValue != "" {
92+
data := struct {
93+
Name string
94+
Version string
95+
Path string
96+
}{
97+
Name: name,
98+
Version: string(version),
99+
Path: path,
100+
}
101+
return executeTemplate(ctx, data)
102+
}
103+
104+
fmt.Println(path)
105+
return nil
106+
}
107+
}
108+
}
109+
110+
// Show plugin info
41111
s, err := manager.LookupSdk(args)
42112
if err != nil {
43113
return fmt.Errorf("%s not supported, error: %w", args, err)
44114
}
45115
source := s.Plugin
46116

117+
// If format flag is set, prepare data for template
118+
if ctx.IsSet("format") {
119+
data := struct {
120+
Name string
121+
Version string
122+
Homepage string
123+
InstallPath string
124+
Description string
125+
}{
126+
Name: source.Name,
127+
Version: source.Version,
128+
Homepage: source.Homepage,
129+
InstallPath: s.InstallPath,
130+
Description: source.Description,
131+
}
132+
return executeTemplate(ctx, data)
133+
}
134+
47135
pterm.Println("Plugin Info:")
48136
pterm.Println("Name ", "->", pterm.LightBlue(source.Name))
49137
pterm.Println("Version ", "->", pterm.LightBlue(source.Version))
50138
pterm.Println("Homepage", "->", pterm.LightBlue(source.Homepage))
51139
pterm.Println("Desc ", "->")
52140
pterm.Println(pterm.LightBlue(source.Description))
53-
if len(source.LegacyFilenames) == 0 {
54-
pterm.Println("Legacy Files ->", pterm.LightRed("None"))
55-
} else {
56-
pterm.Println("Legacy Files ->", pterm.LightBlue(source.LegacyFilenames))
57-
}
58141
source.ShowNotes()
59142
return nil
60143
}
144+
145+
func executeTemplate(ctx *cli.Context, data interface{}) error {
146+
tmplStr := ctx.String("format")
147+
tmpl, err := template.New("format").Parse(tmplStr)
148+
if err != nil {
149+
return fmt.Errorf("error parsing template: %w", err)
150+
}
151+
return tmpl.Execute(ctx.App.Writer, data)
152+
}

0 commit comments

Comments
 (0)