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 #106

Open
mloskot opened this issue Dec 14, 2023 · 3 comments
Open

About subcommand directory #106

mloskot opened this issue Dec 14, 2023 · 3 comments

Comments

@mloskot
Copy link

mloskot commented Dec 14, 2023

This is transfer of @fanux's spf13/cobra#1059 as suggested there by @johnSchnake


➜  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

@mloskot
Copy link
Author

mloskot commented Dec 14, 2023

(Copying myself from spf13/cobra#1059 (comment))

Interestingly, the user_guide.md displays how to organise subcomands in folders but does or could cobra-cli support it?

@nirs
Copy link

nirs commented Dec 15, 2023

The current behavior is good, it is useful for the common case of single package command. I think the missing part is to support also the hierarchical layout when initializing a project or adding a command.

I can think of 2 ways to solve this:

add flag

Adding a child command as a package:

cobra-cli add --package foo

Will create:

cmd/
    root.go
    foo/
        foo.go

Adding a sub command of foo:

cobra-cli add --parent foo bar

Will create:

cmd/
    root.go
    foo/
        foo.go
        bar.go

Adding a sub package for foo:

cobra-cli add --package --parent foo bar

cmd/
    root.go
    foo/
        foo.go
        bar/
            bar.go

Generator option

Add an option the the .cobra.yaml to use single package or multi-package layout. This will make it easier to have consistent initialization in the entire project.

Example yaml for single package (default):

layout: single-package

With this child commands are created in the same package as root and add the child command to the root command in the child init().

Example yaml for multi-package layout (new):

layout: multi-package

With this child command is created in a new package and parent init() is changed to add the child command.

This does not solve he issue of adding a leaf command in a sub package, in this case we most likely want to keep the leaf file in the parent package.

@mloskot
Copy link
Author

mloskot commented Dec 21, 2023

I think the missing part is to support also the hierarchical layout when initializing a project or adding a command.

Yes, I think so. Yesterday I found this video where the author presents a simple semi-manual workaround how to use Cobra CLI and maintain folder-based structure of commands. Here is the specific moment linked How to write beautiful Golang CLI

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

2 participants