From e692452a056d7a81da92284a3fc4ba3115125ac4 Mon Sep 17 00:00:00 2001 From: Yar Kravtsov Date: Sat, 2 Nov 2024 11:37:52 +0200 Subject: [PATCH] refactor: Improve build command structure and error handling --- cmd/build.go | 70 +++++++++++++++++++++++------------- pkg/deployment/deployment.go | 56 ++++++++++++++--------------- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 78dbb9e..5b28b0c 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -2,30 +2,29 @@ package cmd import ( "context" + "fmt" "github.com/spf13/cobra" "github.com/yarlson/ftl/pkg/build" + "github.com/yarlson/ftl/pkg/config" "github.com/yarlson/ftl/pkg/console" "github.com/yarlson/ftl/pkg/executor/local" ) -var ( - noPush bool - - buildCmd = &cobra.Command{ - Use: "build", - Short: "Build your application Docker images", - Long: `Build your application Docker images as defined in ftl.yaml. +// buildCmd represents the build command +var buildCmd = &cobra.Command{ + Use: "build", + Short: "Build your application Docker images", + Long: `Build your application Docker images as defined in ftl.yaml. This command handles the entire build process, including building and pushing the Docker images to the registry.`, - Run: runBuild, - } -) + Run: runBuild, +} func init() { rootCmd.AddCommand(buildCmd) - buildCmd.Flags().BoolVar(&noPush, "no-push", false, "Build images without pushing to registry") + buildCmd.Flags().Bool("skip-push", false, "Skip pushing images to registry after building") } func runBuild(cmd *cobra.Command, args []string) { @@ -35,27 +34,50 @@ func runBuild(cmd *cobra.Command, args []string) { return } + skipPush, err := cmd.Flags().GetBool("skip-push") + if err != nil { + console.ErrPrintln("Failed to get skip-push flag:", err) + return + } + executor := local.NewExecutor() builder := build.NewBuild(executor) ctx := context.Background() - for _, service := range cfg.Services { - if err := builder.Build(ctx, service.Image, service.Path); err != nil { - console.ErrPrintf("Failed to build image for service %s: %v\n", service.Name, err) - continue - } - if !noPush { - if err := builder.Push(ctx, service.Image); err != nil { - console.ErrPrintf("Failed to push image for service %s: %v\n", service.Name, err) - continue - } - } + if err := buildAndPushServices(ctx, cfg.Services, builder, skipPush); err != nil { + console.ErrPrintln("Build process failed:", err) + return } message := "Build process completed successfully." - if noPush { - message += " Images were not pushed due to --no-push flag." + if skipPush { + message += " Images were not pushed due to --skip-push flag." } console.Success(message) } + +func buildAndPushServices(ctx context.Context, services []config.Service, builder *build.Build, skipPush bool) error { + for _, service := range services { + if err := buildAndPushService(ctx, service, builder, skipPush); err != nil { + return fmt.Errorf("failed to build service %s: %w", service.Name, err) + } + } + return nil +} + +func buildAndPushService(ctx context.Context, service config.Service, builder *build.Build, skipPush bool) error { + if err := builder.Build(ctx, service.Image, service.Path); err != nil { + return fmt.Errorf("failed to build image: %w", err) + } + + if skipPush { + return nil + } + + if err := builder.Push(ctx, service.Image); err != nil { + return fmt.Errorf("failed to push image: %w", err) + } + + return nil +} diff --git a/pkg/deployment/deployment.go b/pkg/deployment/deployment.go index 0145c5d..65e3883 100644 --- a/pkg/deployment/deployment.go +++ b/pkg/deployment/deployment.go @@ -827,31 +827,31 @@ func (d *Deployment) reloadNginxConfig(ctx context.Context) error { return err } -func (d *Deployment) deployCertRenewer(project string, cfg *config.Config) error { - command := `zero --domain $DOMAIN --email $EMAIL -c` - - service := &config.Service{ - Name: "certrenewer", - Image: "yarlson/zero-nginx", - Volumes: []string{ - "certs:/etc/nginx/ssl", - "/var/run/docker.sock:/var/run/docker.sock", - }, - EnvVars: map[string]string{ - "DOMAIN": cfg.Project.Domain, - "EMAIL": cfg.Project.Email, - }, - Entrypoint: []string{ - "/bin/sh", - "-c", - }, - Command: command, - Recreate: true, - } - - if err := d.deployService(project, service); err != nil { - return fmt.Errorf("failed to deploy certrenewer service: %w", err) - } - - return nil -} +//func (d *Deployment) deployCertRenewer(project string, cfg *config.Config) error { +// command := `zero --domain $DOMAIN --email $EMAIL -c` +// +// service := &config.Service{ +// Name: "certrenewer", +// Image: "yarlson/zero-nginx", +// Volumes: []string{ +// "certs:/etc/nginx/ssl", +// "/var/run/docker.sock:/var/run/docker.sock", +// }, +// EnvVars: map[string]string{ +// "DOMAIN": cfg.Project.Domain, +// "EMAIL": cfg.Project.Email, +// }, +// Entrypoint: []string{ +// "/bin/sh", +// "-c", +// }, +// Command: command, +// Recreate: true, +// } +// +// if err := d.deployService(project, service); err != nil { +// return fmt.Errorf("failed to deploy certrenewer service: %w", err) +// } +// +// return nil +//}