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

added template for output #83

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions backvendor/display.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package backvendor

import (
"fmt"
"io"
"text/template"
)

const defaultTemplate string = "@{{if .Rev}}{{.Rev}}{{end}}{{if .Tag}} ={{.Tag}}{{end}}{{if .Ver}} ~{{.Ver}}{{end}}"
twaugh marked this conversation as resolved.
Show resolved Hide resolved

//TemplateError Returned when there's an error creating a template from the arguments
type TemplateError template.Template

func (t TemplateError) Error(template template.Template) string {
return fmt.Sprintf("display: Error creating template from %v", template)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TemplateError isn't used.


//Display a Reference using a template
func Display(writer io.Writer, customTemplate string, ref *Reference) error {
if customTemplate == "" {
tmpl, err := template.New("output").Parse(defaultTemplate)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case perhaps we could just customTemplate = defaultTemplate (the caller is not affected by this) and fall through to the case where a custom template has been supplied.

if err != nil {
return err
}
err = tmpl.Execute(writer, ref)
if err != nil {
return err
}
} else {
tmpl, err := template.New("output").Parse(customTemplate)
if err != nil {
return err
}
err = tmpl.Execute(writer, ref)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once tmpl.Execute(writer, ref) is the last thing to do, If we aren't going to use anything like errors.Wrap() on the error we could just immediately return that result and leave the checking to the caller, e.g. return tmpl.Execute(writer, ref).

}
}
return nil
}
43 changes: 43 additions & 0 deletions backvendor/display_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package backvendor

import (
"strings"
"testing"
)

func TestDisplay(t *testing.T) {
ref := &Reference{Rev: "4309345093405934509", Tag: "v0.0.1", Ver: "v0.0.0.20181785"}
var builder strings.Builder
err := Display(&builder, "", ref)
if err != nil {
t.Fatal(err)
}
twaugh marked this conversation as resolved.
Show resolved Hide resolved
}

func TestDisplayGarbage(t *testing.T) {
ref := &Reference{Rev: "4309345093405934509", Tag: "v0.0.1", Ver: "v0.0.0.20181785"}
var builder strings.Builder
err := Display(&builder, "{{.", ref)
if err == nil {
t.Fatal("Should have failed with Error")
}
}

func TestDisplayNoTag(t *testing.T) {
ref := &Reference{Rev: "4309345093405934509", Ver: "v0.0.0.20181785"}
var builder strings.Builder
err := Display(&builder, "", ref)
if err != nil {
t.Fatal(err)
}
}

func TestDisplayTemplate(t *testing.T) {
ref := &Reference{Rev: "4309345093405934509", Ver: "v0.0.0.20181785"}
var builder strings.Builder
err := Display(&builder, "{{if .Tag}}:{{.Tag}}{{end}}{{if .Ver}}:{{.Ver}}{{end}}", ref)
if err != nil {
t.Fatal(err)
}

}
22 changes: 8 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var importPath = flag.String("importpath", "", "top-level import path")
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 Rev, Tag and Ver")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth putting the default value here instead of having to work out whether to use a fallback in Display()? (But don't want to clutter the help output, so not sure...)


var errorShown = false

Expand All @@ -47,18 +48,11 @@ func displayUnknown(name string) {
}
}

func display(name string, ref *backvendor.Reference) {
fmt.Print(name)
if ref.Rev != "" {
fmt.Print("@", ref.Rev)
}
if ref.Tag != "" {
fmt.Print(" =", ref.Tag)
}
if ref.Ver != "" {
fmt.Print(" ~", ref.Ver)
}
fmt.Print("\n")
func display(template string, name string, ref *backvendor.Reference) {
var builder strings.Builder
builder.WriteString(name)
backvendor.Display(&builder, template, ref)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to check the error.

fmt.Println(builder.String())
}

func showTopLevel(src *backvendor.GoSource) {
Expand All @@ -78,7 +72,7 @@ func showTopLevel(src *backvendor.GoSource) {
case backvendor.ErrorVersionNotFound:
displayUnknown("*" + main.Root)
case nil:
display("*"+main.Root, project)
display(*template, "*"+main.Root, project)
default:
log.Fatalf("%s: %s", src.Path, err)
}
Expand All @@ -105,7 +99,7 @@ func showVendored(src *backvendor.GoSource) {
case backvendor.ErrorVersionNotFound:
displayUnknown(project.Root)
case nil:
display(project.Root, vp)
display(*template, project.Root, vp)
default:
log.Fatalf("%s: %s\n", project.Root, err)
}
Expand Down