Skip to content

Commit

Permalink
Reworked everything with a new standalone cmdctx package to bridge to…
Browse files Browse the repository at this point in the history
… the docker package
  • Loading branch information
codepope committed Jun 12, 2020
1 parent ad6f08a commit f5ef6a9
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 226 deletions.
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 `json:"code"`
Name string `json:"name"`
Latitude float32 `json:"latitude,omitempty"`
Longitude float32 `json:"longitude,omitempty"`
Code string
Name string
Latitude float32 `json:"omitempty"`
Longitude float32 `json:"omitempty"`
}

type AutoscalingConfig struct {
Expand Down
9 changes: 5 additions & 4 deletions cmd/appInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

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

Expand All @@ -15,25 +16,25 @@ func newAppInfoCommand() *Command {
return BuildCommand(nil, runAppInfo, ks.Usage, ks.Short, ks.Long, os.Stdout, requireSession, requireAppName)
}

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

err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "App"})
err = ctx.Frender(ctx.Out, 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, PresenterOption{Presentable: &presenters.Services{Services: app.Services}, Title: "Services"})
err = ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.Services{Services: app.Services}, Title: "Services"})
if err != nil {
return err
}

err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.IPAddresses{IPAddresses: app.IPAddresses.Nodes}, Title: "IP Adresses"})
err = ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.IPAddresses{IPAddresses: app.IPAddresses.Nodes}, Title: "IP Adresses"})
if err != nil {
return err
}
Expand Down
19 changes: 9 additions & 10 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

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

Expand Down Expand Up @@ -86,7 +87,7 @@ func newAppListCommand() *Command {
return cmd
}

