Skip to content

Commit f6ee79b

Browse files
authored
refactor(internal/surfer): migrate to urfave/cli/v3 (#3106)
The surfer package now uses github.com/urfave/cli/v3 instead of the internal legacycli package. This is the same CLI framework as the librarian package, and removes a dependency on legacy code.
1 parent 0b91aaf commit f6ee79b

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

cmd/surfer/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
func main() {
2727
ctx := context.Background()
28-
if err := surfer.Run(ctx, os.Args[1:]); err != nil {
28+
if err := surfer.Run(ctx, os.Args...); err != nil {
2929
log.Fatal(err)
3030
}
3131
}

internal/surfer/surfer/surfer.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,51 @@ import (
1919
"context"
2020
"fmt"
2121

22-
cli "github.com/googleapis/librarian/internal/legacylibrarian/legacycli"
2322
"github.com/googleapis/librarian/internal/surfer/gcloud"
23+
"github.com/urfave/cli/v3"
2424
)
2525

2626
// Run executes the surfer CLI with the given command line arguments.
27-
func Run(ctx context.Context, args []string) error {
27+
func Run(ctx context.Context, args ...string) error {
2828
cmd := &cli.Command{
29-
Short: "surfer generates gcloud command YAML files",
30-
UsageLine: "surfer generate [arguments]",
31-
Long: "surfer generates gcloud command YAML files",
29+
Name: "surfer",
30+
Usage: "generates gcloud command YAML files",
31+
UsageText: "surfer generate [arguments]",
32+
Description: "surfer generates gcloud command YAML files",
3233
Commands: []*cli.Command{
33-
newCmdGenerate(),
34+
generateCommand(),
3435
},
3536
}
36-
cmd.Init()
3737
return cmd.Run(ctx, args)
3838
}
3939

40-
func newCmdGenerate() *cli.Command {
41-
var (
42-
googleapis string
43-
out string
44-
)
45-
46-
cmdGenerate := &cli.Command{
47-
Short: "generate generates gcloud commands",
48-
UsageLine: "surfer generate <path to gcloud.yaml> --googleapis <path> [--out <path>]",
49-
Long: `generate generates gcloud commands
50-
51-
generate generates gcloud command files from protobuf API specifications,
40+
func generateCommand() *cli.Command {
41+
return &cli.Command{
42+
Name: "generate",
43+
Usage: "generates gcloud commands",
44+
UsageText: "surfer generate <path to gcloud.yaml> --googleapis <path> [--out <path>]",
45+
Description: `generate generates gcloud command files from protobuf API specifications,
5246
service config yaml, and gcloud.yaml.`,
47+
Flags: []cli.Flag{
48+
&cli.StringFlag{
49+
Name: "googleapis",
50+
Value: "https://github.com/googleapis/googleapis",
51+
Usage: "URL or directory path to googleapis",
52+
},
53+
&cli.StringFlag{
54+
Name: "out",
55+
Value: ".",
56+
Usage: "output directory",
57+
},
58+
},
5359
Action: func(ctx context.Context, cmd *cli.Command) error {
54-
args := cmd.Flags.Args()
55-
if len(args) == 0 {
60+
if cmd.Args().Len() == 0 {
5661
return fmt.Errorf("path to gcloud.yaml is required")
5762
}
58-
config := args[0]
63+
config := cmd.Args().First()
64+
googleapis := cmd.String("googleapis")
65+
out := cmd.String("out")
5966
return gcloud.Generate(ctx, googleapis, config, out)
6067
},
6168
}
62-
cmdGenerate.Init()
63-
cmdGenerate.Flags.StringVar(&googleapis, "googleapis", "https://github.com/googleapis/googleapis", "URL or directory path to googleapis")
64-
cmdGenerate.Flags.StringVar(&out, "out", ".", "output directory")
65-
return cmdGenerate
6669
}

internal/surfer/surfer/surfer_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestRun(t *testing.T) {
2828
{
2929
name: "valid command",
3030
args: []string{
31+
"surfer",
3132
"generate",
3233
"../gcloud/testdata/parallelstore/gcloud.yaml",
3334
"--out", "../gcloud/testdata/parallelstore/surface",
@@ -36,19 +37,20 @@ func TestRun(t *testing.T) {
3637
{
3738
name: "invalid gcloud.yaml filepath",
3839
args: []string{
40+
"surfer",
3941
"generate",
4042
"invalidpath/gcloud.yaml",
4143
},
4244
wantErr: true,
4345
},
4446
{
4547
name: "missing config arg",
46-
args: []string{"generate"},
48+
args: []string{"surfer", "generate"},
4749
wantErr: true,
4850
},
4951
} {
5052
t.Run(test.name, func(t *testing.T) {
51-
if err := Run(t.Context(), test.args); err != nil {
53+
if err := Run(t.Context(), test.args...); err != nil {
5254
// TODO(https://github.com/googleapis/librarian/issues/2817):
5355
// remove once the generate functionality has been implemented
5456
if strings.Contains(err.Error(), "failed to create API model") {

0 commit comments

Comments
 (0)