Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/command/apps/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flag/completion"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/machine"
)

Expand Down Expand Up @@ -50,8 +50,8 @@ func newRestart() *cobra.Command {

func runRestart(ctx context.Context) error {
var (
appName = flag.FirstArg(ctx)
client = flyutil.ClientFromContext(ctx)
appName = flag.FirstArg(ctx)
flapsClient = flapsutil.ClientFromContext(ctx)
)

if appName == "" {
Expand All @@ -61,24 +61,24 @@ func runRestart(ctx context.Context) error {
}
}

app, err := client.GetAppCompact(ctx, appName)
app, err := flapsClient.GetApp(ctx, appName)
if err != nil {
return err
}

if app.IsPostgresApp() {
if flapsutil.IsPostgresApp(app) {
return fmt.Errorf("postgres apps should use `fly pg restart` instead")
}

ctx, err = BuildContext(ctx, app)
ctx, err = BuildContextForApp(ctx, app)
if err != nil {
return err
}

return runMachinesRestart(ctx, app)
}

func runMachinesRestart(ctx context.Context, app *fly.AppCompact) error {
func runMachinesRestart(ctx context.Context, app *flaps.App) error {
input := &fly.RestartMachineInput{
ForceStop: flag.GetBool(ctx, "force-stop"),
SkipHealthChecks: flag.GetBool(ctx, "skip-health-checks"),
Expand Down
5 changes: 2 additions & 3 deletions internal/command/command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,14 @@ func DetermineMounts(ctx context.Context, appName string, mounts []fly.MachineMo
}

func getUnattachedVolumes(ctx context.Context, appName, regionCode string) (map[string][]fly.Volume, error) {
apiclient := flyutil.ClientFromContext(ctx)
flapsClient := flapsutil.ClientFromContext(ctx)

if regionCode == "" {
region, err := apiclient.GetNearestRegion(ctx)
regionData, err := flapsClient.GetRegions(ctx)
if err != nil {
return nil, err
}
regionCode = region.Code
regionCode = regionData.Nearest
}

volumes, err := flapsClient.GetVolumes(ctx, appName)
Expand Down
5 changes: 3 additions & 2 deletions internal/command/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ func runConsole(ctx context.Context) error {
return fmt.Errorf("failed to get app: %w", err)
}

network, err := apiClient.GetAppNetwork(ctx, app.Name)
flapsApp, err := flapsutil.ClientFromContext(ctx).GetApp(ctx, app.Name)
if err != nil {
return fmt.Errorf("failed to get app network: %w", err)
}
network := flapsutil.NetworkName(flapsApp)

appConfig := appconfig.ConfigFromContext(ctx)
if appConfig == nil {
Expand All @@ -208,7 +209,7 @@ func runConsole(ctx context.Context) error {
defer cleanup()
}

_, dialer, err := agent.BringUpAgent(ctx, apiClient, app, *network, false)
_, dialer, err := agent.BringUpAgent(ctx, apiClient, app, network, false)
if err != nil {
return err
}
Expand Down
19 changes: 9 additions & 10 deletions internal/command/curl/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
Expand Down Expand Up @@ -79,17 +79,16 @@ func run(ctx context.Context) error {
}

func fetchRegionCodes(ctx context.Context) (codes []string, err error) {
client := flyutil.ClientFromContext(ctx)
flapsClient := flapsutil.ClientFromContext(ctx)

var regions []fly.Region
if regions, _, err = client.PlatformRegions(ctx); err != nil {
err = fmt.Errorf("failed retrieving regions: %w", err)

return
} else if len(regions) == 0 {
err = errors.New("no regions could be retrieved")
regionData, err := flapsClient.GetRegions(ctx)
if err != nil {
return nil, fmt.Errorf("failed retrieving regions: %w", err)
}

return
regions := regionData.Regions
if len(regions) == 0 {
return nil, errors.New("no regions could be retrieved")
}

// Filter out deprecated regions
Expand Down
7 changes: 3 additions & 4 deletions internal/command/dashboard/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/iostreams"
)

Expand Down Expand Up @@ -67,13 +67,12 @@ func runDashboardMetrics(ctx context.Context) error {
appName := appconfig.NameFromContext(ctx)

if flag.GetBool(ctx, "grafana") {
client := flyutil.ClientFromContext(ctx)
app, err := client.GetAppBasic(ctx, appName)
app, err := flapsutil.ClientFromContext(ctx).GetApp(ctx, appName)
if err != nil {
return fmt.Errorf("failed to get app info: %w", err)
}

url := fmt.Sprintf("https://fly-metrics.net/d/fly-app/fly-app?orgId=%s&var-app=%s", app.Organization.InternalNumericID, appName)
url := fmt.Sprintf("https://fly-metrics.net/d/fly-app/fly-app?orgId=%d&var-app=%s", app.Organization.InternalNumericID, appName)

return runDashboardOpen(ctx, url)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/dig/dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"

"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/iostreams"

"github.com/superfly/flyctl/internal/appconfig"
Expand Down Expand Up @@ -74,7 +75,7 @@ func run(ctx context.Context) error {
if orgSlug == "" {
appName := appconfig.NameFromContext(ctx)

app, err := client.GetAppBasic(ctx, appName)
app, err := flapsutil.ClientFromContext(ctx).GetApp(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/command/image/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
Expand Down Expand Up @@ -44,19 +45,18 @@ func newShow() *cobra.Command {

func runShow(ctx context.Context) error {
var (
client = flyutil.ClientFromContext(ctx)
appName = appconfig.NameFromContext(ctx)
)

app, err := client.GetAppCompact(ctx, appName)
app, err := flapsutil.ClientFromContext(ctx).GetApp(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}

return showMachineImage(ctx, app)
}

func showMachineImage(ctx context.Context, app *fly.AppCompact) error {
func showMachineImage(ctx context.Context, app *flaps.App) error {
var (
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
Expand Down Expand Up @@ -158,7 +158,7 @@ func showMachineImage(ctx context.Context, app *fly.AppCompact) error {
latest = latestImage
}

if app.IsPostgresApp() {
if flapsutil.IsPostgresApp(app) {
// Abort if we detect a postgres machine running a different major version.
if latest.Tag != latestImage.Tag {
return fmt.Errorf("major version mismatch detected")
Expand Down
9 changes: 4 additions & 5 deletions internal/command/image/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/flapsutil"
)

func newUpdate() *cobra.Command {
Expand Down Expand Up @@ -49,20 +49,19 @@ The update will perform a rolling restart against each Machine, which may result
func runUpdate(ctx context.Context) error {
var (
appName = appconfig.NameFromContext(ctx)
client = flyutil.ClientFromContext(ctx)
)

app, err := client.GetAppCompact(ctx, appName)
app, err := flapsutil.ClientFromContext(ctx).GetApp(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}

ctx, err = apps.BuildContext(ctx, app)
ctx, err = apps.BuildContextForApp(ctx, app)
if err != nil {
return err
}

if app.IsPostgresApp() {
if flapsutil.IsPostgresApp(app) {
return updatePostgresOnMachines(ctx, app)
}

Expand Down
5 changes: 3 additions & 2 deletions internal/command/image/update_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/internal/appsecrets"
Expand All @@ -15,7 +16,7 @@ import (
"github.com/superfly/flyctl/iostreams"
)

func updateImageForMachines(ctx context.Context, app *fly.AppCompact) error {
func updateImageForMachines(ctx context.Context, app *flaps.App) error {
var (
io = iostreams.FromContext(ctx)

Expand Down Expand Up @@ -83,7 +84,7 @@ type member struct {
TargetConfig fly.MachineConfig
}

func updatePostgresOnMachines(ctx context.Context, app *fly.AppCompact) (err error) {
func updatePostgresOnMachines(ctx context.Context, app *flaps.App) (err error) {
var (
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
Expand Down
18 changes: 7 additions & 11 deletions internal/command/launch/launch_databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ func (state *launchState) createDatabases(ctx context.Context) error {

func (state *launchState) createFlyPostgres(ctx context.Context) error {
var (
pgPlan = state.Plan.Postgres.FlyPostgres
apiClient = flyutil.ClientFromContext(ctx)
io = iostreams.FromContext(ctx)
pgPlan = state.Plan.Postgres.FlyPostgres
io = iostreams.FromContext(ctx)
)

attachToExisting := false
Expand All @@ -88,12 +87,8 @@ func (state *launchState) createFlyPostgres(ctx context.Context) error {
pgPlan.AppName = fmt.Sprintf("%s-db", state.appConfig.AppName)
}

if apps, err := apiClient.GetApps(ctx, nil); err == nil {
for _, app := range apps {
if app.Name == pgPlan.AppName {
attachToExisting = true
}
}
if _, err := flapsutil.ClientFromContext(ctx).GetApp(ctx, pgPlan.AppName); err == nil {
attachToExisting = true
}

if attachToExisting {
Expand Down Expand Up @@ -486,11 +481,12 @@ func (state *launchState) createUpstashRedis(ctx context.Context) error {

var readReplicaRegions []fly.Region
{
client := flyutil.ClientFromContext(ctx)
regions, _, err := client.PlatformRegions(ctx)
flapsClient := flapsutil.ClientFromContext(ctx)
regionData, err := flapsClient.GetRegions(ctx)
if err != nil {
return err
}
regions := regionData.Regions
// Filter out deprecated regions
regions = lo.Filter(regions, func(r fly.Region, _ int) bool {
return !r.Deprecated
Expand Down
45 changes: 20 additions & 25 deletions internal/command/launch/plan/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,36 +173,29 @@ func TestDefaultPostgres_ForceTypes(t *testing.T) {
ctx = flagctx.NewContext(ctx, flagSet)

// Set up mock flaps client for MPG region availability
var mpgRegions []fly.Region
// Platform regions come from Flaps; MPGAvailable marks MPG region support.
var regions []fly.Region
if tt.mpgRegionsWithIAD {
mpgRegions = []fly.Region{
{Code: "iad", MPGAvailable: true},
{Code: "lax", MPGAvailable: true},
regions = []fly.Region{
{Code: "iad", Name: "Ashburn, Virginia (US)", MPGAvailable: true},
{Code: "lax", Name: "Los Angeles, California (US)", MPGAvailable: true},
{Code: "fra", Name: "Frankfurt, Germany"},
}
} else {
mpgRegions = []fly.Region{
{Code: "lax", MPGAvailable: true},
{Code: "fra", MPGAvailable: true},
// iad is not in the list, so it's not available
regions = []fly.Region{
{Code: "iad", Name: "Ashburn, Virginia (US)"}, // iad is not MPG-available
{Code: "lax", Name: "Los Angeles, California (US)", MPGAvailable: true},
{Code: "fra", Name: "Frankfurt, Germany", MPGAvailable: true},
}
}

ctx = flapsutil.NewContextWithClient(ctx, &mock.FlapsClient{
GetRegionsFunc: func(ctx context.Context) (*flaps.RegionData, error) {
return &flaps.RegionData{Regions: mpgRegions}, nil
return &flaps.RegionData{Regions: regions, Nearest: "iad"}, nil
},
})

// Set up mock API client for platform regions
mockClient := &mock.Client{
PlatformRegionsFunc: func(ctx context.Context) ([]fly.Region, *fly.Region, error) {
// Return some mock regions for testing
return []fly.Region{
{Code: "iad", Name: "Ashburn, Virginia (US)"},
{Code: "lax", Name: "Los Angeles, California (US)"},
{Code: "fra", Name: "Frankfurt, Germany"},
}, &fly.Region{Code: "iad", Name: "Ashburn, Virginia (US)"}, nil
},
GenqClientFunc: func() genq.Client {
return &mockGenqClient{}
},
Expand Down Expand Up @@ -269,15 +262,17 @@ func TestDefaultPostgres_RegionSwitching(t *testing.T) {
mockUIEX := &mockUIEXClient{}
ctx = uiexutil.NewContextWithClient(ctx, mockUIEX)

// Set up mock API client for platform regions
mockClient := &mock.Client{
PlatformRegionsFunc: func(ctx context.Context) ([]fly.Region, *fly.Region, error) {
return []fly.Region{
ctx = flapsutil.NewContextWithClient(ctx, &mock.FlapsClient{
GetRegionsFunc: func(ctx context.Context) (*flaps.RegionData, error) {
return &flaps.RegionData{Regions: []fly.Region{
{Code: "iad", Name: "Ashburn, Virginia (US)"},
{Code: "lax", Name: "Los Angeles, California (US)"},
{Code: "fra", Name: "Frankfurt, Germany"},
}, &fly.Region{Code: "iad", Name: "Ashburn, Virginia (US)"}, nil
{Code: "lax", Name: "Los Angeles, California (US)", MPGAvailable: true},
{Code: "fra", Name: "Frankfurt, Germany", MPGAvailable: true},
}, Nearest: "iad"}, nil
},
})

mockClient := &mock.Client{
GenqClientFunc: func() genq.Client {
return &mockGenqClient{}
},
Expand Down
Loading
Loading