loov / goda

Go Dependency Analysis toolkit
MIT License
1.37k stars 45 forks source link

Include version information on packages/modules #57

Closed iskunk closed 2 years ago

iskunk commented 2 years ago

I am attempting to use goda to produce the same form of output as go mod graph, for use in an application that parses the latter. goda graph -type edges comes close, save for the lack of version suffixes. There does not appear to be a format string argument that will provide this.

It would be helpful for version information to at least be available (as a label-formatting directive) when generating dependency output.

egonelbre commented 2 years ago

You can now use .Module.Version to get the package version information.

As an example:

goda list -f "{{ printf \"%v %v\" .ID .Module.Version }}" ./...:all
goda list -f "{{.ID}}{{with .Module.Version}}@{{.}}{{end}}" ./...:all
iskunk commented 2 years ago

Many thanks!

I've confirmed that the second formatting example you gave is needed when a module version isn't always available (notably for the main project).

I do notice one oddity: I've been using just all for the package expression, which seems to work. When I use ./...:all, I get this:

$ goda graph -type edges -f '{{.ID}}{{with .Module.Version}}@{{.}}{{end}}' ./...:all >goda.out
template error: template: :1:21: executing "" at <.Module.Version>: nil pointer evaluating *packages.Module.Version
template error: template: :1:21: executing "" at <.Module.Version>: nil pointer evaluating *packages.Module.Version
template error: template: :1:21: executing "" at <.Module.Version>: nil pointer evaluating *packages.Module.Version
...

(The output that does get written out is slightly different from all, however, which makes me suspect I'm missing some subtlety in the package selection...)

egonelbre commented 2 years ago

You may need to add additional: goda graph -type edges -f '{{.ID}}{{if .Module}}{{with .Module.Version}}@{{.}}{{end}}{{end}}' ./...:all.

I haven't yet encountered this myself, but I guess it's possible that in some cases .Module is nil.

iskunk commented 2 years ago

Ah, I see what you mean. I've confirmed that invocation works cleanly on the same project, a relatively large and complex one.

I hope there can be better documentation on the formatting syntax eventually, but for now, this example of it should do the trick for me. Thank you again!

egonelbre commented 2 years ago

It uses https://pkg.go.dev/text/template for formatting. You can use goda list -f '{{ printf "%#v\n" . }}' ., To see what is available. Each package usually gets https://github.com/loov/goda/blob/master/internal/pkggraph/graph.go#L22 as package. Depending on the subcommand it may have some additional / fewer things.

iskunk commented 2 years ago

That's very good information. I don't know if the template syntax is recognizable to seasoned gophers, but as I come from outside the community, the absence of any references in the README.md or help output left me unenlightened.

I would suggest adding this, and perhaps a table listing some of the available attributes. The availability of .Module.Version isn't obvious from the location you linked above, for example---just from the brevity of the above commit, it appears that many of the attributes are populated implicitly.

egonelbre commented 2 years ago

Yeah, I've had https://github.com/loov/goda/issues/40 open for a while :D... but just haven't found the motivation to write it.

iskunk commented 2 years ago

I sympathize! Just the link you provided above would be plenty... and adding e.g.

# list dependency graph in same format as "go mod graph"
goda graph -type edges -f '{{.ID}}{{if .Module}}{{with .Module.Version}}@{{.}}{{end}}{{end}}' ./...:all

as a usage example would address a significant use case. Longer-form documentation would be nice, but this should be quite helpful low-hanging fruit.

egonelbre commented 2 years ago

I added goda help format, this has more information. I'll also add your proposed example.

iskunk commented 2 years ago

Looks good! This is a definite improvement, and will help others as it would have helped me.

(One typo I noticed: "...and it's statistics.")