Skip to content

Commit

Permalink
Move sentry provision command to 'create'
Browse files Browse the repository at this point in the history
  • Loading branch information
jsierles committed Apr 21, 2023
1 parent aa80314 commit f8fc1de
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 71 deletions.
87 changes: 87 additions & 0 deletions internal/command/extensions/create_sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package extensions

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/secrets"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)

func newSentryCreate() (cmd *cobra.Command) {

const (
short = "Provision a Sentry project for a Fly.io app"
long = short + "\n"
)

cmd = command.New("create", short, long, runSentryCreate, command.RequireSession, command.RequireAppName)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}

func runSentryCreate(ctx context.Context) (err error) {
client := client.FromContext(ctx).API().GenqClient
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)

if err != nil {
return err
}

// Fetch the target organization from the app
appResponse, err := gql.GetApp(ctx, client, appName)

if err != nil {
return err
}

targetApp := appResponse.App.AppData
targetOrg := targetApp.Organization

if err != nil {
return err
}

// Fetch or create the Logtail integration for the app

_, err = gql.GetAddOn(ctx, client, appName)

if err != nil {

input := gql.CreateAddOnInput{
OrganizationId: targetOrg.Id,
Name: appName,
AppId: targetApp.Id,
Type: "sentry",
}

createAddOnResponse, err := gql.CreateAddOn(ctx, client, input)

if err != nil {
return err
}

dsn := createAddOnResponse.CreateAddOn.AddOn.Token

fmt.Fprintln(io.Out, "A Sentry project was created. Now setting the SENTRY_DSN secret and deploying.")
secrets.SetSecretsAndDeploy(ctx, gql.ToAppCompact(targetApp), map[string]string{
"SENTRY_DSN": dsn,
}, false, false)

return nil
} else {
fmt.Fprintln(io.Out, "A Sentry project already exists for this app")
}

return
}
74 changes: 3 additions & 71 deletions internal/command/extensions/sentry.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package extensions

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/secrets"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)

func newSentry() (cmd *cobra.Command) {
Expand All @@ -21,67 +12,8 @@ func newSentry() (cmd *cobra.Command) {
long = short + "\n"
)

cmd = command.New("sentry", short, long, runSentry, command.RequireSession, command.RequireAppName)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}

func runSentry(ctx context.Context) (err error) {
client := client.FromContext(ctx).API().GenqClient
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)

if err != nil {
return err
}

// Fetch the target organization from the app
appResponse, err := gql.GetApp(ctx, client, appName)

if err != nil {
return err
}

targetApp := appResponse.App.AppData
targetOrg := targetApp.Organization

if err != nil {
return err
}
cmd = command.New("sentry", short, long, nil)
cmd.AddCommand(newSentryCreate())

// Fetch or create the Logtail integration for the app

_, err = gql.GetAddOn(ctx, client, appName)

if err != nil {

input := gql.CreateAddOnInput{
OrganizationId: targetOrg.Id,
Name: appName,
AppId: targetApp.Id,
Type: "sentry",
}

createAddOnResponse, err := gql.CreateAddOn(ctx, client, input)

if err != nil {
return err
}

dsn := createAddOnResponse.CreateAddOn.AddOn.Token

fmt.Fprintln(io.Out, "A Sentry project was created. Now setting the SENTRY_DSN secret and deploying.")
secrets.SetSecretsAndDeploy(ctx, gql.ToAppCompact(targetApp), map[string]string{
"SENTRY_DSN": dsn,
}, false, false)

return nil
} else {
fmt.Fprintln(io.Out, "A Sentry project already exists for this app")
}

return
return cmd
}

0 comments on commit f8fc1de

Please sign in to comment.