github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/commands/build.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/hazelops/ize/internal/config"
     7  	"github.com/hazelops/ize/internal/manager"
     8  	"github.com/hazelops/ize/internal/manager/alias"
     9  	"github.com/hazelops/ize/internal/manager/ecs"
    10  	"github.com/hazelops/ize/internal/manager/serverless"
    11  	"github.com/hazelops/ize/pkg/templates"
    12  	"github.com/hazelops/ize/pkg/terminal"
    13  	"github.com/spf13/cobra"
    14  	"os"
    15  )
    16  
    17  type BuildOptions struct {
    18  	Config  *config.Project
    19  	AppName string
    20  }
    21  
    22  var buildLongDesc = templates.LongDesc(`
    23  	Build app.
    24      App name must be specified for a app build. 
    25  `)
    26  
    27  var buildExample = templates.Examples(`
    28  	# Build app (config file required)
    29  	ize build <app name>
    30  
    31  	# Build app with explicitly specified config file
    32  	ize --config-file (or -c) /path/to/config build <app name>
    33  
    34  	#  Build app with explicitly specified config file passed via environment variable.
    35  	export IZE_CONFIG_FILE=/path/to/config
    36  	ize build <app name>
    37  
    38  	# Build app for arm64
    39  	ize build <app name> --prefer-runtime docker-arm64
    40  `)
    41  
    42  func NewBuildFlags(project *config.Project) *BuildOptions {
    43  	return &BuildOptions{
    44  		Config: project,
    45  	}
    46  }
    47  
    48  func NewCmdBuild(project *config.Project) *cobra.Command {
    49  	o := NewBuildFlags(project)
    50  
    51  	cmd := &cobra.Command{
    52  		Use:               "build [flags] <app name>",
    53  		Example:           buildExample,
    54  		Short:             "build apps",
    55  		Long:              buildLongDesc,
    56  		Args:              cobra.MaximumNArgs(1),
    57  		ValidArgsFunction: config.GetApps,
    58  		RunE: func(cmd *cobra.Command, args []string) error {
    59  			cmd.SilenceUsage = true
    60  
    61  			err := o.Complete(cmd)
    62  			if err != nil {
    63  				return err
    64  			}
    65  
    66  			err = o.Validate()
    67  			if err != nil {
    68  				return err
    69  			}
    70  
    71  			err = o.Run()
    72  			if err != nil {
    73  				return err
    74  			}
    75  
    76  			return nil
    77  		},
    78  	}
    79  
    80  	return cmd
    81  }
    82  
    83  func (o *BuildOptions) Complete(cmd *cobra.Command) error {
    84  	o.AppName = cmd.Flags().Args()[0]
    85  
    86  	return nil
    87  }
    88  
    89  func (o *BuildOptions) Validate() error {
    90  	return nil
    91  }
    92  
    93  func (o *BuildOptions) Run() error {
    94  	ui := terminal.ConsoleUI(context.Background(), o.Config.PlainText)
    95  
    96  	var m manager.Manager
    97  
    98  	m = &ecs.Manager{
    99  		Project: o.Config,
   100  		App:     &config.Ecs{Name: o.AppName},
   101  	}
   102  
   103  	fmt.Println(os.Getenv("IZE_CONFIG_FILE"))
   104  
   105  	if app, ok := o.Config.Serverless[o.AppName]; ok {
   106  		app.Name = o.AppName
   107  		m = &serverless.Manager{
   108  			Project: o.Config,
   109  			App:     app,
   110  		}
   111  	}
   112  	if app, ok := o.Config.Alias[o.AppName]; ok {
   113  		app.Name = o.AppName
   114  		m = &alias.Manager{
   115  			Project: o.Config,
   116  			App:     app,
   117  		}
   118  	}
   119  	if app, ok := o.Config.Ecs[o.AppName]; ok {
   120  		app.Name = o.AppName
   121  		m = &ecs.Manager{
   122  			Project: o.Config,
   123  			App:     app,
   124  		}
   125  	}
   126  
   127  	return m.Build(ui)
   128  }