Skip to content

Commit

Permalink
Use an interface to encapsulate CSV values
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Apr 19, 2021
1 parent 0d1cf5b commit a7251a0
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 26 deletions.
4 changes: 4 additions & 0 deletions internal/cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (b *Backup) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(b.orig, "", " ")
}

func (b *Backup) MarshalCSVValue() interface{} {
return []*Backup{b}
}

func (b Backups) String() string {
var buf strings.Builder
tableprinter.Print(&buf, b)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/backup/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ShowCmd(ch *cmdutil.Helper) *cobra.Command {

end()

return ch.Printer.PrintResource(toBackups([]*planetscale.Backup{bkp}))
return ch.Printer.PrintResource(toBackup(bkp))
},
}

Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/backup/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func TestBackup_ShowCmd(t *testing.T) {
branch := "development"
backup := "mybackup"

res := []*ps.Backup{
{Name: "foo"},
}
res := &ps.Backup{Name: "foo"}

svc := &mock.BackupsService{
GetFn: func(ctx context.Context, req *ps.GetBackupRequest) (*ps.Backup, error) {
Expand All @@ -38,7 +36,7 @@ func TestBackup_ShowCmd(t *testing.T) {
c.Assert(req.Branch, qt.Equals, branch)
c.Assert(req.Backup, qt.Equals, backup)

return res[0], nil
return res, nil
},
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (d *DatabaseBranch) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(d.orig, "", " ")
}

func (d *DatabaseBranch) MarshalCSVValue() interface{} {
return []*DatabaseBranch{d}
}

// toDatabaseBranch returns a struct that prints out the various fields of a
// database model.
func toDatabaseBranch(db *ps.DatabaseBranch) *DatabaseBranch {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/branch/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ShowCmd(ch *cmdutil.Helper) *cobra.Command {

end()

return ch.Printer.PrintResource(toDatabaseBranches([]*planetscale.DatabaseBranch{b}))
return ch.Printer.PrintResource(toDatabaseBranch(b))
},
}

Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/branch/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ func TestBranch_ShowCmd(t *testing.T) {
db := "planetscale"
branch := "development"

res := []*ps.DatabaseBranch{
{Name: branch},
}
res := &ps.DatabaseBranch{Name: branch}

svc := &mock.DatabaseBranchesService{
GetFn: func(ctx context.Context, req *ps.GetDatabaseBranchRequest) (*ps.DatabaseBranch, error) {
c.Assert(req.Branch, qt.Equals, branch)
c.Assert(req.Database, qt.Equals, db)
c.Assert(req.Organization, qt.Equals, org)

return res[0], nil
return res, nil
},
}

Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"time"

"github.com/gocarina/gocsv"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -72,6 +71,6 @@ func (d *Database) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(d.orig, "", " ")
}

func (d *Database) MarshalCSV() (string, error) {
return gocsv.MarshalString([]*Database{d})
func (d *Database) MarshalCSVValue() interface{} {
return []*Database{d}
}
3 changes: 1 addition & 2 deletions internal/cmd/database/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/planetscale/cli/internal/printer"

"github.com/planetscale/planetscale-go/planetscale"
ps "github.com/planetscale/planetscale-go/planetscale"

"github.com/pkg/browser"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,7 +61,7 @@ func ShowCmd(ch *cmdutil.Helper) *cobra.Command {
}
end()

return ch.Printer.PrintResource(toDatabases([]*ps.Database{database}))
return ch.Printer.PrintResource(toDatabase(database))
},
}

Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/database/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ func TestDatabase_ShowCmd(t *testing.T) {
org := "planetscale"
db := "planetscale"

res := []*ps.Database{
{Name: "foo"},
}
res := &ps.Database{Name: "foo"}

svc := &mock.DatabaseService{
GetFn: func(ctx context.Context, req *ps.GetDatabaseRequest) (*ps.Database, error) {
c.Assert(req.Organization, qt.Equals, org)
c.Assert(req.Database, qt.Equals, db)

return res[0], nil
return res, nil
},
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/deployrequest/dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type DeployRequest struct {
ClosedAt *int64 `header:"closed_at,timestamp(ms|utc|human),-" json:"closed_at"`
}

func (d *DeployRequest) MarshalCSVValue() interface{} {
return []*DeployRequest{d}
}

func toDeployRequest(dr *planetscale.DeployRequest) *DeployRequest {
return &DeployRequest{
ID: dr.ID,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/deployrequest/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ShowCmd(ch *cmdutil.Helper) *cobra.Command {
}
}

return ch.Printer.PrintResource(toDeployRequests([]*planetscale.DeployRequest{dr}))
return ch.Printer.PrintResource(toDeployRequest(dr))
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/deployrequest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ func TestDeployRequest_ShowCmd(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(svc.GetFnInvoked, qt.IsTrue)

res := []*DeployRequest{{Number: number}}
res := &DeployRequest{Number: number}
c.Assert(buf.String(), qt.JSONEquals, res)
}
12 changes: 8 additions & 4 deletions internal/cmd/org/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ func ShowCmd(ch *cmdutil.Helper) *cobra.Command {
return nil
}

var res = []struct {
Org string `json:"org"`
}{{Org: cfg.Organization}}
if ch.Printer.Format() == printer.CSV {
var res = []struct {
Org string `json:"org"`
}{{Org: cfg.Organization}}

return ch.Printer.PrintResource(res)
return ch.Printer.PrintResource(res)
}

return ch.Printer.PrintResource(map[string]string{"org": cfg.Organization})
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/snapshot/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ShowCmd(ch *cmdutil.Helper) *cobra.Command {
}
end()

return ch.Printer.PrintResource(toSchemaSnapshots([]*planetscale.SchemaSnapshot{snapshot}))
return ch.Printer.PrintResource(toSchemaSnapshot(snapshot))
},
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (s *SchemaSnapshot) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(s.orig, "", " ")
}

func (s *SchemaSnapshot) MarshalCSVValue() interface{} {
return []*SchemaSnapshot{s}
}

// toSchemaSnapshot returns a struct that prints out the various fields
// of a schema snapshot model.
func toSchemaSnapshot(ss *ps.SchemaSnapshot) *SchemaSnapshot {
Expand Down
8 changes: 8 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ func (p *Printer) PrintResource(v interface{}) error {
fmt.Fprintln(out, string(buf))
return nil
case CSV:
type csvvaluer interface {
MarshalCSVValue() interface{}
}

if c, ok := v.(csvvaluer); ok {
v = c.MarshalCSVValue()
}

buf, err := gocsv.MarshalString(v)
if err != nil {
return err
Expand Down

0 comments on commit a7251a0

Please sign in to comment.