Skip to content

Commit e80aa76

Browse files
committed
move runMigrated to be in main and work with os.Args
1 parent a8a6336 commit e80aa76

File tree

4 files changed

+118
-90
lines changed

4 files changed

+118
-90
lines changed

cmd/src/cmd.go

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package main
22

33
import (
4-
"context"
5-
stderrors "errors"
64
"flag"
75
"fmt"
86
"log"
97
"os"
108
"slices"
11-
"sort"
12-
13-
"github.com/sourcegraph/src-cli/internal/clicompat"
14-
"github.com/sourcegraph/src-cli/internal/cmderrors"
15-
"github.com/urfave/cli/v3"
16-
17-
"github.com/sourcegraph/sourcegraph/lib/errors"
189
)
1910

20-
var MigratedCommands = map[string]*cli.Command{
21-
"version": versionCommandv2,
22-
}
23-
2411
// command is a subcommand handler and its flag set.
2512
type command struct {
2613
// flagSet is the flag set for the command.
@@ -80,9 +67,9 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
8067
// Find the subcommand to execute.
8168
name := flagSet.Arg(0)
8269

70+
// Command is legacy, so lets execute the old way
8371
for _, cmd := range c {
84-
_, isMigratedCmd := MigratedCommands[name]
85-
if !isMigratedCmd && !cmd.matches(name) {
72+
if !cmd.matches(name) {
8673
continue
8774
}
8875
// Read global configuration now.
@@ -92,13 +79,7 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
9279
log.Fatal("reading config: ", err)
9380
}
9481

95-
var exitCode int
96-
97-
if isMigratedCmd {
98-
exitCode, err = runMigrated(flagSet)
99-
} else {
100-
exitCode, err = runLegacy(cmd, flagSet)
101-
}
82+
exitCode, err := runLegacy(cmd, flagSet)
10283
if err != nil {
10384
log.Fatal(err)
10485
}
@@ -108,69 +89,3 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
10889
log.Printf("%s: unknown subcommand %q", cmdName, name)
10990
log.Fatalf("Run '%s help' for usage.", cmdName)
11091
}
111-
112-
// migratedRootCommand constructs a root 'src' command and adds
113-
// MigratedCommands as subcommands to it
114-
func migratedRootCommand() *cli.Command {
115-
names := make([]string, 0, len(MigratedCommands))
116-
for name := range MigratedCommands {
117-
names = append(names, name)
118-
}
119-
sort.Strings(names)
120-
121-
commands := make([]*cli.Command, 0, len(names))
122-
for _, name := range names {
123-
commands = append(commands, MigratedCommands[name])
124-
}
125-
126-
return clicompat.WrapRoot(&cli.Command{
127-
Name: "src",
128-
HideVersion: true,
129-
Commands: commands,
130-
})
131-
}
132-
133-
// runMigrated runs the command within urfave/cli framework
134-
func runMigrated(flagSet *flag.FlagSet) (int, error) {
135-
ctx := context.Background()
136-
args := append([]string{"src"}, flagSet.Args()...)
137-
138-
err := migratedRootCommand().Run(ctx, args)
139-
if _, ok := stderrors.AsType[*cmderrors.UsageError](err); ok {
140-
return 2, nil
141-
}
142-
var exitErr cli.ExitCoder
143-
if errors.AsInterface(err, &exitErr) {
144-
return exitErr.ExitCode(), err
145-
}
146-
return 0, err
147-
}
148-
149-
// runLegacy runs the command using the original commander framework
150-
func runLegacy(cmd *command, flagSet *flag.FlagSet) (int, error) {
151-
// Parse subcommand flags.
152-
args := flagSet.Args()[1:]
153-
if err := cmd.flagSet.Parse(args); err != nil {
154-
fmt.Printf("Error parsing subcommand flags: %s\n", err)
155-
panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err))
156-
}
157-
158-
// Execute the subcommand.
159-
if err := cmd.handler(flagSet.Args()[1:]); err != nil {
160-
if _, ok := err.(*cmderrors.UsageError); ok {
161-
log.Printf("error: %s\n\n", err)
162-
cmd.flagSet.SetOutput(os.Stderr)
163-
flag.CommandLine.SetOutput(os.Stderr)
164-
cmd.flagSet.Usage()
165-
return 2, nil
166-
}
167-
if e, ok := err.(*cmderrors.ExitCodeError); ok {
168-
if e.HasError() {
169-
log.Println(e)
170-
}
171-
return e.Code(), nil
172-
}
173-
return 1, err
174-
}
175-
return 0, nil
176-
}

