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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/hazelops/ize/internal/config"
     8  	"github.com/hazelops/ize/internal/manager/serverless"
     9  	"github.com/hazelops/ize/internal/requirements"
    10  	"github.com/hazelops/ize/pkg/templates"
    11  	"github.com/hazelops/ize/pkg/terminal"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type NvmOptions struct {
    16  	Config  *config.Project
    17  	AppName string
    18  	Command []string
    19  }
    20  
    21  var nvmLongDesc = templates.LongDesc(`
    22  	Run nvm with the specified command for app.
    23      Command must be specified for a command run. 
    24      App name must be specified for a command run. 
    25  `)
    26  
    27  var nvmExample = templates.Examples(`
    28  	# Run nvm with command (config file required)
    29  	ize nvm <app name> -- [command]
    30  
    31  	# Run nvm with command via config file
    32  	ize --config-file (or -c) /path/to/config nvm <app name> -- [command]
    33  
    34  	# Run nvm with command via config file installed from env
    35  	export IZE_CONFIG_FILE=/path/to/config
    36  	ize nvm <app name> -- [command]
    37  `)
    38  
    39  func NewNvmFlags(project *config.Project) *NvmOptions {
    40  	return &NvmOptions{
    41  		Config: project,
    42  	}
    43  }
    44  
    45  func NewCmdNvm(project *config.Project) *cobra.Command {
    46  	o := NewNvmFlags(project)
    47  
    48  	cmd := &cobra.Command{
    49  		Use:               "nvm [app-name] -- [commands]",
    50  		Example:           nvmExample,
    51  		Short:             "Run nvm with the specified command for app",
    52  		Long:              nvmLongDesc,
    53  		ValidArgsFunction: config.GetApps,
    54  		RunE: func(cmd *cobra.Command, args []string) error {
    55  			cmd.SilenceUsage = true
    56  
    57  			if len(cmd.Flags().Args()) == 0 {
    58  				return fmt.Errorf("app name must be specified")
    59  			}
    60  
    61  			argsLenAtDash := cmd.ArgsLenAtDash()
    62  			err := o.Complete(cmd, args, argsLenAtDash)
    63  			if err != nil {
    64  				return err
    65  			}
    66  
    67  			err = o.Validate()
    68  			if err != nil {
    69  				return err
    70  			}
    71  
    72  			err = o.Run()
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			return nil
    78  		},
    79  	}
    80  
    81  	return cmd
    82  }
    83  
    84  func (o *NvmOptions) Complete(cmd *cobra.Command, args []string, argsLenAtDash int) error {
    85  	if err := requirements.CheckRequirements(requirements.WithIzeStructure(), requirements.WithConfigFile(), requirements.WithNVM()); err != nil {
    86  		return err
    87  	}
    88  
    89  	if argsLenAtDash > -1 {
    90  		o.Command = args[argsLenAtDash:]
    91  	}
    92  
    93  	o.AppName = cmd.Flags().Args()[0]
    94  
    95  	return nil
    96  }
    97  
    98  func (o *NvmOptions) Validate() error {
    99  	if len(o.Command) == 0 {
   100  		return fmt.Errorf("can't validate: you must specify at least one command for the container")
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func (o *NvmOptions) Run() error {
   107  	ui := terminal.ConsoleUI(aws.BackgroundContext(), o.Config.PlainText)
   108  
   109  	sg := ui.StepGroup()
   110  	defer sg.Wait()
   111  
   112  	var m *serverless.Manager
   113  
   114  	if app, ok := o.Config.Serverless[o.AppName]; ok {
   115  		app.Name = o.AppName
   116  		m = &serverless.Manager{
   117  			Project: o.Config,
   118  			App:     app,
   119  		}
   120  	} else {
   121  		return fmt.Errorf("%s not found in config file", o.AppName)
   122  	}
   123  
   124  	err := m.Nvm(ui, o.Command)
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	return nil
   130  }