|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/MakeNowJust/heredoc" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/algolia/cli/api/dashboard" |
| 10 | + "github.com/algolia/cli/pkg/auth" |
| 11 | + "github.com/algolia/cli/pkg/cmd/shared/apputil" |
| 12 | + "github.com/algolia/cli/pkg/cmdutil" |
| 13 | + "github.com/algolia/cli/pkg/config" |
| 14 | + "github.com/algolia/cli/pkg/iostreams" |
| 15 | + "github.com/algolia/cli/pkg/validators" |
| 16 | +) |
| 17 | + |
| 18 | +type CreateOptions struct { |
| 19 | + IO *iostreams.IOStreams |
| 20 | + Config config.IConfig |
| 21 | + |
| 22 | + Name string |
| 23 | + Region string |
| 24 | + ProfileName string |
| 25 | + Default bool |
| 26 | + DryRun bool |
| 27 | + |
| 28 | + PrintFlags *cmdutil.PrintFlags |
| 29 | + |
| 30 | + NewDashboardClient func(clientID string) *dashboard.Client |
| 31 | +} |
| 32 | + |
| 33 | +func NewCreateCmd(f *cmdutil.Factory) *cobra.Command { |
| 34 | + opts := &CreateOptions{ |
| 35 | + IO: f.IOStreams, |
| 36 | + Config: f.Config, |
| 37 | + PrintFlags: cmdutil.NewPrintFlags(), |
| 38 | + NewDashboardClient: func(clientID string) *dashboard.Client { |
| 39 | + return dashboard.NewClient(clientID) |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + cmd := &cobra.Command{ |
| 44 | + Use: "create", |
| 45 | + Short: "Create a new Algolia application", |
| 46 | + Long: heredoc.Doc(` |
| 47 | + Create a new Algolia application and optionally configure it as a CLI profile. |
| 48 | + Requires an active session (run "algolia auth login" first). |
| 49 | + `), |
| 50 | + Example: heredoc.Doc(` |
| 51 | + # Create an application interactively |
| 52 | + $ algolia application create |
| 53 | +
|
| 54 | + # Create with specific options |
| 55 | + $ algolia application create --name "My App" --region CA |
| 56 | +
|
| 57 | + # Create and set as default profile |
| 58 | + $ algolia application create --name "My App" --region CA --default |
| 59 | +
|
| 60 | + # Preview what would be created without actually creating it |
| 61 | + $ algolia application create --name "My App" --region CA --dry-run |
| 62 | + `), |
| 63 | + Args: validators.NoArgs(), |
| 64 | + Annotations: map[string]string{ |
| 65 | + "skipAuthCheck": "true", |
| 66 | + }, |
| 67 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 68 | + return runCreateCmd(opts) |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + cmd.Flags().StringVar(&opts.Name, "name", "My First Application", "Name for the application") |
| 73 | + cmd.Flags().StringVar(&opts.Region, "region", "", "Region code (e.g. CA, US, EU)") |
| 74 | + cmd.Flags().StringVar(&opts.ProfileName, "profile-name", "", "Name for the CLI profile (defaults to app name)") |
| 75 | + cmd.Flags().BoolVar(&opts.Default, "default", false, "Set the new profile as the default") |
| 76 | + cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "Preview the create request without sending it") |
| 77 | + |
| 78 | + opts.PrintFlags.AddFlags(cmd) |
| 79 | + |
| 80 | + return cmd |
| 81 | +} |
| 82 | + |
| 83 | +func runCreateCmd(opts *CreateOptions) error { |
| 84 | + if opts.DryRun { |
| 85 | + summary := map[string]any{ |
| 86 | + "action": "create_application", |
| 87 | + "name": opts.Name, |
| 88 | + "region": opts.Region, |
| 89 | + "default": opts.Default, |
| 90 | + "dryRun": true, |
| 91 | + } |
| 92 | + return cmdutil.PrintRunSummary( |
| 93 | + opts.IO, |
| 94 | + opts.PrintFlags, |
| 95 | + summary, |
| 96 | + fmt.Sprintf("Dry run: would create application %q in region %q", opts.Name, opts.Region), |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + client := opts.NewDashboardClient(auth.OAuthClientID()) |
| 101 | + |
| 102 | + accessToken, err := auth.EnsureAuthenticated(opts.IO, client) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + appDetails, err := apputil.CreateAndFetchApplication(opts.IO, client, accessToken, opts.Region, opts.Name) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil { |
| 113 | + p, err := opts.PrintFlags.ToPrinter() |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + return p.Print(opts.IO, appDetails) |
| 118 | + } |
| 119 | + |
| 120 | + return apputil.ConfigureProfile(opts.IO, opts.Config, appDetails, opts.ProfileName, opts.Default) |
| 121 | +} |
0 commit comments