cmd/src/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ func main() {
9393
log.SetFlags(0)
9494
log.SetPrefix("")
9595

96-
commands.run(flag.CommandLine, "src", usageText, normalizeDashHelp(os.Args[1:]))
96+
err, exitCode, ranMigratedCmd := maybeRunMigratedCommand()
97+
if ranMigratedCmd {
98+
if err != nil {
99+
log.Println(err)
100+
}
101+
os.Exit(exitCode)
102+
}
103+
104+
// if we didn't run a migrated command, then lets try running the legacy version
105+
if !ranMigratedCmd {
106+
commands.run(flag.CommandLine, "src", usageText, normalizeDashHelp(os.Args[1:]))
107+
}
97108
}
98109

99110
// normalizeDashHelp converts --help to -help since Go's flag parser only supports single dash.

cmd/src/run_migration_compat.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"os"
9+
"sort"
10+
11+
"github.com/sourcegraph/src-cli/internal/clicompat"
12+
"github.com/sourcegraph/src-cli/internal/cmderrors"
13+
"github.com/urfave/cli/v3"
14+
15+
"github.com/sourcegraph/sourcegraph/lib/errors"
16+
)
17+
18+
var migratedCommands = map[string]*cli.Command{
19+
"version": versionCommand,
20+
}
21+
22+
func maybeRunMigratedCommand() (err error, exitCode int, isMigrated bool) {
23+
// need to figure out if a migrated command has been requested
24+
flag.Parse()
25+
subCommand := flag.CommandLine.Arg(0)
26+
_, isMigrated = migratedCommands[subCommand]
27+
if !isMigrated {
28+
return
29+
}
30+
cfg, err = readConfig()
31+
if err != nil {
32+
log.Fatal("reading config: ", err)
33+
}
34+
35+
exitCode, err = runMigrated()
36+
return
37+
}
38+
39+
// migratedRootCommand constructs a root 'src' command and adds
40+
// MigratedCommands as subcommands to it
41+
func migratedRootCommand() *cli.Command {
42+
names := make([]string, 0, len(migratedCommands))
43+
for name := range migratedCommands {
44+
names = append(names, name)
45+
}
46+
sort.Strings(names)
47+
48+
commands := make([]*cli.Command, 0, len(names))
49+
for _, name := range names {
50+
commands = append(commands, migratedCommands[name])
51+
}
52+
53+
return clicompat.WrapRoot(&cli.Command{
54+
Name: "src",
55+
HideVersion: true,
56+
Commands: commands,
57+
})
58+
}
59+
60+
// runMigrated runs the command within urfave/cli framework
61+
func runMigrated() (int, error) {
62+
ctx := context.Background()
63+
64+
err := migratedRootCommand().Run(ctx, os.Args)
65+
if errors.HasType[*cmderrors.UsageError](err) {
66+
return 2, nil
67+
}
68+
var exitErr cli.ExitCoder
69+
if errors.AsInterface(err, &exitErr) {
70+
return exitErr.ExitCode(), err
71+
}
72+
return 0, err
73+
}
74+
75+
// runLegacy runs the command using the original commander framework
76+
func runLegacy(cmd *command, flagSet *flag.FlagSet) (int, error) {
77+
// Parse subcommand flags.
78+
args := flagSet.Args()[1:]
79+
if err := cmd.flagSet.Parse(args); err != nil {
80+
fmt.Printf("Error parsing subcommand flags: %s\n", err)
81+
panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err))
82+
}
83+
84+
// Execute the subcommand.
85+
if err := cmd.handler(flagSet.Args()[1:]); err != nil {
86+
if _, ok := err.(*cmderrors.UsageError); ok {
87+
log.Printf("error: %s\n\n", err)
88+
cmd.flagSet.SetOutput(os.Stderr)
89+
flag.CommandLine.SetOutput(os.Stderr)
90+
cmd.flagSet.Usage()
91+
return 2, nil
92+
}
93+
if e, ok := err.(*cmderrors.ExitCodeError); ok {
94+
if e.HasError() {
95+
log.Println(e)
96+
}
97+
return e.Code(), nil
98+
}
99+
return 1, err
100+
}
101+
return 0, nil
102+
}

cmd/src/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const versionExamples = `Examples:
2424
$ src version
2525
`
2626

27-
var versionCommandv2 = clicompat.Wrap(&cli.Command{
27+
var versionCommand = clicompat.Wrap(&cli.Command{
2828
Name: "version",
2929
Usage: "display and compare the src-cli version against the recommended version for your instance",
3030
UsageText: "src version [options]",

0 commit comments

Comments
 (0)