Skip to content

Commit

Permalink
Merge pull request #166 from superfly/jsonsnagging
Browse files Browse the repository at this point in the history
JSON Snagging
  • Loading branch information
codepope committed Jun 24, 2020
2 parents 5b0647f + 8a9dfc2 commit 988a905
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 28 deletions.
11 changes: 11 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import (
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strings"

"github.com/machinebox/graphql"
)

var baseURL string
var errorLog bool

func SetBaseURL(url string) {
baseURL = url
}

func SetErrorLog(log bool) {
errorLog = log
}

type Client struct {
httpClient *http.Client
client *graphql.Client
Expand Down Expand Up @@ -58,6 +64,11 @@ func (c *Client) RunWithContext(ctx context.Context, req *graphql.Request) (Quer
if err != nil && strings.HasPrefix(err.Error(), "graphql: ") {
return resp, errors.New(strings.TrimPrefix(err.Error(), "graphql: "))
}

if resp.Errors != nil && errorLog {
fmt.Fprintf(os.Stderr, "Error: %+v\n", resp.Errors)
}

return resp, err
}

Expand Down
46 changes: 46 additions & 0 deletions api/resource_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,52 @@ func (client *Client) GetApp(appName string) (*App, error) {
return &data.App, nil
}

func (client *Client) GetAppCompact(appName string) (*AppCompact, error) {
query := `
query ($appName: String!) {
appcompact:app(name: $appName) {
id
name
hostname
deployed
status
version
appUrl
organization {
slug
}
services {
description
protocol
internalPort
ports {
port
handlers
}
}
ipAddresses {
nodes {
id
address
type
createdAt
}
}
}
}
`

req := client.NewRequest(query)
req.Var("appName", appName)

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

return &data.AppCompact, nil
}

func (client *Client) CreateApp(name string, orgId string) (*App, error) {
query := `
mutation($input: CreateAppInput!) {
Expand Down
2 changes: 1 addition & 1 deletion api/resource_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Client) GetDeploymentStatus(appName string, deploymentID string) (*Depl
desiredStatus
version
healthy
failed
failed
canary
restarts
checks {
Expand Down
6 changes: 3 additions & 3 deletions api/resource_monitoring.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package api

func (c *Client) GetAppStatus(appName string, showCompleted bool) (*App, error) {
func (c *Client) GetAppStatus(appName string, showCompleted bool) (*AppStatus, error) {
query := `
query($appName: String!, $showCompleted: Boolean!) {
app(name: $appName) {
appstatus:app(name: $appName) {
id
name
deployed
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *Client) GetAppStatus(appName string, showCompleted bool) (*App, error)
return nil, err
}

return &data.App, nil
return &data.AppStatus, nil
}

func (c *Client) GetAllocationStatus(appName string, allocID string, logLimit int) (*AllocationStatus, error) {
Expand Down
53 changes: 52 additions & 1 deletion api/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package api

import "time"
import (
"time"
)

// Query - Master query which encapsulates all possible returned structures
type Query struct {
Errors Errors

Apps struct {
Nodes []App
}
App App
AppCompact AppCompact
AppStatus AppStatus
CurrentUser User
Organizations struct {
Nodes []Organization
Expand Down Expand Up @@ -139,6 +146,35 @@ type App struct {
Regions *[]Region
}

type AppCompact struct {
ID string
Name string
Status string
Deployed bool
Hostname string
AppURL string
Version int
Release *Release
Organization Organization
IPAddresses struct {
Nodes []IPAddress
}
Services []Service
}

type AppStatus struct {
ID string
Name string
Deployed bool
Status string
Hostname string
Version int
AppURL string
Organization Organization
DeploymentStatus *DeploymentStatus
Allocations []*AllocationStatus
}

type AppConfig struct {
Definition Definition
Services []Service
Expand Down Expand Up @@ -464,3 +500,18 @@ type ConfigureRegionsInput struct {
AllowRegions []string `json:"allowRegions"`
DenyRegions []string `json:"denyRegions"`
}

type Errors []Error

type Error struct {
Message string
Path []string
Extensions Extensions
}

type Extensions struct {
Code string
ServiceName string
Query string
Variables map[string]string
}
7 changes: 4 additions & 3 deletions cmd/appInfo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

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

"github.com/superfly/flyctl/cmdctx"

"github.com/superfly/flyctl/docstrings"

"github.com/superfly/flyctl/cmd/presenters"
Expand All @@ -15,12 +16,12 @@ func newAppInfoCommand() *Command {
}

func runAppInfo(ctx *cmdctx.CmdContext) error {
app, err := ctx.Client.API().GetApp(ctx.AppName)
app, err := ctx.Client.API().GetAppCompact(ctx.AppName)
if err != nil {
return err
}

err = ctx.Frender(cmdctx.PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "App"})
err = ctx.Frender(cmdctx.PresenterOption{Presentable: &presenters.AppCompact{AppCompact: *app}, HideHeader: true, Vertical: true, Title: "App"})
if err != nil {
return err
}
Expand Down
38 changes: 35 additions & 3 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

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

"github.com/superfly/flyctl/cmdctx"

"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -15,6 +18,8 @@ import (
"github.com/superfly/flyctl/flyctl"
)

//TODO: Move all output to status styled begin/done updates

func newAppListCommand() *Command {

appsStrings := docstrings.Get("apps")
Expand Down Expand Up @@ -97,11 +102,35 @@ func runAppsList(ctx *cmdctx.CmdContext) error {
}

func runAppsPause(ctx *cmdctx.CmdContext) error {
app, err := ctx.Client.API().PauseApp(ctx.AppName)
_, err := ctx.Client.API().PauseApp(ctx.AppName)
if err != nil {
return err
}
fmt.Printf("%s is now %s\n", app.Name, app.Status)

appstatus, err := ctx.Client.API().GetAppStatus(ctx.AppName, false)

fmt.Printf("%s is now %s\n", appstatus.Name, appstatus.Status)

allocount := len(appstatus.Allocations)

s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = fmt.Sprintf("Pausing %s with %d instances to stop ", appstatus.Name, allocount)
s.Start()

for allocount > 0 {
plural := ""
if allocount > 1 {
plural = "s"
}
s.Prefix = fmt.Sprintf("Pausing %s with %d instance%s to stop ", appstatus.Name, allocount, plural)
appstatus, err = ctx.Client.API().GetAppStatus(ctx.AppName, false)
allocount = len(appstatus.Allocations)
}

s.FinalMSG = fmt.Sprintf("Pause complete - %s is now paused with no running instances\n", appstatus.Name)
s.Stop()

return nil
}

Expand All @@ -111,7 +140,10 @@ func runAppsResume(ctx *cmdctx.CmdContext) error {
return err
}

app, err = ctx.Client.API().GetApp(ctx.AppName)

fmt.Printf("%s is now %s\n", app.Name, app.Status)

return nil
}

Expand Down
18 changes: 14 additions & 4 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ func runDeploy(commandContext *cmdctx.CmdContext) error {
}
}

appcheck, err := commandContext.Client.API().GetApp(commandContext.AppName)

if err != nil {
return err
}

if appcheck.Status == "dead" {
return fmt.Errorf("app %s is currently paused - resume it with flyctl apps resume", commandContext.AppName)
}

var strategy = docker.DefaultDeploymentStrategy
if val, _ := commandContext.Config.GetString("strategy"); val != "" {
strategy, err = docker.ParseDeploymentStrategy(val)
Expand All @@ -105,7 +115,7 @@ func runDeploy(commandContext *cmdctx.CmdContext) error {

if imageRef, _ := commandContext.Config.GetString("image"); imageRef != "" {
// image specified, resolve it, tagging and pushing if docker+local
// fmt.Printf("Deploying image: %s\n", imageRef)

commandContext.Statusf("flyctl", cmdctx.SINFO, "Deploying image: %s\n", imageRef)

img, err := op.ResolveImage(ctx, commandContext, imageRef)
Expand Down Expand Up @@ -182,7 +192,6 @@ func runDeploy(commandContext *cmdctx.CmdContext) error {
commandContext.Status("flyctl", cmdctx.SDONE, "Done Pushing Image")

if commandContext.Config.GetBool("build-only") {
//fmt.Printf("Image: %s\n", image.Tag)
commandContext.Statusf("flyctl", cmdctx.SINFO, "Image: %s\n", image.Tag)

return nil
Expand All @@ -204,7 +213,9 @@ func runDeploy(commandContext *cmdctx.CmdContext) error {

buildMonitor := builds.NewBuildMonitor(build.ID, commandContext.Client.API())
for line := range buildMonitor.Logs(ctx) {
fmt.Println(line)
s.Stop()
commandContext.Status("remotebuild", cmdctx.SINFO, line)
s.Start()
}

s.FinalMSG = fmt.Sprintf("Build complete - %s\n", buildMonitor.Status())
Expand Down Expand Up @@ -290,7 +301,6 @@ func watchDeployment(ctx context.Context, commandContext *cmdctx.CmdContext) err
commandContext.Statusf("deploy", cmdctx.SDETAIL, "v%d %s - %s\n", d.Version, d.Status, d.Description)

if len(failedAllocs) > 0 {
fmt.Fprintln(commandContext.Out)
commandContext.Status("flyctl", cmdctx.STITLE, "Failed Allocations")

x := make(chan *api.AllocationStatus)
Expand Down
40 changes: 40 additions & 0 deletions cmd/presenters/appstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package presenters

import (
"strconv"

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

type AppStatus struct {
AppStatus api.AppStatus
}

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

func (p *AppStatus) FieldNames() []string {
return []string{"Name", "Owner", "Version", "Status", "Hostname"}
}

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

info := map[string]string{
"Name": p.AppStatus.Name,
"Owner": p.AppStatus.Organization.Slug,
"Version": strconv.Itoa(p.AppStatus.Version),
"Status": p.AppStatus.Status,
}

if len(p.AppStatus.Hostname) > 0 {
info["Hostname"] = p.AppStatus.Hostname
} else {
info["Hostname"] = "<empty>"
}

out = append(out, info)

return out
}

0 comments on commit 988a905

Please sign in to comment.