Skip to content

Commit 91684f9

Browse files
authored
fix(devtools/cmd/migrate-librarian): make repo path a positional arg (#3187)
The repo path is now a positional argument instead of a -repo flag, allowing usage like `migrate-librarian .` instead of `migrate-librarian -repo .`. The deriveLanguage function now converts the path to an absolute path before checking if it ends with "go" or "python", enabling it to work correctly with relative paths like ".".
1 parent 9dfc00a commit 91684f9

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

devtools/cmd/migrate-librarian/main.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ const (
4343
)
4444

4545
var (
46-
errRepoNotFound = errors.New("-repo flag is required")
47-
errLangNotSupported = errors.New("only go and python are supported")
4846
errFetchSource = errors.New("cannot fetch source")
47+
errLangNotSupported = errors.New("only go and python are supported")
48+
errRepoNotFound = errors.New("exactly one repo path argument is required")
4949
errTidyFailed = errors.New("librarian tidy failed")
5050

5151
fetchSource = fetchGoogleapis
@@ -60,27 +60,26 @@ func main() {
6060

6161
func run(ctx context.Context, args []string) error {
6262
flagSet := flag.NewFlagSet("migrate-librarian", flag.ContinueOnError)
63-
repoPath := flagSet.String("repo", "",
64-
"Path to the repository containing legacy .librarian configuration (required). Must end with go or python.")
6563
outputPath := flagSet.String("output", "./librarian.yaml", "Output file path (default: ./librarian.yaml)")
6664
if err := flagSet.Parse(args); err != nil {
6765
return err
6866
}
69-
if *repoPath == "" {
67+
if flagSet.NArg() != 1 {
7068
return errRepoNotFound
7169
}
70+
repoPath := flagSet.Arg(0)
7271

73-
language, err := deriveLanguage(*repoPath)
72+
language, err := deriveLanguage(repoPath)
7473
if err != nil {
7574
return err
7675
}
7776

78-
librarianState, err := readState(*repoPath)
77+
librarianState, err := readState(repoPath)
7978
if err != nil {
8079
return err
8180
}
8281

83-
librarianConfig, err := readConfig(*repoPath)
82+
librarianConfig, err := readConfig(repoPath)
8483
if err != nil {
8584
return err
8685
}
@@ -102,15 +101,15 @@ func run(ctx context.Context, args []string) error {
102101
}
103102

104103
func deriveLanguage(repoPath string) (string, error) {
105-
if strings.HasSuffix(repoPath, "go") {
104+
base := filepath.Base(repoPath)
105+
switch {
106+
case strings.HasSuffix(base, "go"):
106107
return "go", nil
107-
}
108-
109-
if strings.HasSuffix(repoPath, "python") {
108+
case strings.HasSuffix(base, "python"):
110109
return "python", nil
110+
default:
111+
return "", errLangNotSupported
111112
}
112-
113-
return "", errLangNotSupported
114113
}
115114

116115
func buildConfig(

devtools/cmd/migrate-librarian/main_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func TestRunMigrateLibrarian(t *testing.T) {
3636
for _, test := range []struct {
3737
name string
3838
repoPath string
39-
lang string
4039
wantErr error
4140
}{
4241
{
@@ -69,10 +68,9 @@ func TestRunMigrateLibrarian(t *testing.T) {
6968
}
7069
})
7170

72-
args := []string{"-repo", test.repoPath, "-output", outputPath}
73-
74-
if test.lang != "" {
75-
args = append(args, "-lang", test.lang)
71+
args := []string{"-output", outputPath}
72+
if test.repoPath != "" {
73+
args = append(args, test.repoPath)
7674
}
7775

7876
if err := run(t.Context(), args); err != nil {

0 commit comments

Comments
 (0)