Skip to content

Commit

Permalink
reusable output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldwan committed Aug 7, 2019
1 parent 298ca69 commit dd8f4dd
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 107 deletions.
36 changes: 36 additions & 0 deletions api/client.go
Expand Up @@ -212,3 +212,39 @@ func (client *Client) DeployImage(appName, imageTag string) (*Deployment, error)

return &data.DeployImage.Deployment, nil
}

func (c *Client) GetAppReleases(appName string, limit int) ([]Release, error) {
query := `
query ($appName: String!, $limit: Int!) {
app(name: $appName) {
releases(first: $limit) {
nodes {
id
version
reason
description
user {
id
email
name
}
createdAt
}
}
}
}
`

req := c.NewRequest(query)

req.Var("appName", appName)
req.Var("limit", limit)

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

return data.App.Releases.Nodes, nil

}
76 changes: 45 additions & 31 deletions api/types.go
@@ -1,5 +1,7 @@
package api

import "time"

type Query struct {
Apps struct {
Nodes []App
Expand Down Expand Up @@ -29,46 +31,49 @@ type Query struct {
}

type App struct {
ID string `json:"id"`
Name string `json:"name"`
Runtime string `json:"runtime"`
Status string `json:"status"`
Version int `json:"version"`
AppURL string `json:"appUrl"`
Organization Organization `json:"organization"`
Services []Service `json:"services"`
Secrets []string `json:"secrets"`
ID string
Name string
Runtime string
Status string
Version int
AppURL string
Organization Organization
Services []Service
Secrets []string
Deployments struct {
Nodes []Deployment
}
Releases struct {
Nodes []Release
}
}

type Organization struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
ID string
Name string
Slug string
}

type Service struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Allications []Allocation `json:"allocations"`
ID string
Name string
Status string
Allications []Allocation
}

type Allocation struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Region string `json:"region"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
ID string
Name string
Status string
Region string
CreatedAt string
UpdatedAt string
}

type User struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
ID string
Name string
Email string
}

type Deployment struct {
Expand All @@ -89,18 +94,18 @@ type Deployment struct {
}

type SetSecretsInput struct {
AppID string `json:"appId"`
Secrets []SecretInput `json:"secrets"`
AppID string
Secrets []SecretInput
}

type SecretInput struct {
Key string `json:"key"`
Value string `json:"value"`
Key string
Value string
}

type UnsetSecretsInput struct {
AppID string `json:"appId"`
Keys []string `json:"keys"`
AppID string
Keys []string
}

type CreateAppInput struct {
Expand All @@ -115,3 +120,12 @@ type LogEntry struct {
Region string
}
}

type Release struct {
ID string
Version int
Reason string
Description string
User User
CreatedAt time.Time
}
64 changes: 0 additions & 64 deletions cmd/appDeployments.go

This file was deleted.

71 changes: 71 additions & 0 deletions cmd/appReleases.go
@@ -0,0 +1,71 @@
package cmd

import (
"os"

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

func newAppReleasesListCommand() *Command {
return BuildCommand(runAppReleasesList, "releases", "list app releases", os.Stdout, true, requireAppName)
}

func runAppReleasesList(ctx *CmdContext) error {
releases, err := ctx.FlyClient.GetAppReleases(ctx.AppName(), 25)
if err != nil {
return err
}

return ctx.Render(&presenters.ReleasePresenter{Releases: releases})

// query := `
// query ($appName: String!) {
// app(id: $appName) {
// deployments {
// nodes {
// id
// number
// status
// inProgress
// currentPhase
// reason
// description
// user {
// email
// }
// createdAt
// updatedAt
// }
// }
// }
// }
// `

// req := ctx.FlyClient.NewRequest(query)

// req.Var("appName", ctx.AppName())
// req.Var("limit", 25)

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

// table := tablewriter.NewWriter(os.Stdout)
// table.SetHeader([]string{"#", "Status", "Reason", "Description", "User", "Created"})

// for _, deployment := range data.App.Deployments.Nodes {
// table.Append([]string{
// strconv.Itoa(deployment.Number),
// deployment.Status,
// deployment.Reason,
// deployment.Description,
// deployment.User.Email,
// deployment.CreatedAt,
// })
// }

// table.Render()

// return nil
}
13 changes: 2 additions & 11 deletions cmd/apps.go
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"os"

"github.com/olekukonko/tablewriter"
"github.com/superfly/flyctl/cmd/presenters"
)

func newAppListCommand() *Command {
Expand Down Expand Up @@ -33,14 +33,5 @@ func runAppsList(ctx *CmdContext) error {
return err
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Owner", "Runtime"})

for _, app := range data.Apps.Nodes {
table.Append([]string{app.Name, app.Organization.Slug, app.Runtime})
}

table.Render()

return nil
return ctx.Render(&presenters.AppsPresenter{Apps: data.Apps.Nodes})
}
10 changes: 10 additions & 0 deletions cmd/command.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/flyctl"
)

Expand Down Expand Up @@ -71,6 +72,15 @@ func (ctx *CmdContext) AppName() string {
return ""
}

func (ctx *CmdContext) Render(presentable presenters.Presentable) error {
presenter := &presenters.Presenter{
Item: presentable,
Out: os.Stdout,
}

return presenter.Render()
}

func newCmdContext(ns string, out io.Writer, args []string, initClient bool, initApp bool) (*CmdContext, error) {
ctx := &CmdContext{
NS: ns,
Expand Down
33 changes: 33 additions & 0 deletions cmd/presenters/app.go
@@ -0,0 +1,33 @@
package presenters

import "github.com/superfly/flyctl/api"

type AppsPresenter struct {
Apps []api.App
}

func (p *AppsPresenter) FieldNames() []string {
return []string{"Name", "Owner", "Runtime"}
}

func (p *AppsPresenter) FieldMap() map[string]string {
return map[string]string{
"Name": "Name",
"Owner": "Owner",
"Runtime": "Runtime",
}
}

func (p *AppsPresenter) Records() []map[string]string {
out := []map[string]string{}

for _, app := range p.Apps {
out = append(out, map[string]string{
"Name": app.Name,
"Owner": app.Organization.Slug,
"Runtime": app.Runtime,
})
}

return out
}

0 comments on commit dd8f4dd

Please sign in to comment.