Skip to content

Commit

Permalink
All commands should respond to --json now with something JSON like
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Jun 17, 2020
1 parent f5ef6a9 commit 4110b20
Show file tree
Hide file tree
Showing 35 changed files with 559 additions and 477 deletions.
1 change: 0 additions & 1 deletion api/resource_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func (client *Client) GetConfig(appName string) (*AppConfig, error) {
if err != nil {
return nil, err
}

return &data.App.Config, nil
}

Expand Down
14 changes: 7 additions & 7 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type App struct {
Certificates struct {
Nodes []AppCertificate
}
Certificate AppCertificate
Certificate AppCertificate `json:"omitempty"`
Services []Service
Config AppConfig
ParseConfig AppConfig
Expand Down Expand Up @@ -298,12 +298,12 @@ type DeployImageInput struct {

type Service struct {
Description string `json:"description"`
Protocol string `json:"protocol"`
InternalPort int `json:"internalPort"`
Ports []PortHandler `json:"ports"`
Checks []Check `json:"checks"`
SoftConcurrency int `json:"softConcurrency"`
HardConcurrency int `json:"hardConcurrency"`
Protocol string `json:"protocol,omitempty"`
InternalPort int `json:"internalPort,omitempty"`
Ports []PortHandler `json:"ports,omitempty"`
Checks []Check `json:"checks,omitempty"`
SoftConcurrency int `json:"softConcurrency,omitempty"`
HardConcurrency int `json:"hardConcurrency,omitempty"`
}

type PortHandler struct {
Expand Down
12 changes: 5 additions & 7 deletions cmd/appInfo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cmd

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

"github.com/superfly/flyctl/docstrings"
Expand All @@ -22,25 +20,25 @@ func runAppInfo(ctx *cmdctx.CmdContext) error {
return err
}

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

// For JSON, everything is included in the previous render, for humans, we need to do some formatting
if !ctx.GlobalConfig.GetBool(flyctl.ConfigJSONOutput) {
err = ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.Services{Services: app.Services}, Title: "Services"})
if !ctx.OutputJSON() {
err = ctx.Frender(cmdctx.PresenterOption{Presentable: &presenters.Services{Services: app.Services}, Title: "Services"})
if err != nil {
return err
}

err = ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.IPAddresses{IPAddresses: app.IPAddresses.Nodes}, Title: "IP Adresses"})
err = ctx.Frender(cmdctx.PresenterOption{Presentable: &presenters.IPAddresses{IPAddresses: app.IPAddresses.Nodes}, Title: "IP Adresses"})
if err != nil {
return err
}

if !app.Deployed {
fmt.Fprintln(ctx.Out, `App has not been deployed yet. Try running "flyctl deploy --image flyio/hellofly"`)
ctx.Status("flyctl", `App has not been deployed yet. Try running "flyctl deploy --image flyio/hellofly"`)
}
}
return nil
Expand Down
42 changes: 21 additions & 21 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ func runDestroyApp(ctx *cmdctx.CmdContext) error {
return nil
}

