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

Consider changing cmd/ directory as cobra command recommendation #641

Closed
integrii opened this issue Feb 27, 2018 · 4 comments
Closed

Consider changing cmd/ directory as cobra command recommendation #641

integrii opened this issue Feb 27, 2018 · 4 comments

Comments

@integrii
Copy link

@integrii integrii commented Feb 27, 2018

Hello @spf13 and thank you for your many great contributions. I contributed to Hugo some time ago and use it for several sites!

The typical go pattern is to use the cmd directory like an index of all buildable binaries in a project. Cobra directly conflicts with this by recommending that the cmd directory is used to hold small packages which each contain cobra command packages.

Please consider recommending a more compatible file structure, such as this one that nests cobra commands with their relevant binaries:

project/
    Dockerfile
    kubernetes.yaml
    README.md
    cmd/
        binaryNameA/
            main.go
            cobraCommandA/
                cobraCommandA.go
            cobraCommandB/
                cobraCommandB.go
        binaryNameB/
            main.go
            otherStuff.go
    pkg/
        packageName/
            packageName.go

Or perhaps this layout, which organizes the cobra command packages as packages:

project/
    Dockerfile
    kubernetes.yaml
    README.md
    cmd/
        binaryNameA/
            main.go
        binaryNameB/
            main.go
            otherStuff.go
    pkg/
        packageName/
            packageName.go
        cobraCommandA/
            cobraCommandA.go
        cobraCommandB/
            cobraCommandB.go

The goal here is to enable the typical cmd/ directory behavior - rather than trying to make cobra users decide which layout to use.

Thanks again!

@integrii
Copy link
Author

@integrii integrii commented Apr 29, 2018

I ended up making my own flags package to solve this problem: https://github.com/integrii/flaggy

@buchanae
Copy link

@buchanae buchanae commented Jul 13, 2018

As I understand it, cobra doesn't require anything in particular, so this is a simple documentation change to the README. Is that right?

Or is this also related to the code generator.

For example, we use cobra with package directories per-subcommand here: https://github.com/ohsu-comp-bio/funnel/tree/master/cmd

@integrii
Copy link
Author

@integrii integrii commented Jul 13, 2018

I suppose you could always go against the recommended directory layout. My proposal was to change the recommended patterns.

I think its safe to close this for now :-)

@peterbourgon
Copy link

@peterbourgon peterbourgon commented Sep 6, 2020

I don't want to necromancy the issue or whatever, but the recommended layout generates a fair bit of confusion for new Gophers, as the pretty clear idiom in the rest of the ecosystem is to keep cmd/ free of actual .go files, just a kind of directory namespace for other subdirs that hold whatever binaries are in the project.

A few other things in the codegen utility also seem to go against the grain: creating package names with_underscores, and naming files as camelCase.go instead of snake_case.go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants