Skip to content
This repository has been archived by the owner on Jun 6, 2022. It is now read-only.

Improve templating #96

Merged
merged 4 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ Running
-------

```
$ retrodep -h
retrodep: help requested
usage: retrodep [OPTION]... PATH
-debug
show debugging output
show debugging output
-deps
show vendored dependencies (default true)
-exclude-from string
ignore glob patterns listed in provided file
show vendored dependencies (default true)
-exclude-from exclusions
ignore directory entries matching globs in exclusions
-help
print help
print help
-importpath string
top-level import path
top-level import path
-o string
output format, one of: go-template=...
-only-importpath
only show the top-level import path
only show the top-level import path
-template string
go template to use for output with Repo, Rev, Tag and Ver
-x exit on the first failure
go template to use for output with Reference fields (deprecated)
-x exit on the first failure
```

In many cases retrodep can work out the import path for the top-level project. In those cases, simply supply the directory name to examine:
Expand Down Expand Up @@ -68,24 +69,25 @@ Example output
--------------

```
$ retrodep -importpath github.com/docker/distribution go/src/github.com/docker/distribution
*github.com/docker/distribution@90705d2fb81dda1466be49bd958ed8a0dd9a6145 ~v2.6.0rc.1-1.20180831002537-90705d2fb81d
github.com/opencontainers/image-spec@87998cd070d9e7a2c79f8b153a26bea0425582e5 =v1.0.0 ~v1.0.0
github.com/ncw/swift@b964f2ca856aac39885e258ad25aec08d5f64ee6 ~v1.0.25-0.20160617142549-b964f2ca856a
golang.org/x/oauth2@2897dcade18a126645f1368de827f1e613a60049 ~v0.0.0-0.20160323192119-2897dcade18a
rsc.io/letsencrypt ?
$ retrodep $GOPATH/src/github.com/docker/distribution
github.com/docker/distribution:v2.7.1
github.com/docker/distribution:v2.7.1/github.com/Azure/azure-sdk-for-go:v16.2.1
github.com/docker/distribution:v2.7.1/github.com/Azure/go-autorest:v10.8.1
github.com/docker/distribution:v2.7.1/github.com/Shopify/logrus-bugsnag:v0.0.0-0.20171204154709-577dee27f20d
github.com/docker/distribution:v2.7.1/github.com/aws/aws-sdk-go:v1.15.11
github.com/docker/distribution:v2.7.1/github.com/beorn7/perks:v0.0.0-0.20160804124726-4c0e84591b9a
github.com/docker/distribution:v2.7.1/github.com/bshuster-repo/logrus-logstash-hook:0.4
github.com/docker/distribution:v2.7.1/github.com/bugsnag/bugsnag-go:v1.0.3-0.20150204195350-f36a9b3a9e01
...
```

In this example,

* github.com/docker/distribution is the top-level repository (indicated by \*)
* github.com/opencontainers/image-spec had a matching semantic version tag (v1.0.0)
* github.com/ncw/swift's matching commit had semantic version tag v1.0.24 reachable (so the next patch version would be v1.0.25)
* golang.org/x/oauth2 had a matching commit but no reachable semantic version tag
* rsc.io/letsencrypt had no matching commit (because the vendored source was from a fork)
* for github.com/docker/distribution, tag v2.6.0rc.1 was reachable from the matching commit (note, 1.timestamp instead of 0.timestamp)

* github.com/docker/distribution is the top-level package, and the upstream semantic version tag v2.7.1 matches
* github.com/Azure/azure-sdk-for-go etc are vendored dependencies of distribution
* github.com/Azure/azure-sdk-for-go, github.com/Azure/go-autorest, github.com/aws/awk-sdk-go, and github.com/bshuster-repo/logrus-logstash-hook all had matches with upstream semantic version tags
* github.com/bugsnag/bugsnag-go matched a commit from which tag v1.0.2 was reachable (note: v1.0.2, not v1.0.3 -- see below)
* github.com/beorn7/perks matched a commit from which there were no reachable semantic version tags

Pseudo-versions
---------------
Expand Down
61 changes: 44 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import (
"path/filepath"
"sort"
"strings"
"text/template"

"github.com/op/go-logging"
"github.com/release-engineering/retrodep/retrodep"
)

const defaultTemplate string = "{{if .TopPkg}}{{.TopPkg}}:{{.TopVer}}/{{end}}{{.Pkg}}:{{.Ver}}"

var log = logging.MustGetLogger("retrodep")

var helpFlag = flag.Bool("help", false, "print help")
Expand All @@ -37,10 +40,12 @@ var onlyImportPath = flag.Bool("only-importpath", false, "only show the top-leve
var depsFlag = flag.Bool("deps", true, "show vendored dependencies")
var excludeFrom = flag.String("exclude-from", "", "ignore directory entries matching globs in `exclusions`")
var debugFlag = flag.Bool("debug", false, "show debugging output")
var template = flag.String("template", "", "go template to use for output with Repo, Rev, Tag and Ver")
var outputArg = flag.String("o", "", "output format, one of: go-template=...")
var templateArg = flag.String("template", "", "go template to use for output with Pkg, Repo, Rev, Tag and Ver (deprecated)")
var exitFirst = flag.Bool("x", false, "exit on the first failure")

var errorShown = false
var usage func(string)

func displayUnknown(name string) {
fmt.Printf("%s ?\n", name)
Expand All @@ -53,14 +58,10 @@ func displayUnknown(name string) {
}
}

func display(template string, name string, ref *retrodep.Reference) {
func display(tmpl *template.Template, name string, ref *retrodep.Reference) {
var builder strings.Builder
builder.WriteString(name)
var tmpl, err = retrodep.Display(template)
if err != nil {
log.Fatalf("Error parsing supplied template. %s ", err)
}
err = tmpl.Execute(&builder, ref)
err := tmpl.Execute(&builder, ref)
if err != nil {
log.Fatalf("Error generating output. %s ", err)
}
Expand All @@ -82,20 +83,30 @@ func getProject(src *retrodep.GoSource, importPath string) *retrodep.RepoPath {
return main
}

func showTopLevel(src *retrodep.GoSource) {
func showTopLevel(tmpl *template.Template, src *retrodep.GoSource) *retrodep.Reference {
main := getProject(src, *importPath)
project, err := src.DescribeProject(main, src.Path)
project, err := src.DescribeProject(main, src.Path, nil)
var topLevelMarker string
if *templateArg != "" {
topLevelMarker = "*"
}
switch err {
case retrodep.ErrorVersionNotFound:
displayUnknown("*" + main.Root)
displayUnknown(topLevelMarker + main.Root)
case nil:
display(*template, "*"+main.Root, project)
display(tmpl, topLevelMarker, project)
default:
log.Fatalf("%s: %s", src.Path, err)
}

if err != nil {
return nil
}

return project
}

func showVendored(src *retrodep.GoSource) {
func showVendored(tmpl *template.Template, src *retrodep.GoSource, top *retrodep.Reference) {
vendored, err := src.VendoredProjects()
if err != nil {
log.Fatal(err)
Expand All @@ -111,12 +122,12 @@ func showVendored(src *retrodep.GoSource) {
// Describe each vendored project
for _, repo := range repos {
project := vendored[repo]
vp, err := src.DescribeVendoredProject(project)
vp, err := src.DescribeVendoredProject(project, top)
switch err {
case retrodep.ErrorVersionNotFound:
displayUnknown(project.Root)
case nil:
display(*template, project.Root, vp)
display(tmpl, "", vp)
default:
log.Fatalf("%s: %s\n", project.Root, err)
}
Expand Down Expand Up @@ -153,7 +164,7 @@ func processArgs(args []string) []*retrodep.GoSource {
cli.Usage = func() {}

usageMsg := fmt.Sprintf("usage: %s [OPTION]... PATH", progName)
usage := func(flaw string) {
usage = func(flaw string) {
log.Fatalf("%s: %s\n%s\n", progName, flaw, usageMsg)
}
err := cli.Parse(args[1:])
Expand Down Expand Up @@ -198,14 +209,30 @@ func processArgs(args []string) []*retrodep.GoSource {
func main() {
srcs := processArgs(os.Args)

var customTemplate string
switch {
case *outputArg != "":
customTemplate = strings.TrimPrefix(*outputArg, "go-template=")
if customTemplate == *outputArg {
usage("unknown output format")
}
case *templateArg != "":
customTemplate = "{{.Pkg}}" + *templateArg
default:
customTemplate = defaultTemplate
}
tmpl, err := template.New("output").Parse(customTemplate)
if err != nil {
log.Fatal(err)
}
for _, src := range srcs {
if *onlyImportPath {
main := getProject(src, *importPath)
fmt.Println("*" + main.Root)
} else {
showTopLevel(src)
top := showTopLevel(tmpl, src)
if *depsFlag {
showVendored(src)
showVendored(tmpl, src, top)
}
}
}
Expand Down
15 changes: 0 additions & 15 deletions retrodep/display.go

This file was deleted.

99 changes: 0 additions & 99 deletions retrodep/display_test.go

This file was deleted.

Loading