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

Refactor the user guide to avoid to use the init() func #1862

Open
enrichman opened this issue Nov 21, 2022 · 5 comments
Open

Refactor the user guide to avoid to use the init() func #1862

enrichman opened this issue Nov 21, 2022 · 5 comments
Labels
kind/documentation Documentation of cobra itself

Comments

@enrichman
Copy link
Contributor

enrichman commented Nov 21, 2022

The user guide is linked from the main README, and I guess it's where most of the people will just land for a quick copy-paste.

The guide is showing how to setup a command with the init() func, but I think it's a bad approach (and also looking for related issues I'm not the only one):

One of the main problem of this is that is not possible to handle any errors, so I would like to know if it was feasible to at least update the user guide to show a more idiomatic approach (i.e. the BindPFlag func err is ignored, and the errcheck linter complains about it).

Are you willing to accept a PR on a different style? 🙂

@johnSchnake johnSchnake added the kind/documentation Documentation of cobra itself label Nov 30, 2022
@johnSchnake
Copy link
Collaborator

Absolutely; as the other issues make clear we are on board with those kind of changes. My only comment would be to try and make it so that those changes (boilerplate, docs, etc) all change in a similar fashion to avoid confusion. If you want to try and tackle multiple parts of that then even better.

@github-actions
Copy link

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interested in reopening

@asphaltbuffet
Copy link

nope

@red8888
Copy link

red8888 commented Oct 24, 2023

Just want to add this is kind of tangentially related to documenting or changing the default templates to setup unit testing correctly: #770

I'm new to cobra and I am trying the approach of wrapping the command in a function (common approach), but the template includes init().

I assumed init() was required and it took me some googling to find out 1. I don't need init() and 2. I shouldn't even be using it:

func RootCmd() *cobra.Command {
	return &cobra.Command{
		.....
	}
}

func init() {
	cobra.OnInitialize(initConfig)
	// What do I do about rootCmd in init() now that its a func not a var?
	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dirk.yaml)")
	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

@sheldonhull
Copy link

Not sure if useful but I avoided mostly init() in this projct: https://github.com/sheldonhull/az-pr/blob/e23d6b7f4d500310e938e51e88e5ba553b0edf5b/cmd/root.go#L45

Still some stuff to cleanup (quick side project), so maybe that can get you started until someone else provides more detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/documentation Documentation of cobra itself
Projects
None yet
Development

No branches or pull requests

5 participants