func runAppsCreate(ctx *cmdctx.CmdContext) error {
func runAppsCreate(commandContext *cmdctx.CmdContext) error {
var appName = ""
var internalPort = 0

if len(ctx.Args) > 0 {
appName = ctx.Args[0]
if len(commandContext.Args) > 0 {
appName = commandContext.Args[0]
}

configPort, _ := ctx.Config.GetString("port")
configPort, _ := commandContext.Config.GetString("port")

// If ports set, validate
if configPort != "" {
Expand All @@ -173,11 +173,11 @@ func runAppsCreate(ctx *cmdctx.CmdContext) error {

newAppConfig := flyctl.NewAppConfig()

if builder, _ := ctx.Config.GetString("builder"); builder != "" {
if builder, _ := commandContext.Config.GetString("builder"); builder != "" {
newAppConfig.Build = &flyctl.Build{Builder: builder}
}

name, _ := ctx.Config.GetString("name")
name, _ := commandContext.Config.GetString("name")

if name != "" && appName != "" {
return fmt.Errorf(`two app names specified %s and %s. Select and specify only one`, appName, name)
Expand All @@ -200,8 +200,8 @@ func runAppsCreate(ctx *cmdctx.CmdContext) error {
fmt.Printf("Selected App Name: %s\n", name)
}

targetOrgSlug, _ := ctx.Config.GetString("org")
org, err := selectOrganization(ctx.Client.API(), targetOrgSlug)
targetOrgSlug, _ := commandContext.Config.GetString("org")
org, err := selectOrganization(commandContext.Client.API(), targetOrgSlug)

switch {
case isInterrupt(err):
Expand All @@ -210,7 +210,7 @@ func runAppsCreate(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Error setting organization: %s", err)
}

app, err := ctx.Client.API().CreateApp(name, org.ID)
app, err := commandContext.Client.API().CreateApp(name, org.ID)
if err != nil {
return err
}
Expand All @@ -221,27 +221,27 @@ func runAppsCreate(ctx *cmdctx.CmdContext) error {
newAppConfig.SetInternalPort(internalPort)
}

err = ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "New app created"})
err = commandContext.Frender(cmdctx.PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "New app created"})
if err != nil {
return err
}

if ctx.ConfigFile == "" {
newCfgFile, err := flyctl.ResolveConfigFileFromPath(ctx.WorkingDir)
if commandContext.ConfigFile == "" {
newCfgFile, err := flyctl.ResolveConfigFileFromPath(commandContext.WorkingDir)
if err != nil {
return err
}
ctx.ConfigFile = newCfgFile
commandContext.ConfigFile = newCfgFile
}

return writeAppConfig(ctx.ConfigFile, newAppConfig)
return writeAppConfig(commandContext.ConfigFile, newAppConfig)
}

func runAppsMove(ctx *cmdctx.CmdContext) error {
appName := ctx.Args[0]
func runAppsMove(commandContext *cmdctx.CmdContext) error {
appName := commandContext.Args[0]

targetOrgSlug, _ := ctx.Config.GetString("org")
org, err := selectOrganization(ctx.Client.API(), targetOrgSlug)
targetOrgSlug, _ := commandContext.Config.GetString("org")
org, err := selectOrganization(commandContext.Client.API(), targetOrgSlug)

switch {
case isInterrupt(err):
Expand All @@ -250,12 +250,12 @@ func runAppsMove(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Error setting organization: %s", err)
}

app, err := ctx.Client.API().GetApp(appName)
app, err := commandContext.Client.API().GetApp(appName)
if err != nil {
return errors.Wrap(err, "Error fetching app")
}

if !ctx.Config.GetBool("yes") {
if !commandContext.Config.GetBool("yes") {
fmt.Println(aurora.Red("Are you sure you want to move this app?"))

confirm := false
Expand All @@ -269,7 +269,7 @@ func runAppsMove(ctx *cmdctx.CmdContext) error {
}
}

app, err = ctx.Client.API().MoveApp(appName, org.ID)
app, err = commandContext.Client.API().MoveApp(appName, org.ID)
if err != nil {
return errors.WithMessage(err, "Failed to move app")
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func runLogout(ctx *cmdctx.CmdContext) error {
func runAuthToken(ctx *cmdctx.CmdContext) error {
token, _ := ctx.GlobalConfig.GetString(flyctl.ConfigAPIToken)

if ctx.OutputJSON() {
ctx.WriteJSON(map[string]string{"flyctlAuthToken": token})
return nil
}
fmt.Fprintln(ctx.Out, token)

return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func newBuildsCommand() *Command {
return cmd
}

func runListBuilds(ctx *cmdctx.CmdContext) error {
builds, err := ctx.Client.API().ListBuilds(ctx.AppName)
func runListBuilds(commandContext *cmdctx.CmdContext) error {
builds, err := commandContext.Client.API().ListBuilds(commandContext.AppName)
if err != nil {
return err
}

return ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.Builds{Builds: builds}})
return commandContext.Frender(cmdctx.PresenterOption{Presentable: &presenters.Builds{Builds: builds}})
}

func runBuildLogs(cc *cmdctx.CmdContext) error {
Expand Down
43 changes: 21 additions & 22 deletions cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/superfly/flyctl/docstrings"

"github.com/AlecAivazis/survey/v2"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/cmd/presenters"
)
Expand Down Expand Up @@ -47,55 +46,55 @@ func newCertificatesCommand() *Command {
return cmd
}

func runCertsList(ctx *cmdctx.CmdContext) error {
certs, err := ctx.Client.API().GetAppCertificates(ctx.AppName)
func runCertsList(commandContext *cmdctx.CmdContext) error {
certs, err := commandContext.Client.API().GetAppCertificates(commandContext.AppName)
if err != nil {
return err
}

return ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.Certificates{Certificates: certs}})
return commandContext.Frender(cmdctx.PresenterOption{Presentable: &presenters.Certificates{Certificates: certs}})
}

func runCertShow(ctx *cmdctx.CmdContext) error {
hostname := ctx.Args[0]
func runCertShow(commmandContext *cmdctx.CmdContext) error {
hostname := commmandContext.Args[0]

cert, err := ctx.Client.API().GetAppCertificate(ctx.AppName, hostname)
cert, err := commmandContext.Client.API().GetAppCertificate(commmandContext.AppName, hostname)
if err != nil {
return err
}

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

func runCertCheck(ctx *cmdctx.CmdContext) error {
hostname := ctx.Args[0]
func runCertCheck(commandContext *cmdctx.CmdContext) error {
hostname := commandContext.Args[0]

cert, err := ctx.Client.API().CheckAppCertificate(ctx.AppName, hostname)
cert, err := commandContext.Client.API().CheckAppCertificate(commandContext.AppName, hostname)
if err != nil {
return err
}

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

func runCertAdd(ctx *cmdctx.CmdContext) error {
hostname := ctx.Args[0]
func runCertAdd(commandContext *cmdctx.CmdContext) error {
hostname := commandContext.Args[0]

cert, err := ctx.Client.API().AddCertificate(ctx.AppName, hostname)
cert, err := commandContext.Client.API().AddCertificate(commandContext.AppName, hostname)
if err != nil {
return err
}

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

func runCertDelete(ctx *cmdctx.CmdContext) error {
hostname := ctx.Args[0]
func runCertDelete(commandContext *cmdctx.CmdContext) error {
hostname := commandContext.Args[0]

if !ctx.Config.GetBool("yes") {
if !commandContext.Config.GetBool("yes") {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Remove certificate %s from app %s?", hostname, ctx.AppName),
Message: fmt.Sprintf("Remove certificate %s from app %s?", hostname, commandContext.AppName),
}
survey.AskOne(prompt, &confirm)

Expand All @@ -104,12 +103,12 @@ func runCertDelete(ctx *cmdctx.CmdContext) error {
}
}

cert, err := ctx.Client.API().DeleteCertificate(ctx.AppName, hostname)
cert, err := commandContext.Client.API().DeleteCertificate(commandContext.AppName, hostname)
if err != nil {
return err
}

fmt.Fprintf(ctx.Out, "Certificate %s deleted from app %s\n", aurora.Bold(cert.Certificate.Hostname), aurora.Bold(cert.App.Name))
commandContext.Statusf("flyctl", cmdctx.SINFO, "Certificate %s deleted from app %s\n", cert.Certificate.Hostname, cert.App.Name)

return nil
}
44 changes: 0 additions & 44 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/client"
Expand Down Expand Up @@ -138,49 +137,6 @@ func (c *Command) AddStringSliceFlag(options StringSliceFlagOpts) {
}
}

// Render - Render a presentable structure via the context
func Render(presentable presenters.Presentable) error {
presenter := &presenters.Presenter{
Item: presentable,
Out: os.Stdout,
}

return presenter.Render()
}

// PresenterOption - options for RenderEx, RenderView, render etc...
type PresenterOption struct {
Presentable presenters.Presentable
AsJSON bool
Vertical bool
HideHeader bool
Title string
}

//func (ctx *cmdctx.CmdContext) render(out io.Writer, views ...PresenterOption) error {
// for _, v := range views {
// presenter := &presenters.Presenter{
// Item: v.Presentable,
// Out: out,
// Opts: presenters.Options{
// Vertical: v.Vertical,
// HideHeader: v.HideHeader,
// AsJSON: v.AsJSON,
// },
// }
//
// if v.Title != "" && !v.AsJSON {
// fmt.Fprintln(out, aurora.Bold(v.Title))
// }
//
// if err := presenter.Render(); err != nil {
// return err
// }
// }
//
// return nil
//}

// Initializer - Retains Setup and PreRun functions
type Initializer struct {
Setup InitializerFn
Expand Down

0 comments on commit 4110b20

Please sign in to comment.