-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathrun_migration_compat.go
More file actions
102 lines (89 loc) · 2.42 KB
/
run_migration_compat.go
File metadata and controls
102 lines (89 loc) · 2.42 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"sort"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/sourcegraph/src-cli/internal/cmderrors"
"github.com/urfave/cli/v3"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
var migratedCommands = map[string]*cli.Command{
"version": versionCommand,
}
func maybeRunMigratedCommand() (isMigrated bool, exitCode int, err error) {
// need to figure out if a migrated command has been requested
flag.Parse()
subCommand := flag.CommandLine.Arg(0)
_, isMigrated = migratedCommands[subCommand]
if !isMigrated {
return
}
cfg, err = readConfig()
if err != nil {
log.Fatal("reading config: ", err)
}
exitCode, err = runMigrated()
return
}
// migratedRootCommand constructs a root 'src' command and adds
// MigratedCommands as subcommands to it
func migratedRootCommand() *cli.Command {
names := make([]string, 0, len(migratedCommands))
for name := range migratedCommands {
names = append(names, name)
}
sort.Strings(names)
commands := make([]*cli.Command, 0, len(names))
for _, name := range names {
commands = append(commands, migratedCommands[name])
}
return clicompat.WrapRoot(&cli.Command{
Name: "src",
HideVersion: true,
Commands: commands,
})
}
// runMigrated runs the command within urfave/cli framework
func runMigrated() (int, error) {
ctx := context.Background()
err := migratedRootCommand().Run(ctx, os.Args)
if errors.HasType[*cmderrors.UsageError](err) {
return 2, nil
}
var exitErr cli.ExitCoder
if errors.AsInterface(err, &exitErr) {
return exitErr.ExitCode(), err
}
return 0, err
}
// runLegacy runs the command using the original commander framework
func runLegacy(cmd *command, flagSet *flag.FlagSet) (int, error) {
// Parse subcommand flags.
args := flagSet.Args()[1:]
if err := cmd.flagSet.Parse(args); err != nil {
fmt.Printf("Error parsing subcommand flags: %s\n", err)
panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err))
}
// Execute the subcommand.
if err := cmd.handler(flagSet.Args()[1:]); err != nil {
if _, ok := err.(*cmderrors.UsageError); ok {
log.Printf("error: %s\n\n", err)
cmd.flagSet.SetOutput(os.Stderr)
flag.CommandLine.SetOutput(os.Stderr)
cmd.flagSet.Usage()
return 2, nil
}
if e, ok := err.(*cmderrors.ExitCodeError); ok {
if e.HasError() {
log.Println(e)
}
return e.Code(), nil
}
return 1, err
}
return 0, nil
}