Skip to content

Commit

Permalink
POC: Displayer helper
Browse files Browse the repository at this point in the history
Exploration for adding a helper that builds a displayer.
  • Loading branch information
dadgar committed Feb 26, 2024
1 parent ae89ffc commit e8a2311
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 61 deletions.
2 changes: 1 addition & 1 deletion internal/commands/projects/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func createRun(opts *CreateOpts) error {
}

// Display the created project
d := newDisplayer(format.Pretty, true, resp.Payload.Project)
d := format.NewDisplayer(resp.Payload.Project, format.Pretty, projectFields)
if err := opts.Output.Display(d); err != nil {
return err
}
Expand Down
57 changes: 0 additions & 57 deletions internal/commands/projects/displayer.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/commands/projects/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ func listRun(opts *ListOpts) error {
req.PaginationNextPageToken = &next
}

d := newDisplayer(format.Table, false, projects...)
d := format.NewDisplayer(projects, format.Table, projectFields)
return opts.Output.Display(d)
}
11 changes: 11 additions & 0 deletions internal/commands/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ package projects
import (
"github.com/hashicorp/hcp/internal/commands/projects/iam"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/format"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
)

var (
// projectFields is the set of fields to display for a project.
projectFields = []format.Field{
format.NewField("Name", "{{ .Name }}"),
format.NewField("ID", "{{ .ID }}"),
format.NewField("Description", "{{ .Description }}"),
format.NewField("Created At", "{{ .CreatedAt }}"),
}
)

func NewCmdProjects(ctx *cmd.Context) *cmd.Command {
cmd := &cmd.Command{
Name: "projects",
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/projects/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewCmdRead(ctx *cmd.Context, runF func(*ReadOpts) error) *cmd.Command {
LongHelp: "Show metadata for the project.",
Examples: []cmd.Example{
{
Preamble: "Read a project:",
Preamble: "Read a project.",
Command: "$ hcp projects read --project=cd3d34d5-ceeb-493d-b004-9297365a01af",
},
},
Expand Down Expand Up @@ -61,6 +61,6 @@ func readRun(opts *ReadOpts) error {
return fmt.Errorf("failed to read project: %w", err)
}

d := newDisplayer(format.Pretty, true, resp.Payload.Project)
d := format.NewDisplayer(resp.Payload.Project, format.Pretty, projectFields)
return opts.Output.Display(d)
}
27 changes: 27 additions & 0 deletions internal/pkg/format/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package format

// NewField creates a new Field with the given name and value format string. See
// the Field struct for more information.
func NewField(name, valueFormat string) Field {
return Field{Name: name, ValueFormat: valueFormat}
}

// NewDisplayer creates a new Displayer with the given payload, default format,
// and fields.
func NewDisplayer[T any](payload T, defaultFormat Format, fields []Field) Displayer {
return &internalDisplayer[T]{
payload: payload,
fields: fields,
defaultFormat: defaultFormat,
}
}

type internalDisplayer[T any] struct {
payload T
fields []Field
defaultFormat Format
}

func (i *internalDisplayer[T]) DefaultFormat() Format { return i.defaultFormat }
func (i *internalDisplayer[T]) FieldTemplates() []Field { return i.fields }
func (i *internalDisplayer[T]) Payload() any { return i.payload }

0 comments on commit e8a2311

Please sign in to comment.