-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move helpers to output.go and add example
- Loading branch information
Showing
3 changed files
with
87 additions
and
27 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,61 @@ | ||
package format_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcp/internal/pkg/format" | ||
"github.com/hashicorp/hcp/internal/pkg/iostreams" | ||
) | ||
|
||
func ExampleDisplayer() { | ||
// Create the outputter. This is typically passed to the command. | ||
io := iostreams.Test() | ||
outputter := format.New(io) | ||
|
||
// Resource is an example resource that we want to display | ||
type Resource struct { | ||
Name string | ||
ID string | ||
Description string | ||
} | ||
|
||
// Define the fields that we want to ExampleDisplayer | ||
var fields = []format.Field{ | ||
format.NewField("Name", "{{ .Name }}"), | ||
format.NewField("ID", "{{ .ID }}"), | ||
format.NewField("Description", "{{ .Description }}"), | ||
} | ||
|
||
// Build our mock resources. Typically this is the response payload from an API | ||
// request. | ||
payload := []Resource{ | ||
{ | ||
Name: "hello", | ||
ID: "123", | ||
Description: "world", | ||
}, | ||
{ | ||
Name: "another", | ||
ID: "456", | ||
Description: "example", | ||
}, | ||
} | ||
|
||
// Build the displayer | ||
d := format.NewDisplayer(payload, format.Pretty, fields) | ||
|
||
// Run the displayer | ||
outputter.Display(d) | ||
|
||
// Since the IO is a test io, manually print it | ||
fmt.Println(io.Output.String()) | ||
|
||
// Output: | ||
// Name: hello | ||
// ID: 123 | ||
// Description: world | ||
// --- | ||
// Name: another | ||
// ID: 456 | ||
// Description: example | ||
} |
This file was deleted.
Oops, something went wrong.
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