Skip to content

Commit

Permalink
Merge pull request #155 from superfly/fixverbosehandling
Browse files Browse the repository at this point in the history
Fix verbose handling, implement --json, purge many renderers
  • Loading branch information
codepope committed Jun 17, 2020
2 parents 713471b + c0c657f commit 0dfbdec
Show file tree
Hide file tree
Showing 32 changed files with 174 additions and 68 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
49 changes: 21 additions & 28 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ type CmdContext struct {
Terminal *terminal.Terminal
WorkingDir string
ConfigFile string
Verbose bool
AppName string
AppConfig *flyctl.AppConfig
}
Expand All @@ -165,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 @@ -192,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 @@ -207,13 +208,15 @@ func (ctx *CmdContext) render(out io.Writer, views ...PresenterOption) error {
return nil
}

// RenderView - render a view through the context to the terminal
func (ctx *CmdContext) RenderView(views ...PresenterOption) (err error) {
return ctx.render(ctx.Terminal, views...)
}
// 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
}
}

// RenderViewW - render a view to a Writer
func (ctx *CmdContext) RenderViewW(w io.Writer, views ...PresenterOption) error {
return ctx.render(w, views...)
}

Expand Down Expand Up @@ -331,13 +334,6 @@ func requireAppName(cmd *Command) Initializer {
Default: defaultConfigFilePath,
EnvName: "FLY_APP_CONFIG",
})
cmd.AddBoolFlag(BoolFlagOpts{
Name: "verbose",
Shorthand: "v",
Description: "Use verbose output where available",
Default: false,
EnvName: "FLY_APP_VERBOSE",
})

return Initializer{
Setup: func(ctx *CmdContext) error {
Expand Down Expand Up @@ -379,9 +375,6 @@ func requireAppName(cmd *Command) Initializer {
ctx.AppName = ctx.AppConfig.AppName
}

verbose := ctx.Config.GetBool("verbose")
ctx.Verbose = verbose

return nil
},
PreRun: func(ctx *CmdContext) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func watchDeployment(ctx context.Context, cc *CmdContext) error {
for alloc := range x {
count++
fmt.Fprintf(cc.Out, "\n Failure #%d\n", count)
err := cc.RenderViewW(p,
err := cc.Frender(p,
PresenterOption{
Title: "Allocation",
Presentable: &presenters.Allocations{
Expand Down
4 changes: 2 additions & 2 deletions cmd/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func runIPAddressesList(ctx *CmdContext) error {
return err
}

return ctx.RenderView(PresenterOption{
return ctx.Frender(ctx.Out, PresenterOption{
Presentable: &presenters.IPAddresses{IPAddresses: ipAddresses},
})
}
Expand All @@ -66,7 +66,7 @@ func runAllocateIPAddress(ctx *CmdContext, addrType string) error {
return err
}

return ctx.RenderView(PresenterOption{
return ctx.Frender(ctx.Out, PresenterOption{
Presentable: &presenters.IPAddresses{IPAddresses: []api.IPAddress{*ipAddress}},
})
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func monitorDeployment(ctx context.Context, cc *CmdContext) error {
monitor.DeploymentUpdated = func(d *api.DeploymentStatus, updatedAllocs []*api.AllocationStatus) error {
fmt.Fprintln(cc.Out, presenters.FormatDeploymemntAllocSummary(d))

if cc.Verbose {
if cc.GlobalConfig.GetBool("verbose") {
for _, alloc := range updatedAllocs {
fmt.Fprintln(cc.Out, presenters.FormatAllocSummary(alloc))
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func monitorDeployment(ctx context.Context, cc *CmdContext) error {
for alloc := range x {
count++
fmt.Fprintf(cc.Out, "\n Failure #%d\n", count)
err := cc.RenderViewW(p,
err := cc.Frender(p,
PresenterOption{
Title: "Allocation",
Presentable: &presenters.Allocations{
Expand Down
8 changes: 6 additions & 2 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,7 +36,9 @@ func runPlatformRegions(ctx *CmdContext) error {
return err
}

return ctx.RenderView(PresenterOption{
fmt.Println(ctx.GlobalConfig.GetBool(flyctl.ConfigJSONOutput))

return ctx.Frender(ctx.Out, PresenterOption{
Presentable: &presenters.Regions{Regions: regions},
})
}
Expand All @@ -45,7 +49,7 @@ func runPlatformVMSizes(ctx *CmdContext) error {
return err
}

return ctx.RenderView(PresenterOption{
return ctx.Frender(ctx.Out, PresenterOption{
Presentable: &presenters.VMSizes{VMSizes: sizes},
})
}
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

0 comments on commit 0dfbdec

Please sign in to comment.