Skip to content

Commit

Permalink
Refactoring for JSON support in output
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Jun 11, 2020
1 parent 39d454e commit c0c657f
Show file tree
Hide file tree
Showing 28 changed files with 157 additions and 38 deletions.
22 changes: 22 additions & 0 deletions api/resource_platform.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
package api

func (c *Client) PlatformRegions() ([]Region, error) {
query := `
query {
platform {
regions {
name
code
}
}
}
`

req := c.NewRequest(query)

data, err := c.Run(req)
if err != nil {
return nil, err
}

return data.Platform.Regions, nil
}

func (c *Client) PlatformRegionsAll() ([]Region, error) {
query := `
query {
platform {
Expand Down
8 changes: 4 additions & 4 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,10 @@ type CheckState struct {
}

type Region struct {
Code string
Name string
Latitude float32
Longitude float32
Code string `json:"code"`
Name string `json:"name"`
Latitude float32 `json:"latitude,omitempty"`
Longitude float32 `json:"longitude,omitempty"`
}

type AutoscalingConfig struct {
Expand Down
12 changes: 4 additions & 8 deletions cmd/appInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/superfly/flyctl/docstrings"

"github.com/logrusorgru/aurora"
"github.com/superfly/flyctl/cmd/presenters"
)

Expand All @@ -21,26 +20,23 @@ func runAppInfo(ctx *CmdContext) error {
return err
}

fmt.Println(aurora.Bold("App"))
err = ctx.RenderEx(&presenters.AppInfo{App: *app}, presenters.Options{HideHeader: true, Vertical: true})
err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "App"})
if err != nil {
return err
}

fmt.Println(aurora.Bold("Services"))
err = ctx.Render(&presenters.Services{Services: app.Services})
err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.Services{Services: app.Services}, Title: "Services"})
if err != nil {
return err
}

fmt.Println(aurora.Bold("IP Addresses"))
err = ctx.Render(&presenters.IPAddresses{IPAddresses: app.IPAddresses.Nodes})
err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.IPAddresses{IPAddresses: app.IPAddresses.Nodes}, Title: "IP Adresses"})
if err != nil {
return err
}

if !app.Deployed {
fmt.Println(`App has not been deployed yet. Try running "flyctl deploy --image flyio/hellofly"`)
fmt.Fprintln(ctx.Out, `App has not been deployed yet. Try running "flyctl deploy --image flyio/hellofly"`)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func runAppsCreate(ctx *CmdContext) error {

fmt.Println("New app created")

err = ctx.RenderEx(&presenters.AppInfo{App: *app}, presenters.Options{HideHeader: true, Vertical: true})
err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true})
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func newCertificatesCommand() *Command {
create.Command.Args = cobra.ExactArgs(1)

certsDeleteStrings := docstrings.Get("certs.delete")
delete := BuildCommand(cmd, runCertDelete, certsDeleteStrings.Usage, certsDeleteStrings.Short, certsDeleteStrings.Long, os.Stdout, requireSession, requireAppName)
delete.Command.Args = cobra.ExactArgs(1)
delete.AddBoolFlag(BoolFlagOpts{Name: "yes", Shorthand: "y", Description: "accept all confirmations"})
deleteCmd := BuildCommand(cmd, runCertDelete, certsDeleteStrings.Usage, certsDeleteStrings.Short, certsDeleteStrings.Long, os.Stdout, requireSession, requireAppName)
deleteCmd.Command.Args = cobra.ExactArgs(1)
deleteCmd.AddBoolFlag(BoolFlagOpts{Name: "yes", Shorthand: "y", Description: "accept all confirmations"})

certsShowStrings := docstrings.Get("certs.show")
show := BuildCommand(cmd, runCertShow, certsShowStrings.Usage, certsShowStrings.Short, certsShowStrings.Long, os.Stdout, requireSession, requireAppName)
Expand Down Expand Up @@ -63,7 +63,7 @@ func runCertShow(ctx *CmdContext) error {
return err
}

return ctx.RenderEx(&presenters.Certificate{Certificate: cert}, presenters.Options{Vertical: true})
return ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.Certificate{Certificate: cert}, Vertical: true})
}

func runCertCheck(ctx *CmdContext) error {
Expand All @@ -74,7 +74,7 @@ func runCertCheck(ctx *CmdContext) error {
return err
}

return ctx.RenderEx(&presenters.Certificate{Certificate: cert}, presenters.Options{Vertical: true})
return ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.Certificate{Certificate: cert}, Vertical: true})
}

func runCertAdd(ctx *CmdContext) error {
Expand All @@ -85,7 +85,7 @@ func runCertAdd(ctx *CmdContext) error {
return err
}

return ctx.RenderEx(&presenters.Certificate{Certificate: cert}, presenters.Options{Vertical: true})
return ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.Certificate{Certificate: cert}, Vertical: true})
}

func runCertDelete(ctx *CmdContext) error {
Expand Down
31 changes: 20 additions & 11 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,21 @@ func (ctx *CmdContext) Render(presentable presenters.Presentable) error {
return presenter.Render()
}

// RenderEx - Render a presentable structure via the context with additional options
func (ctx *CmdContext) RenderEx(presentable presenters.Presentable, options presenters.Options) error {
presenter := &presenters.Presenter{
Item: presentable,
Out: os.Stdout,
Opts: options,
}

return presenter.Render()
}
//// RenderEx - Render a presentable structure via the context with additional options
//func (ctx *CmdContext) RenderEx(presentable presenters.Presentable, options presenters.Options) error {
// presenter := &presenters.Presenter{
// Item: presentable,
// Out: os.Stdout,
// Opts: options,
// }
//
// return presenter.Render()
//}

// PresenterOption - options for RenderEx, RenderView, render etc...
type PresenterOption struct {
Presentable presenters.Presentable
AsJSON bool
Vertical bool
HideHeader bool
Title string
Expand All @@ -191,10 +192,11 @@ func (ctx *CmdContext) render(out io.Writer, views ...PresenterOption) error {
Opts: presenters.Options{
Vertical: v.Vertical,
HideHeader: v.HideHeader,
AsJSON: v.AsJSON,
},
}

if v.Title != "" {
if v.Title != "" && !v.AsJSON {
fmt.Fprintln(out, aurora.Bold(v.Title))
}

Expand All @@ -208,6 +210,13 @@ func (ctx *CmdContext) render(out io.Writer, views ...PresenterOption) error {

// Frender - render a view to a Writer
func (ctx *CmdContext) Frender(w io.Writer, views ...PresenterOption) error {
// If JSON output wanted, set in all views
if ctx.GlobalConfig.GetBool(flyctl.ConfigJSONOutput) {
for i, _ := range views {
views[i].AsJSON = true
}
}

return ctx.render(w, views...)
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/platform.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"
"github.com/superfly/flyctl/flyctl"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,6 +36,8 @@ func runPlatformRegions(ctx *CmdContext) error {
return err
}

fmt.Println(ctx.GlobalConfig.GetBool(flyctl.ConfigJSONOutput))

return ctx.Frender(ctx.Out, PresenterOption{
Presentable: &presenters.Regions{Regions: regions},
})
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/alloc_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type AllocationChecks struct {
Checks []api.CheckState
}

func (p *AllocationChecks) APIStruct() interface{} {
return nil
}

func (p *AllocationChecks) FieldNames() []string {
return []string{"ID", "Service", "State", "Output"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/alloc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type AllocationEvents struct {
Events []api.AllocationEvent
}

func (p *AllocationEvents) APIStruct() interface{} {
return p.Events
}

func (p *AllocationEvents) FieldNames() []string {
return []string{"Timestamp", "Type", "Message"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Allocations struct {
Allocations []*api.AllocationStatus
}

func (p *Allocations) APIStruct() interface{} {
return p.Allocations
}

func (p *Allocations) FieldNames() []string {
return []string{"ID", "Version", "Region", "Desired", "Status", "Health Checks", "Restarts", "Created"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/appInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type AppInfo struct {
App api.App
}

func (p *AppInfo) APIStruct() interface{} {
return p.App
}

func (p *AppInfo) FieldNames() []string {
return []string{"Name", "Owner", "Version", "Status", "Hostname"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type Apps struct {
Apps []api.App
}

func (p *Apps) APIStruct() interface{} {
return nil
}

func (p *Apps) FieldNames() []string {
return []string{"Name", "Owner", "Status", "Latest Deploy"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type Builds struct {
Builds []api.Build
}

func (p *Builds) APIStruct() interface{} {
return nil
}

func (p *Builds) FieldNames() []string {
return []string{"ID", "Status", "User", "Created At", "Updated At"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type Certificate struct {
Certificate *api.AppCertificate
}

func (p *Certificate) APIStruct() interface{} {
return p.Certificate
}

func (p *Certificate) FieldNames() []string {
return []string{"Hostname", "Configured", "Issued", "Certificate Authority", "DNS Provider", "DNS Validation Instructions", "DNS Validation Hostname", "DNS Validation Target", "Source", "Created At", "Status"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type Certificates struct {
Certificates []api.AppCertificate
}

func (p *Certificates) APIStruct() interface{} {
return nil
}

func (p *Certificates) FieldNames() []string {
return []string{"Hostname", "Created At", "Status"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/deploymentStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type DeploymentStatus struct {
Status *api.DeploymentStatus
}

func (p *DeploymentStatus) APIStruct() interface{} {
return p.Status
}

func (p *DeploymentStatus) FieldNames() []string {
return []string{"ID", "Version", "Status", "Description", "Allocations"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type AppHistory struct {
AppChanges []api.AppChange
}

func (p *AppHistory) APIStruct() interface{} {
return nil
}

func (p *AppHistory) FieldNames() []string {
return []string{"Type", "Status", "Description", "User", "Date"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/ipAddresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type IPAddresses struct {
IPAddresses []api.IPAddress
}

func (p *IPAddresses) APIStruct() interface{} {
return nil
}

func (p *IPAddresses) FieldNames() []string {
return []string{"Type", "Address", "Created At"}
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/presenters/presenter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package presenters

import (
"encoding/json"
"fmt"
"io"

Expand All @@ -11,6 +12,7 @@ import (
type Presentable interface {
FieldNames() []string
Records() []map[string]string
APIStruct() interface{}
}

// Presenter - A self managing presenter which can be rendered in multiple ways
Expand All @@ -24,10 +26,15 @@ type Presenter struct {
type Options struct {
Vertical bool
HideHeader bool
AsJSON bool
}

// Render - Renders a presenter as a field list or table
func (p *Presenter) Render() error {
if p.Opts.AsJSON {
return p.renderJSON()
}

if p.Opts.Vertical {
return p.renderFieldList()
}
Expand Down Expand Up @@ -89,3 +96,14 @@ func (p *Presenter) renderFieldList() error {

return nil
}

func (p *Presenter) renderJSON() error {
data := p.Item.APIStruct()
if data == nil {
return fmt.Errorf("JSON output not available")
}
prettyJSON, err := json.MarshalIndent(p.Item.APIStruct(), "", " ")
fmt.Fprintln(p.Out, string(prettyJSON))

return err
}
4 changes: 4 additions & 0 deletions cmd/presenters/regionAutoscaleConfigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type AutoscalingRegionConfigs struct {
Regions []api.AutoscalingRegionConfig
}

func (p *AutoscalingRegionConfigs) APIStruct() interface{} {
return nil
}

func (p *AutoscalingRegionConfigs) FieldNames() []string {
return []string{"Region", "Min Count", "Weight"}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/presenters/regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type Regions struct {
Regions []api.Region
}

func (p *Regions) APIStruct() interface{} {
return p.Regions
}

func (p *Regions) FieldNames() []string {
return []string{"Code", "Name"}
}
Expand Down

0 comments on commit c0c657f

Please sign in to comment.