Shareable Go package experiment in sdk-platform-java#3938
Conversation
langaugecontainer and message packages are to be migrated to the librarian repository.
There was a problem hiding this comment.
Code Review
This pull request refactors the command-line handling into a shared languagecontainer package, which is a good step towards modularity. However, there are a few critical issues that need to be addressed. The new implementation incorrectly passes os.Args to the Run function, which will cause command dispatching to fail. I've also pointed out that error handling has been changed to use panic, which is not ideal for a CLI tool and should be reverted to graceful error logging and exit codes. Additionally, the --version flag functionality has been lost in the refactoring. My review includes suggestions to fix these issues.
| slog.Info("librariangen: finished successfully") | ||
| return 0 | ||
| container := javaContainer{} | ||
| os.Exit(languagecontainer.Run(os.Args, &container)) |
There was a problem hiding this comment.
The Run function in languagecontainer expects the command-line arguments without the program name. Currently, os.Args is passed, which includes the program name as the first element (os.Args[0]). This will cause the command dispatching in Run to fail, as it will try to match the program name against commands like generate. You should pass os.Args[1:] instead.
| os.Exit(languagecontainer.Run(os.Args, &container)) | |
| os.Exit(languagecontainer.Run(os.Args[1:], &container)) |
| if len(args) < 1 { | ||
| panic("args must not be empty") | ||
| } |
There was a problem hiding this comment.
| if err != nil { | ||
| // TODO: Save it as a response file. | ||
| panic(err) | ||
| } |
There was a problem hiding this comment.
Using panic for handling errors from business logic can lead to abrupt program termination, which is generally undesirable for a CLI tool. It's better to log the error and return a non-zero exit code to indicate failure gracefully.
if err != nil {
// TODO: Save it as a response file.
slog.Error("librariangen: generate command failed", "error", err)
return 1
}| if err != nil { | ||
| panic(err) | ||
| } |
There was a problem hiding this comment.
Using panic for handling errors can lead to abrupt program termination. It's better to log the error and return a non-zero exit code to indicate failure gracefully, which is more conventional for CLI applications.
if err != nil {
slog.Error("librariangen: configure command failed", "error", err)
return 1
}| slog.Error("librariangen: unknown command: %s (with flags %v)", args[0], args) | ||
| return 1 | ||
| } | ||
| return 0 |
|
|
||
| // run executes the appropriate command based on the CLI's invocation arguments. | ||
| // The idiomatic structure is `librariangen [command] [flags]`. | ||
| func run(ctx context.Context, args []string) error { |
|




langaugecontainer and message packages are to be migrated to
the librarian repository.
Thank you for opening a Pull Request! Before submitting your PR, please read our contributing guidelines.
There are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> ☕️