From 4cdaaeb5488eb59b7cc5ba039fc07581c93f8b10 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 19 Nov 2017 23:10:54 +0530 Subject: [PATCH 1/3] status: Add template formatted output support --- cmd/dep/status.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index a1f9a0a8c1..fbd3ea511f 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -9,6 +9,7 @@ import ( "encoding/json" "flag" "fmt" + "html/template" "io" "io/ioutil" "log" @@ -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) +} + +func (out *templateOutput) MissingHeader() {} +func (out *templateOutput) MissingFooter() {} + +func (out *templateOutput) MissingLine(ms *MissingStatus) { + out.tmpl.Execute(out.w, ms) +} + func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { p, err := ctx.LoadProject() if err != nil { @@ -208,6 +228,11 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { return err } + templateOut := false + if cmd.template != "" { + templateOut = true + } + var buf bytes.Buffer var out outputter switch { @@ -231,6 +256,15 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { o: cmd.output, w: &buf, } + case templateOut: + 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), From ede5f57ea1280a0065beec96d3e1a3453d010dbd Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 19 Nov 2017 23:22:35 +0530 Subject: [PATCH 2/3] test: Integration test for status template output --- .../status/case1/template/final/Gopkg.lock | 21 +++++++++++++++++++ .../status/case1/template/final/Gopkg.toml | 3 +++ .../status/case1/template/initial/Gopkg.lock | 13 ++++++++++++ .../status/case1/template/initial/Gopkg.toml | 3 +++ .../status/case1/template/initial/main.go | 18 ++++++++++++++++ .../status/case1/template/stdout.txt | 2 ++ .../status/case1/template/testcase.json | 11 ++++++++++ 7 files changed, 71 insertions(+) create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.lock create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.toml create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.lock create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.toml create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/initial/main.go create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/stdout.txt create mode 100644 cmd/dep/testdata/harness_tests/status/case1/template/testcase.json diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.lock new file mode 100644 index 0000000000..77278d07bc --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/sdboyer/deptest" + packages = ["."] + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v0.8.0" + +[[projects]] + name = "github.com/sdboyer/deptestdos" + packages = ["."] + revision = "5c607206be5decd28e6263ffffdcee067266015e" + version = "v2.0.0" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "1b381263a360eafafe3ef7f9be626672668d17250a3c9a8debd169d1b5e2eebb" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.toml new file mode 100644 index 0000000000..94deb714a4 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/final/Gopkg.toml @@ -0,0 +1,3 @@ +[[constraint]] + name = "github.com/sdboyer/deptest" + version = "^0.8.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.lock new file mode 100644 index 0000000000..04268862a1 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.lock @@ -0,0 +1,13 @@ +memo = "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5" + +[[projects]] + name = "github.com/sdboyer/deptest" + version = "v0.8.0" + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + packages = ["."] + +[[projects]] + name = "github.com/sdboyer/deptestdos" + version = "v2.0.0" + revision = "5c607206be5decd28e6263ffffdcee067266015e" + packages = ["."] diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.toml new file mode 100644 index 0000000000..94deb714a4 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/initial/Gopkg.toml @@ -0,0 +1,3 @@ +[[constraint]] + name = "github.com/sdboyer/deptest" + version = "^0.8.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/initial/main.go b/cmd/dep/testdata/harness_tests/status/case1/template/initial/main.go new file mode 100644 index 0000000000..2eae5b511d --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/initial/main.go @@ -0,0 +1,18 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "github.com/sdboyer/deptest" + "github.com/sdboyer/deptestdos" +) + +func main() { + err := nil + if err != nil { + deptest.Map["yo yo!"] + } + deptestdos.diMeLo("whatev") +} diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/stdout.txt b/cmd/dep/testdata/harness_tests/status/case1/template/stdout.txt new file mode 100644 index 0000000000..c27320c8b6 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/stdout.txt @@ -0,0 +1,2 @@ +PROJECT: github.com/sdboyer/deptest, VERSION: v0.8.0 +PROJECT: github.com/sdboyer/deptestdos, VERSION: v2.0.0 diff --git a/cmd/dep/testdata/harness_tests/status/case1/template/testcase.json b/cmd/dep/testdata/harness_tests/status/case1/template/testcase.json new file mode 100644 index 0000000000..c208fbf6fd --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/template/testcase.json @@ -0,0 +1,11 @@ +{ + "commands": [ + ["ensure"], + ["status", "-f=PROJECT: {{.ProjectRoot}}, VERSION: {{.Version}}\n"] + ], + "error-expected": "", + "vendor-final": [ + "github.com/sdboyer/deptest", + "github.com/sdboyer/deptestdos" + ] +} From 8a9cea36fe052711fbc42b86e1f99a4fa0065deb Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 20 Nov 2017 22:31:55 +0530 Subject: [PATCH 3/3] status: simplify template command selection --- cmd/dep/status.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index fbd3ea511f..785c86c64b 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -228,11 +228,6 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { return err } - templateOut := false - if cmd.template != "" { - templateOut = true - } - var buf bytes.Buffer var out outputter switch { @@ -256,7 +251,7 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { o: cmd.output, w: &buf, } - case templateOut: + case cmd.template != "": tmpl, err := template.New("status").Parse(cmd.template) if err != nil { return err