cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Refactor the user guide to avoid to use the `init()` func

Open enrichman opened this issue 2 years ago • 5 comments

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):

  • https://github.com/spf13/cobra/issues/1335
  • https://github.com/spf13/cobra/issues/1336
  • https://github.com/spf13/cobra/issues/1599
  • https://github.com/spf13/cobra-cli/issues/12
    • https://github.com/spf13/cobra/issues/1614

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? :slightly_smiling_face:

enrichman avatar Nov 21 '22 15:11 enrichman

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.

johnSchnake avatar Nov 30 '22 18:11 johnSchnake

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

github-actions[bot] avatar Jan 30 '23 00:01 github-actions[bot]

nope

asphaltbuffet avatar Feb 11 '23 21:02 asphaltbuffet

Just want to add this is kind of tangentially related to documenting or changing the default templates to setup unit testing correctly: https://github.com/spf13/cobra/issues/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")
}

red8888 avatar Oct 24 '23 18:10 red8888

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.

sheldonhull avatar Oct 24 '23 21:10 sheldonhull