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

About subcommand directory #1059

Closed
fanux opened this issue Mar 13, 2020 · 13 comments
Closed

About subcommand directory #1059

fanux opened this issue Mar 13, 2020 · 13 comments
Labels
area/cobra-command Core `cobra.Command` implementations

Comments

@fanux
Copy link

fanux commented Mar 13, 2020

➜  app cobra add create
create created at /Users/fanux/work/src/app/cmd/create.go
➜  app cobra add user -p create
user created at /Users/fanux/work/src/app/cmd/user.go
➜  app tree
.
├── LICENSE
├── cmd
│   ├── create.go
│   ├── root.go
│   └── user.go
└── main.go

cobra generates subcommand user in the same dir of its parent dir, it not very nice.

If I add a command user this user command is not create subcommand:

 cobra add user
Error: /Users/fanux/work/src/app/cmd/user.go already exists

So I think, create subcommand in a sub dir is better, like this:

.
├── LICENSE
├── cmd
│   ├── create.go
│   ├── root.go
|   |---create
│       └── user.go
└── main.go

This will not conflict

@jharshman jharshman added the area/cobra-command Core `cobra.Command` implementations label Mar 30, 2020
@joshuabezaleel
Copy link

Hi @fanux @jharshman , I am interested in this issue if that would be okay but I think I need to ask some clarification questions. Suppose that we are still discussing the example of creating a user subcommand under create command like the one mentioned above. Does it mean that:

  1. user.go and other subcommands under create folder will be have a package name of create? e.g. package create on the top of the code.
  2. since the create command in the upper level package of cmd and user subcommand in the lower level package of create live in different packages means that we need to make the createCmd at the cmd/create.go to be exported (CreateCmd) so that we can import it at cmd/create/user.go to add the userCmd command under create command?
import (
   "github.com/joshuabezaleel/test-cobra/cmd"
)

var userCmd

func init() {
   cmd.CreateCmd.AddCommand(userCmd)

}

Looking forward to hearing your thoughts! Thank you very much 🙂

@fanux
Copy link
Author

fanux commented May 21, 2020

For example,my app command line like this:

app user create xxx
app group create xxx

So I want generate codes:

cobra add user
cobra add group
cobra add create -p user
cobra add create -p group

If all create.go in cmd dir, it will failed, and mess...
A better way is cmd/user/create.go and cmd/group/create.go

@fanux
Copy link
Author

fanux commented May 21, 2020

Other example:

app create user
app delete user
cobra add create
cobra add delete
cobra add user -p create
cobra add user -p delete

@joshuabezaleel
Copy link

For example,my app command line like this:

app user create xxx
app group create xxx

So I want generate codes:

cobra add user
cobra add group
cobra add create -p user
cobra add create -p group

If all create.go in cmd dir, it will failed, and mess...
A better way is cmd/user/create.go and cmd/group/create.go

@fanux
Yes I got this so far. But by having cmd/user/create.go it means that

  1. create.go file package is using package user and
  2. either cmd/user/create.go needs to import package cmd to add userCmd (cmd/user.go) so that we can use cmd.UserCmd.AddCommand(createCmd) or it works otherwise, where
    cmd/user.go needs to import package cmd/user so that we can use userCmd.AddCommand(user.createCmd) because we need to register createCmd as the subcommand from userCmd right?

@fanux
Copy link
Author

fanux commented May 22, 2020

So I think only subcommand rely on parent command, can we regist subcommand in cmd/user/create.go? Set userCmd as public. So in cmd/user/create.go we can regist subcommand like this: UserCmd.AddCommand(createCmd), cmd/user.go no need import any more.

@InTheCloudDan
Copy link

I'm also interested in this capability. To model after the kubernetes style of verb noun for commands.

@github-actions
Copy link

github-actions bot commented Aug 6, 2020

This issue is being marked as stale due to a long period of inactivity

@trouphaz
Copy link

I'm interested in this. In the meantime, I will try just creating separate files for each subcommand.

@johnSchnake
Copy link
Collaborator

Now that the cobra-cli has been moved to https://github.com/spf13/cobra-cli/ this issue should be migrated there. I'll let you so that you are subscribed and the OP.

@mloskot
Copy link

mloskot commented Dec 14, 2023

It looks like this issue has not been migrated by the OP or anyone as I can not find it in the cobra-cli repo.
It also is unclear if support of sub-directories instead of flat files has been added to cobra-cli, has it?
The user_guide.md displays how to organise subcomands in folders but does cobra-cli support it?

I'm interested in this feature, shall I copy this issue to cobra-cli myself?

@marckhouzam
Copy link
Collaborator

Yes please. Thank you.

@mloskot
Copy link

mloskot commented Dec 14, 2023

@marckhouzam
Copy link
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cobra-command Core `cobra.Command` implementations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants