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

cmd/go: add a go mod edit -f flag akin to go list -f #51757

Open
mvdan opened this issue Mar 17, 2022 · 1 comment
Open

cmd/go: add a go mod edit -f flag akin to go list -f #51757

mvdan opened this issue Mar 17, 2022 · 1 comment
Labels
GoCommand NeedsDecision Tools
Milestone

Comments

@mvdan
Copy link
Member

@mvdan mvdan commented Mar 17, 2022

go list -json is very useful, because it makes its output very easy to consume by other programs. Its companion, go list -f, is almost equally as useful for similar cases, such as when one needs just one field, or when one is calling the Go tool from a shell script, where JSON decoding isn't as straightforward.

So, for instance, I can obtain the current package's import path via just go list -f {{.ImportPath}} rather than reaching for an external tool like go list -json | jq -r .ImportPath.

I argue that a similar situation applies to go mod edit -json; it's a great way to obtain the data contained by go.mod files without needing to use a parser that understands its syntax. However, we lack a go mod edit -f, so if I want to obtain just one field like {{.Go}}, I have to reach for a JSON parser.

I think it would be relatively straightforward to add go mod edit -f, and it would make the command more consistent with go list -f.

One potential argument against this proposed flag addition is that we already have other cmd/go commands that have a -json flag, but no -f flag. One by one, and excluding go mod edit, these are:

  • go work edit: arguably the same case as go mod edit, and should be changed as well
  • go test -json: it prints a newline-separated stream of JSON objects, and it tends to print a lot more output, so a proper program with a JSON parser is a reasonable requirement
  • go env -json: it's already easy to get a single value without JSON encoding via go env NAME, so go env -f {{.NAME}} feels redundant
@mvdan mvdan added GoCommand Tools labels Mar 17, 2022
@mvdan
Copy link
Member Author

@mvdan mvdan commented Mar 17, 2022

cc @bcmills @matloob for cmd/go and workspaces

@heschi heschi added the NeedsDecision label Mar 17, 2022
@heschi heschi added this to the Backlog milestone Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand NeedsDecision Tools
Projects
None yet
Development

No branches or pull requests

2 participants