-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] improve generated boilerplate. #1614
Comments
I'll check out that article but 👍👍 The globals and init methods are an issue and we did have some other issues resolving to redo all that. Definitely would review a PR for that if you're up for it. 🎉 |
The pattern described in the article is actually the one many programs based on Cobra do use, in my (limited) experience. It would make sense to improve the generated boilerplate code. As a note however, the code generator was very recently moved to |
Created new issue there and linked to original comment. Closing. |
In my opinion the generated boilerplate is an unmaintainable mess.
You have got global variables and init functions all over the place.
Which may follow a compiler specified initialization order but imo is pretty unclear at first glance. Especially when you start to have more than a single init function in a package.
I would like to propose that instead of a lot of init functions and global variables we introduce constructor functions as described in this pretty neat article: https://gianarb.it/blog/golang-mockmania-cli-command-with-cobra
The constructors do clearly show the tree structure that is build top down from the root command down to the last sub commands.
This allows for constructing either the whole application or defined sub commands of the application at any point one wants to and not at the point of the top most package (main) initialization time.
Also as pointed out by the blog post, it allows to properly test the application, especially the cli part of it.
The boilerplate in itself is a single command iirc. The above text refers to the state in which your application will be if you follow the approach that the boilerplate shows.
The text was updated successfully, but these errors were encountered: