Cobra: About subcommand directory

Created on 13 Mar 2020  Β·  8Comments  Β·  Source: spf13/cobra

➜  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

arecmd kinstale

Most helpful comment

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

All 8 comments

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

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

Other example:

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

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?

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.

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eine picture eine  Β·  5Comments

supershal picture supershal  Β·  4Comments

mdaymard picture mdaymard  Β·  5Comments

andygrunwald picture andygrunwald  Β·  6Comments

pjbgf picture pjbgf  Β·  5Comments