|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/render-oss/cli/pkg/client" |
| 10 | + "github.com/render-oss/cli/pkg/command" |
| 11 | + "github.com/render-oss/cli/pkg/config" |
| 12 | + "github.com/render-oss/cli/pkg/dependencies" |
| 13 | + "github.com/render-oss/cli/pkg/service" |
| 14 | + "github.com/render-oss/cli/pkg/text" |
| 15 | + "github.com/render-oss/cli/pkg/types" |
| 16 | + servicetypes "github.com/render-oss/cli/pkg/types/service" |
| 17 | +) |
| 18 | + |
| 19 | +var ServiceCreateCmd = &cobra.Command{ |
| 20 | + Use: "create", |
| 21 | + Args: cobra.NoArgs, |
| 22 | + Short: "Create a new service", |
| 23 | + Long: `Create a new service on Render. |
| 24 | +
|
| 25 | +This command currently runs in non-interactive mode only. |
| 26 | +Provide all config with flags. |
| 27 | +
|
| 28 | +Examples: |
| 29 | + render services create --name my-api --type web_service --repo https://github.com/org/repo --output json |
| 30 | + render services create --from srv-abc123 --name my-api-clone --output json |
| 31 | +`, |
| 32 | +} |
| 33 | + |
| 34 | +func init() { |
| 35 | + servicesCmd.AddCommand(ServiceCreateCmd) |
| 36 | + |
| 37 | + ServiceCreateCmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 38 | + var cliInput servicetypes.Service |
| 39 | + if err := command.ParseCommand(cmd, args, &cliInput); err != nil { |
| 40 | + return fmt.Errorf("failed to parse command: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + // Interactive mode is not implemented yet; force non-interactive behavior for now. |
| 44 | + command.DefaultFormatNonInteractive(cmd) |
| 45 | + |
| 46 | + ctx := cmd.Context() |
| 47 | + |
| 48 | + nonInteractive, err := command.NonInteractive(cmd, func() (*client.Service, error) { |
| 49 | + return createServiceNonInteractive(ctx, cliInput) |
| 50 | + }, func(svc *client.Service) string { |
| 51 | + return text.FormatStringF("Created service %s (%s)", svc.Name, svc.Id) |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + if nonInteractive { |
| 57 | + return nil |
| 58 | + } |
| 59 | + |
| 60 | + // TODO: Implement interactive TUI mode in a later phase |
| 61 | + // This will use views.NewServiceCreateView for guided configuration |
| 62 | + return fmt.Errorf("interactive mode not yet implemented") |
| 63 | + } |
| 64 | + |
| 65 | + ServiceCreateCmd.Flags().String("name", "", "Service name") |
| 66 | + serviceTypeFlag := command.NewEnumInput(servicetypes.ServiceTypeValues(), false) |
| 67 | + ServiceCreateCmd.Flags().Var(serviceTypeFlag, "type", "Service type") |
| 68 | + ServiceCreateCmd.Flags().String("from", "", "Clone configuration from existing service (ID or name). Other flags override cloned values.") |
| 69 | + ServiceCreateCmd.Flags().String("repo", "", "Git repository URL") |
| 70 | + ServiceCreateCmd.Flags().String("branch", "", "Git branch") |
| 71 | + ServiceCreateCmd.Flags().String("image", "", "Docker image URL") |
| 72 | + regionFlag := command.NewEnumInput(types.RegionValues(), false) |
| 73 | + ServiceCreateCmd.Flags().Var(regionFlag, "region", "Deployment region") |
| 74 | + ServiceCreateCmd.Flags().String("plan", "", "Service plan") |
| 75 | + runtimeFlag := command.NewEnumInput(servicetypes.ServiceRuntimeValues(), false) |
| 76 | + ServiceCreateCmd.Flags().Var(runtimeFlag, "runtime", "Runtime environment") |
| 77 | + ServiceCreateCmd.Flags().String("root-directory", "", "Root directory") |
| 78 | + ServiceCreateCmd.Flags().String("build-command", "", "Build command") |
| 79 | + ServiceCreateCmd.Flags().String("start-command", "", "Start command") |
| 80 | + ServiceCreateCmd.Flags().String("health-check-path", "", "Health check path") |
| 81 | + ServiceCreateCmd.Flags().String("publish-directory", "", "Publish directory") |
| 82 | + ServiceCreateCmd.Flags().String("cron-command", "", "Cron command") |
| 83 | + ServiceCreateCmd.Flags().String("cron-schedule", "", "Cron schedule") |
| 84 | + ServiceCreateCmd.Flags().String("environment-id", "", "Environment ID") |
| 85 | + ServiceCreateCmd.Flags().StringArray("env-var", nil, "Environment variable in KEY=VALUE format (can be specified multiple times)") |
| 86 | + ServiceCreateCmd.Flags().StringArray("secret-file", nil, "Secret file in NAME:LOCAL_PATH format (can be specified multiple times)") |
| 87 | + ServiceCreateCmd.Flags().String("registry-credential", "", "Registry credential") |
| 88 | + ServiceCreateCmd.Flags().Bool("auto-deploy", true, "Enable auto-deploy") |
| 89 | + ServiceCreateCmd.Flags().String("pre-deploy-command", "", "Pre-deploy command") |
| 90 | +} |
| 91 | + |
| 92 | +func createServiceNonInteractive(ctx context.Context, cliInput servicetypes.Service) (*client.Service, error) { |
| 93 | + deps := dependencies.GetFromContext(ctx) |
| 94 | + serviceRepo := deps.ServiceRepo() |
| 95 | + registryService := deps.RegistryService() |
| 96 | + |
| 97 | + ownerID, err := config.WorkspaceID() |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + cliInput = servicetypes.NormalizeServiceCreateCLIInput(cliInput) |
| 103 | + |
| 104 | + if cliInput.From != nil { |
| 105 | + if err := getConfigFromService(ctx, serviceRepo, &cliInput); err != nil { |
| 106 | + return nil, fmt.Errorf("failed to clone configuration from source service %q: %w", *cliInput.From, err) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + cliInput, err = servicetypes.NormalizeAndValidateCreateInput(cliInput, false) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + if cliInput.RegistryCredential != nil && cliInput.SupportsRegistryCredentials() { |
| 116 | + registryCredentialID, err := registryService.FindOneRegistryCredentialByIDFromNameOrID(ctx, ownerID, *cliInput.RegistryCredential) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + cliInput.RegistryCredential = ®istryCredentialID |
| 121 | + } |
| 122 | + |
| 123 | + body, err := service.BuildCreateRequest(cliInput, ownerID) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + return serviceRepo.CreateService(ctx, body) |
| 128 | +} |
| 129 | + |
| 130 | +func getConfigFromService(ctx context.Context, repo *service.Repo, input *servicetypes.Service) error { |
| 131 | + serviceID, err := repo.ResolveServiceIDFromNameOrID(ctx, *input.From) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("failed to resolve source service %s: %w", *input.From, err) |
| 134 | + } |
| 135 | + input.From = &serviceID |
| 136 | + |
| 137 | + sourceService, err := repo.GetService(ctx, serviceID) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("failed to load source service %s: %w", serviceID, err) |
| 140 | + } |
| 141 | + service.ServiceFromAPI(input, sourceService) |
| 142 | + return nil |
| 143 | +} |
0 commit comments