func runAppsList(ctx *CmdContext) error {
func runAppsList(ctx *cmdctx.CmdContext) error {
apps, err := ctx.Client.API().GetApps()
if err != nil {
return err
Expand All @@ -95,7 +96,7 @@ func runAppsList(ctx *CmdContext) error {
return ctx.Render(&presenters.Apps{Apps: apps})
}

func runAppsPause(ctx *CmdContext) error {
func runAppsPause(ctx *cmdctx.CmdContext) error {
app, err := ctx.Client.API().PauseApp(ctx.AppName)
if err != nil {
return err
Expand All @@ -104,7 +105,7 @@ func runAppsPause(ctx *CmdContext) error {
return nil
}

func runAppsResume(ctx *CmdContext) error {
func runAppsResume(ctx *cmdctx.CmdContext) error {
app, err := ctx.Client.API().ResumeApp(ctx.AppName)
if err != nil {
return err
Expand All @@ -114,7 +115,7 @@ func runAppsResume(ctx *CmdContext) error {
return nil
}

func runAppsRestart(ctx *CmdContext) error {
func runAppsRestart(ctx *cmdctx.CmdContext) error {
app, err := ctx.Client.API().RestartApp(ctx.AppName)
if err != nil {
return err
Expand All @@ -124,7 +125,7 @@ func runAppsRestart(ctx *CmdContext) error {
return nil
}

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

if !ctx.Config.GetBool("yes") {
Expand All @@ -150,7 +151,7 @@ func runDestroyApp(ctx *CmdContext) error {
return nil
}

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

Expand Down Expand Up @@ -220,9 +221,7 @@ func runAppsCreate(ctx *CmdContext) error {
newAppConfig.SetInternalPort(internalPort)
}

fmt.Println("New app created")

err = ctx.Frender(ctx.Out, PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true})
err = ctx.Frender(ctx.Out, cmdctx.PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "New app created"})
if err != nil {
return err
}
Expand All @@ -238,7 +237,7 @@ func runAppsCreate(ctx *CmdContext) error {
return writeAppConfig(ctx.ConfigFile, newAppConfig)
}

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

targetOrgSlug, _ := ctx.Config.GetString("org")
Expand Down
31 changes: 16 additions & 15 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cmd
import (
"errors"
"fmt"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docker"
"os"
"time"

"github.com/superfly/flyctl/docker"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/internal/client"

Expand Down Expand Up @@ -72,7 +73,7 @@ func newAuthCommand() *Command {
return cmd
}

func runWhoami(ctx *CmdContext) error {
func runWhoami(ctx *cmdctx.CmdContext) error {
user, err := ctx.Client.API().GetCurrentUser()
if err != nil {
return err
Expand All @@ -81,7 +82,7 @@ func runWhoami(ctx *CmdContext) error {
return nil
}

func runLogin(ctx *CmdContext) error {
func runLogin(ctx *cmdctx.CmdContext) error {
if ctx.Config.GetBool("interactive") {
return runInteractiveLogin(ctx)
}
Expand All @@ -98,19 +99,19 @@ func runLogin(ctx *CmdContext) error {
return runWebLogin(ctx, false)
}

func runSignup(ctx *CmdContext) error {
func runSignup(ctx *cmdctx.CmdContext) error {
return runWebLogin(ctx, true)
}

func runWebLogin(ctx *CmdContext, signup bool) error {
func runWebLogin(ctx *cmdctx.CmdContext, signup bool) error {
name, _ := os.Hostname()

cliAuth, err := api.StartCLISessionWebAuth(name, signup)
if err != nil {
return err
}

fmt.Println("Opening browser to url", aurora.Bold(cliAuth.AuthURL))
fmt.Fprintln(ctx.Out, "Opening browser to url", aurora.Bold(cliAuth.AuthURL))

if err := open.Run(cliAuth.AuthURL); err != nil {
terminal.Error("Error opening browser. Copy the above url into a browser and continue")
Expand Down Expand Up @@ -170,7 +171,7 @@ func waitForCLISession(id string) <-chan api.CLISessionAuth {
return done
}

func runInteractiveLogin(ctx *CmdContext) error {
func runInteractiveLogin(ctx *cmdctx.CmdContext) error {
email, _ := ctx.Config.GetString("email")
if email == "" {
prompt := &survey.Input{
Expand Down Expand Up @@ -218,7 +219,7 @@ func runInteractiveLogin(ctx *CmdContext) error {
return flyctl.SaveConfig()
}

func runLogout(ctx *CmdContext) error {
func runLogout(ctx *cmdctx.CmdContext) error {
viper.Set(flyctl.ConfigAPIToken, "")

if err := flyctl.SaveConfig(); err != nil {
Expand All @@ -230,29 +231,29 @@ func runLogout(ctx *CmdContext) error {
return nil
}

func runAuthToken(ctx *CmdContext) error {
func runAuthToken(ctx *cmdctx.CmdContext) error {
token, _ := ctx.GlobalConfig.GetString(flyctl.ConfigAPIToken)

fmt.Println(token)
fmt.Fprintln(ctx.Out, token)

return nil
}

func runAuthDocker(cc *CmdContext) error {
ctx := createCancellableContext()
func runAuthDocker(ctx *cmdctx.CmdContext) error {
cc := createCancellableContext()

dockerClient, err := docker.NewDockerClient()
if err != nil {
return fmt.Errorf("Docker daemon unavailable: %s", err)
}

token, _ := cc.GlobalConfig.GetString(flyctl.ConfigAPIToken)
token, _ := ctx.GlobalConfig.GetString(flyctl.ConfigAPIToken)
authConfig := docker.RegistryAuth(token)
if _, err := dockerClient.Client().RegistryLogin(ctx, authConfig); err != nil {
if _, err := dockerClient.Client().RegistryLogin(cc, authConfig); err != nil {
return err
}

fmt.Println("Authentication successful. You can now tag and push images to registry.fly.io/{your-app}")
fmt.Fprintln(ctx.Out, "Authentication successful. You can now tag and push images to registry.fly.io/{your-app}")

return nil
}
8 changes: 5 additions & 3 deletions cmd/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

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

"github.com/superfly/flyctl/docstrings"
Expand Down Expand Up @@ -31,21 +32,22 @@ func newBuildsCommand() *Command {
return cmd
}

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

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

func runBuildLogs(cc *CmdContext) error {
func runBuildLogs(cc *cmdctx.CmdContext) error {
ctx := createCancellableContext()
buildID := cc.Args[0]

logs := builds.NewBuildMonitor(buildID, cc.Client.API())

// TODO: Need to consider what is appropriate to output with JSON set
for line := range logs.Logs(ctx) {
fmt.Println(line)
}
Expand Down
21 changes: 11 additions & 10 deletions cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

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

"github.com/superfly/flyctl/docstrings"
Expand Down Expand Up @@ -46,49 +47,49 @@ func newCertificatesCommand() *Command {
return cmd
}

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

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

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

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

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

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

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

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

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

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

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

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

if !ctx.Config.GetBool("yes") {
Expand All @@ -108,7 +109,7 @@ func runCertDelete(ctx *CmdContext) error {
return err
}

fmt.Printf("Certificate %s deleted from app %s\n", aurora.Bold(cert.Certificate.Hostname), aurora.Bold(cert.App.Name))
fmt.Fprintf(ctx.Out, "Certificate %s deleted from app %s\n", aurora.Bold(cert.Certificate.Hostname), aurora.Bold(cert.App.Name))

return nil
}

0 comments on commit f5ef6a9

Please sign in to comment.