This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module github.com/nuclio/renderer | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/ghodss/yaml v1.0.0 | ||
github.com/mattn/go-runewidth v0.0.4 // indirect | ||
github.com/nuclio/errors v0.0.1 | ||
github.com/olekukonko/tablewriter v0.0.1 | ||
github.com/stretchr/testify v1.3.0 // indirect | ||
gopkg.in/yaml.v2 v2.2.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= | ||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= | ||
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= | ||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= | ||
github.com/nuclio/errors v0.0.1 h1:JoADBDnhRKjW05Npu5CLS27Peo7gx+QZcNrLwINV6UY= | ||
github.com/nuclio/errors v0.0.1/go.mod h1:it2rUqDarIL8PasLYZo0Q1Ebsx4NRPM+OyYYakgNyrQ= | ||
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88= | ||
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2017 The Nuclio Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package renderer | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/nuclio/errors" | ||
"github.com/ghodss/yaml" | ||
"github.com/olekukonko/tablewriter" | ||
) | ||
|
||
type Renderer struct { | ||
output io.Writer | ||
} | ||
|
||
func NewRenderer(output io.Writer) *Renderer { | ||
return &Renderer{ | ||
output: output, | ||
} | ||
} | ||
|
||
func (r *Renderer) RenderTable(header []string, records [][]string) { | ||
tableWriter := tablewriter.NewWriter(r.output) | ||
tableWriter.SetHeader(header) | ||
tableWriter.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false}) | ||
tableWriter.SetCenterSeparator("|") | ||
tableWriter.SetHeaderLine(false) | ||
tableWriter.AppendBulk(records) | ||
tableWriter.Render() | ||
} | ||
|
||
func (r *Renderer) RenderYAML(items interface{}) error { | ||
body, err := yaml.Marshal(items) | ||
if err != nil { | ||
return errors.Wrap(err, "Failed to render YAML") | ||
} | ||
|
||
fmt.Fprintln(r.output, string(body)) // nolint: errcheck | ||
|
||
return nil | ||
} | ||
|
||
func (r *Renderer) RenderJSON(items interface{}) error { | ||
body, err := json.Marshal(items) | ||
if err != nil { | ||
return errors.Wrap(err, "Failed to render JSON") | ||
} | ||
|
||
var pbody bytes.Buffer | ||
err = json.Indent(&pbody, body, "", "\t") | ||
if err != nil { | ||
return errors.Wrap(err, "Failed to indent JSON") | ||
} | ||
|
||
fmt.Fprintln(r.output, pbody.String()) // nolint: errcheck | ||
|
||
return nil | ||
} |