Skip to content
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

Closed
jxsl13 opened this issue Feb 27, 2022 · 3 comments
Closed

[RFC] improve generated boilerplate. #1614

jxsl13 opened this issue Feb 27, 2022 · 3 comments

Comments

@jxsl13
Copy link

jxsl13 commented Feb 27, 2022

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.

@johnSchnake
Copy link
Collaborator

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.

🎉

@marckhouzam
Copy link
Collaborator

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 https://github.com/spf13/cobra-cli so I believe this issue would need to be opened there instead.

@johnSchnake
Copy link
Collaborator

Created new issue there and linked to original comment. Closing.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants