Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Status template output #1389

Merged
merged 3 commits into from
Nov 27, 2017
Merged
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
34 changes: 34 additions & 0 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -191,6 +192,25 @@ func (out *dotOutput) MissingHeader() {}
func (out *dotOutput) MissingLine(ms *MissingStatus) {}
func (out *dotOutput) MissingFooter() {}

type templateOutput struct {
w io.Writer
tmpl *template.Template
}

func (out *templateOutput) BasicHeader() {}
func (out *templateOutput) BasicFooter() {}

func (out *templateOutput) BasicLine(bs *BasicStatus) {
out.tmpl.Execute(out.w, bs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should errors be captured? Don't necessarily need to halt on error since it may not affect all lines (although go list -f does halt), but I'm not sure whether the feedback would be better in-line to out.w or routed to ctx.Out/Err. Maybe Err, to cleanly separate it for anything parsing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh! I didn't realize that it returned an error. Maybe because we are not handling the returned error in any of the other writes, like error from fmt.Fprintf() in tableOutput and json.Encode() in jsonOutput. Encoding failure seems important to handle to me.
Routing the error to Err should be better; similar to how we keep the partial status output separate from the raised errors.

Copy link
Member

Choose a reason for hiding this comment

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

@darkowlzz so is there a change to be made here? this was on my list to "merge fast, fail fast," but it seems like you're planning on a followup.

}

func (out *templateOutput) MissingHeader() {}
func (out *templateOutput) MissingFooter() {}

func (out *templateOutput) MissingLine(ms *MissingStatus) {
out.tmpl.Execute(out.w, ms)
Copy link

Choose a reason for hiding this comment

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

Using the same template for BasicLine and MissingLine makes hard to write a template that handles both.
I think that -f should only provide formatting for BasicLine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dolmen thanks for reviewing :)

I don't think there is any use-case for MissingStatus with templateOutput because MissingStatus is used only while printing the error invoked by input-digest mismatch due to missing packages. This function exists because it's required to implement the outputter interface and avoid empty output when user tries to use templateout and there's a inputs-digest mismatch due to missing packages.

A non-templateOutput status with missing packages error looks like this.

$ dep status
Lock inputs-digest mismatch due to the following packages missing from the lock:

PROJECT                MISSING PACKAGES
github.com/pkg/errors  [github.com/pkg/errors]

This happens when a new import is added. Run `dep ensure` to install the missing packages.
input-digest mismatch

When the user does $ dep status -f="some-template" only the table output in the above error message would be replaced by the template rendered content. I'm not sure if that can be useful rendered content in any way.

Is there some use-case that I'm overlooking? 🤔

}

func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
p, err := ctx.LoadProject()
if err != nil {
Expand All @@ -208,6 +228,11 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
return err
}

templateOut := false
if cmd.template != "" {
templateOut = true
}

Copy link

Choose a reason for hiding this comment

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

This block is not necessary. See below.

var buf bytes.Buffer
var out outputter
switch {
Expand All @@ -231,6 +256,15 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
o: cmd.output,
w: &buf,
}
case templateOut:
Copy link

Choose a reason for hiding this comment

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

replace with:

case cmd.template != "":

tmpl, err := template.New("status").Parse(cmd.template)
if err != nil {
return err
}
out = &templateOutput{
w: &buf,
tmpl: tmpl,
}
default:
out = &tableOutput{
w: tabwriter.NewWriter(&buf, 0, 4, 2, ' ', 0),
Expand Down