Skip to content

Commit

Permalink
refactor: Improve build command structure and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yarlson committed Nov 2, 2024
1 parent 98c3ac0 commit e692452
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 52 deletions.
70 changes: 46 additions & 24 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
56 changes: 28 additions & 28 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//}

0 comments on commit e692452

Please sign in to comment.