-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Ability to add nested commands through cobra add
#266
Comments
@ianwalter I love the idea, please contribute this. If I can, I'll provide my ideas on what this feature should do/work like. I think that the syntax should be Currently the add command does support the |
That sounds good to me. I will work on this. I must have missed |
Lets leave it for now. It works slightly differently and supports a few use
|
Currently |
I don't think it should warn. I think it should just do the right thing.
|
Looks like this bug still exists. I'm happy to work on this. |
Please do :)
…On Fri, Dec 28, 2018 at 1:31 AM Kasisnu ***@***.***> wrote:
Looks like this bug still exists. I'm happy to work on this.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#266 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAKlZNVE5_7yCjur3t5HqJ8mWxKMVzsIks5u9brAgaJpZM4ICUNR>
.
|
@kasisnu what's the current status? |
This issue is being marked as stale due to a long period of inactivity |
For me this doesn't create the subdirectory automatically. Once I create the subdir, it creates a .go file inside it, but as discussed above the file has invalid syntax. The next problem is that the subdir is considered a separate package (I think?) One more problem is that since the command is now in a "sub-package", that sub-package needs to be explicitly imported somewhere (e.g. main) otherwise it won't even be compiled. @spf13 Should we make the parent command public? Add a public function? Something else? |
@joshuabezaleel I think this issue is a duplicate right? |
@geniass Yes, I think these 2 issues convey the same problem statement. |
Gonna reply later or in a few days. |
No worries. Will be happy to help and contribute if I can be of one @Wulfheart 🙂 |
@joshuabezaleel did you find / build a solution to this? Right now I managed to handle it by following the structure suggested here #641 (comment) but it makes the cobra cli kinda impossible to use with subcommands. I'll attempt to summarise the situation with a simple example (and my very sparse Go knowledge): We want to have a The directory tree should look like:
The
|
Hi @johnraz I successfully follow your
I don't know why. I have followed all your steps. |
Since this is a cobra-cli issue I'm going to close; it can be opened again in the new repo: https://github.com/spf13/cobra-cli/issues/ @rulisastra without being able to see your code its hard to help with the import cycle. The files in Also worth noting: I think the decision to put commands into subdirectories instead of a single large directory has some pros/cons. A related ticket to boilerplate project structure has been opened as well spf13/cobra-cli#12 and may be a good place to discuss that. |
I went ahead with the solution 2 proposed above. It seems like a natural way to organize sub-commands, especially if there are multiple levels. Would you be open to having this added to the docs? I'm happy to contribute |
Please contribute. Solution 2 seems like a viable approach. |
Using Solution too I am getting an import cycle because a sub-command is trying to import root.go so that it can access a value saved in a PersistentFlags() call. For example, in var BaseURL string
...
func init() {
rootCmd.PersistentFlags().StringVarP(&BaseURL, "server-url", "s", url, "Base URL")
} then What's the best way to avoid that? Should flags be persisted in a different package? |
I would love to use
cobra add
to create nested commands, for example:Cobra would then see that there are multiple arguments, create a directory called
db
if it doesn't exist and a command file inside of it calledmigrate.go
. It would also create another file either inside ofdb
or at the same directory level calleddb.go
orroot.go
that will simply create adbCommand
so that nested commands can be added to it. I can submit a PR for this after my existing PR is resolved. Thanks.The text was updated successfully, but these errors were encountered: