Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-659 added ize nvm command #513

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions internal/commands/ize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package commands
import (
"bytes"
"fmt"
"os"
"path"
"runtime"
"strings"
"text/template"

"github.com/hazelops/ize/internal/config"
"github.com/hazelops/ize/internal/version"
"github.com/hazelops/ize/pkg/templates"
Expand All @@ -12,11 +18,6 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"os"
"path"
"runtime"
"strings"
"text/template"
)

var izeDescTpl = templates.LongDesc(`
Expand Down Expand Up @@ -87,6 +88,7 @@ func newRootCmd(project *config.Project) *cobra.Command {
NewCmdGen(project),
NewCmdPush(project),
NewCmdUp(project),
NewCmdNvm(project),
NewValidateCmd(),
NewVersionCmd())

Expand Down
129 changes: 129 additions & 0 deletions internal/commands/nvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package commands

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/hazelops/ize/internal/config"
"github.com/hazelops/ize/internal/manager/serverless"
"github.com/hazelops/ize/internal/requirements"
"github.com/hazelops/ize/pkg/templates"
"github.com/hazelops/ize/pkg/terminal"
"github.com/spf13/cobra"
)

type NvmOptions struct {
Config *config.Project
AppName string
Command []string
}

var nvmLongDesc = templates.LongDesc(`
Run nvm with the specified command for app.
Command must be specified for a command run.
App name must be specified for a command run.
`)

var nvmExample = templates.Examples(`
# Run nvm with command (config file required)
ize nvm <app name> -- [command]

# Run nvm with command via config file
ize --config-file (or -c) /path/to/config nvm <app name> -- [command]

# Run nvm with command via config file installed from env
export IZE_CONFIG_FILE=/path/to/config
ize nvm <app name> -- [command]
`)

func NewNvmFlags(project *config.Project) *NvmOptions {
return &NvmOptions{
Config: project,
}
}

func NewCmdNvm(project *config.Project) *cobra.Command {
o := NewNvmFlags(project)

cmd := &cobra.Command{
Use: "nvm [app-name] -- [commands]",
Example: nvmExample,
Short: "Run nvm with the specified command for app",
Long: nvmLongDesc,
ValidArgsFunction: config.GetApps,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
argsLenAtDash := cmd.ArgsLenAtDash()
err := o.Complete(cmd, args, argsLenAtDash)
if err != nil {
return err
}

err = o.Validate()
if err != nil {
return err
}

err = o.Run()
if err != nil {
return err
}

return nil
},
}

return cmd
}

func (o *NvmOptions) Complete(cmd *cobra.Command, args []string, argsLenAtDash int) error {
if err := requirements.CheckRequirements(requirements.WithIzeStructure(), requirements.WithConfigFile(), requirements.WithNVM()); err != nil {
return err
}

if argsLenAtDash > -1 {
o.Command = args[argsLenAtDash:]
}

o.AppName = cmd.Flags().Args()[0]

return nil
}

func (o *NvmOptions) Validate() error {
if len(o.AppName) == 0 {
return fmt.Errorf("can't validate options: app name must be specified")
}

if len(o.Command) == 0 {
return fmt.Errorf("can't validate: you must specify at least one command for the container")
}

return nil
}

func (o *NvmOptions) Run() error {
ui := terminal.ConsoleUI(aws.BackgroundContext(), o.Config.PlainText)

sg := ui.StepGroup()
defer sg.Wait()

var m *serverless.Manager

if app, ok := o.Config.Serverless[o.AppName]; ok {
app.Name = o.AppName
m = &serverless.Manager{
Project: o.Config,
App: app,
}
} else {
return fmt.Errorf("%s not found in config file", o.AppName)
}

err := m.Nvm(ui, o.Command)
if err != nil {
return err
}

return nil
}
103 changes: 59 additions & 44 deletions internal/manager/serverless/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os/exec"
"path/filepath"
"strings"

"github.com/hazelops/ize/pkg/term"
)

func (sls *Manager) runNpmInstall(w io.Writer) error {
Expand All @@ -29,35 +31,60 @@ func (sls *Manager) runNpmInstall(w io.Writer) error {
return nil
}

func (sls *Manager) runNvm(w io.Writer) error {
func (sls *Manager) nvm(w io.Writer, command string) error {
nvmDir := os.Getenv("NVM_DIR")
if len(nvmDir) == 0 {
nvmDir = "$HOME/.nvm"
}
var command string
err := sls.readNvmrc()
if err != nil {
return err
}

cmd := exec.Command("bash", "-c",
fmt.Sprintf("source %s/nvm.sh && nvm install %s && %s", nvmDir, sls.App.NodeVersion, command),
)

return term.New(
term.WithDir(sls.App.Path),
term.WithStdout(w),
term.WithStderr(w),
).InteractiveRun(cmd)
}

func (sls *Manager) readNvmrc() error {
_, err := os.Stat(filepath.Join(sls.App.Path, ".nvmrc"))
if os.IsNotExist(err) {
command = fmt.Sprintf("source %s/nvm.sh && nvm install %s", nvmDir, sls.App.NodeVersion)

} else {
file, err := os.ReadFile(filepath.Join(sls.App.Path, ".nvmrc"))
if err != nil {
return fmt.Errorf("can't read .nvmrc: %w", err)
}
sls.App.NodeVersion = strings.TrimSpace(string(file))
command = fmt.Sprintf("source %s/nvm.sh && nvm install %s", nvmDir, sls.App.NodeVersion)
}
return nil
}

cmd := exec.Command("bash", "-c", command)
cmd.Stdout = w
cmd.Stderr = w
cmd.Dir = filepath.Join(sls.App.Path)
err = cmd.Run()
func (sls *Manager) runNvm(w io.Writer) error {
nvmDir := os.Getenv("NVM_DIR")
if len(nvmDir) == 0 {
nvmDir = "$HOME/.nvm"
}

err := sls.readNvmrc()
if err != nil {
return err
}

return nil
command := fmt.Sprintf("source %s/nvm.sh && nvm install %s", nvmDir, sls.App.NodeVersion)

cmd := exec.Command("bash", "-c", command)

return term.New(
term.WithDir(sls.App.Path),
term.WithStdout(w),
term.WithStderr(w),
).InteractiveRun(cmd)
}

func (sls *Manager) runDeploy(w io.Writer) error {
Expand Down Expand Up @@ -100,15 +127,12 @@ func (sls *Manager) runDeploy(w io.Writer) error {
}

cmd := exec.Command("bash", "-c", command)
cmd.Stdout = w
cmd.Stderr = w
cmd.Dir = filepath.Join(sls.App.Path)
err := cmd.Run()
if err != nil {
return err
}

return nil
return term.New(
term.WithDir(sls.App.Path),
term.WithStdout(w),
term.WithStderr(w),
).InteractiveRun(cmd)
}

func (sls *Manager) runRemove(w io.Writer) error {
Expand Down Expand Up @@ -152,15 +176,12 @@ func (sls *Manager) runRemove(w io.Writer) error {
}

cmd := exec.Command("bash", "-c", command)
cmd.Stdout = w
cmd.Stderr = w
cmd.Dir = filepath.Join(sls.App.Path)
err := cmd.Run()
if err != nil {
return err
}

return nil
return term.New(
term.WithDir(sls.App.Path),
term.WithStdout(w),
term.WithStderr(w),
).InteractiveRun(cmd)
}

func (sls *Manager) runCreateDomain(w io.Writer) error {
Expand All @@ -181,15 +202,12 @@ func (sls *Manager) runCreateDomain(w io.Writer) error {
sls.App.AwsProfile, sls.Project.Env)

cmd := exec.Command("bash", "-c", command)
cmd.Stdout = w
cmd.Stderr = w
cmd.Dir = filepath.Join(sls.App.Path)
err := cmd.Run()
if err != nil {
return err
}

return nil
return term.New(
term.WithDir(sls.App.Path),
term.WithStdout(w),
term.WithStderr(w),
).InteractiveRun(cmd)
}

func (sls *Manager) runRemoveDomain(w io.Writer) error {
Expand All @@ -208,16 +226,13 @@ func (sls *Manager) runRemoveDomain(w io.Writer) error {
--stage %s`,
nvmDir, sls.App.NodeVersion, sls.App.AwsRegion,
sls.App.AwsProfile, sls.Project.Env)


cmd := exec.Command("bash", "-c", command)

cmd.Stdout = w
cmd.Stderr = w
cmd.Dir = filepath.Join(sls.App.Path)
err := cmd.Run()
if err != nil {
return err
}

return nil
return term.New(
term.WithDir(sls.App.Path),
term.WithStdout(w),
term.WithStderr(w),
).InteractiveRun(cmd)
}
25 changes: 24 additions & 1 deletion internal/manager/serverless/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package serverless

import (
"fmt"
"github.com/hazelops/ize/internal/config"
"os"
"path/filepath"
"strings"
"time"

"github.com/hazelops/ize/internal/config"

"github.com/hazelops/ize/pkg/terminal"
)

Expand All @@ -15,6 +17,27 @@ type Manager struct {
App *config.Serverless
}

func (sls *Manager) Nvm(ui terminal.UI, command []string) error {
sls.prepare()

sg := ui.StepGroup()
defer sg.Wait()

s := sg.Add("%s: running '%s'...", sls.App.Name, strings.Join(command, " "))
defer func() { s.Abort(); time.Sleep(time.Millisecond * 200) }()

err := sls.nvm(s.TermOutput(), strings.Join(command, " "))
if err != nil {
return fmt.Errorf("can't run nvm: %w", err)
}

s.Done()
s = sg.Add("%s: running '%s' completed!", sls.App.Name, strings.Join(command, " "))
s.Done()

return nil
}

func (sls *Manager) prepare() {
if sls.App.Path == "" {
appsPath := sls.Project.AppsPath
Expand Down