Skip to content

Commit 09b487b

Browse files
committed
split command runner into legacy and migrated
1 parent d69e79c commit 09b487b

1 file changed

Lines changed: 60 additions & 8 deletions

File tree

cmd/src/cmd.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
78
"os"
89
"slices"
10+
"sort"
911

12+
"github.com/sourcegraph/src-cli/internal/clicompat"
1013
"github.com/sourcegraph/src-cli/internal/cmderrors"
14+
"github.com/urfave/cli/v3"
15+
16+
"github.com/sourcegraph/sourcegraph/lib/errors"
1117
)
1218

19+
var MigratedCommands = map[string]*cli.Command{
20+
"version": versionCommandv2,
21+
}
22+
1323
// command is a subcommand handler and its flag set.
1424
type command struct {
1525
// flagSet is the flag set for the command.
@@ -68,12 +78,26 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
6878

6979
// Find the subcommand to execute.
7080
name := flagSet.Arg(0)
81+
7182
for _, cmd := range c {
72-
if !cmd.matches(name) {
83+
_, isMigratedCmd := MigratedCommands[name]
84+
if !isMigratedCmd && !cmd.matches(name) {
7385
continue
7486
}
87+
// Read global configuration now.
88+
var err error
89+
cfg, err = readConfig()
90+
if err != nil {
91+
log.Fatal("reading config: ", err)
92+
}
93+
94+
var exitCode int
7595

76-
exitCode, err := runLegacy(cmd, flagSet)
96+
if isMigratedCmd {
97+
exitCode, err = runMigrated(flagSet)
98+
} else {
99+
exitCode, err = runLegacy(cmd, flagSet)
100+
}
77101
if err != nil {
78102
log.Fatal(err)
79103
}
@@ -84,14 +108,42 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
84108
log.Fatalf("Run '%s help' for usage.", cmdName)
85109
}
86110

87-
func runLegacy(cmd *command, flagSet *flag.FlagSet) (int, error) {
88-
// Read global configuration now.
89-
var err error
90-
cfg, err = readConfig()
91-
if err != nil {
92-
log.Fatal("reading config: ", err)
111+
// migratedRootCommand constructs a root 'src' command and adds
112+
// MigratedCommands as subcommands to it
113+
func migratedRootCommand() *cli.Command {
114+
names := make([]string, 0, len(MigratedCommands))
115+
for name := range MigratedCommands {
116+
names = append(names, name)
93117
}
118+
sort.Strings(names)
94119

120+
commands := make([]*cli.Command, 0, len(names))
121+
for _, name := range names {
122+
commands = append(commands, MigratedCommands[name])
123+
}
124+
125+
return clicompat.WithLegacyRootCommandHelp(&cli.Command{
126+
Name: "src",
127+
HideVersion: true,
128+
Commands: commands,
129+
})
130+
}
131+
132+
// runMigrated runs the command within urfave/cli framework
133+
func runMigrated(flagSet *flag.FlagSet) (int, error) {
134+
ctx := context.Background()
135+
args := append([]string{"src"}, flagSet.Args()...)
136+
137+
err := migratedRootCommand().Run(ctx, args)
138+
var exitErr cli.ExitCoder
139+
if errors.AsInterface(err, &exitErr) {
140+
return exitErr.ExitCode(), err
141+
}
142+
return 0, err
143+
}
144+
145+
// runLegacy runs the command using the original commander framework
146+
func runLegacy(cmd *command, flagSet *flag.FlagSet) (int, error) {
95147
// Parse subcommand flags.
96148
args := flagSet.Args()[1:]
97149
if err := cmd.flagSet.Parse(args); err != nil {

0 commit comments

Comments
 (0)