Skip to content

Commit

Permalink
fix(cmd): no auto-install
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard committed Sep 17, 2024
1 parent 003645a commit 0cb1f15
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 57 deletions.
124 changes: 74 additions & 50 deletions cmd/lk/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"regexp"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -62,6 +61,11 @@ var (
Usage: "`URL` to instantiate, must contain a taskfile.yaml",
Destination: &templateURL,
},
&cli.BoolFlag{
Name: "install",
Usage: "Run installation tasks after creating the app",
Hidden: true,
},
},
},
{
Expand All @@ -83,6 +87,11 @@ var (
Destination: &serverURL,
Hidden: true,
},
&cli.BoolFlag{
Name: "install",
Usage: "Run installation tasks after creating the app",
Hidden: true,
},
},
},
{
Expand Down Expand Up @@ -161,6 +170,9 @@ func requireProject(ctx context.Context, cmd *cli.Command) error {
}

func setupBootstrapTemplate(ctx context.Context, cmd *cli.Command) error {
verbose := cmd.Bool("verbose")
install := cmd.Bool("install")

var preinstallPrompts []huh.Field

appName = cmd.Args().First()
Expand Down Expand Up @@ -200,7 +212,9 @@ func setupBootstrapTemplate(ctx context.Context, cmd *cli.Command) error {
WithTheme(theme)
var options []huh.Option[string]
for _, t := range templates {
options = append(options, huh.NewOption(t.Name, t.URL))
if t.IsSandbox {
options = append(options, huh.NewOption(t.Name, t.URL))
}
}
templateSelect.(*huh.Select[string]).Options(options...)
preinstallPrompts = append(preinstallPrompts, templateSelect)
Expand Down Expand Up @@ -241,15 +255,18 @@ func setupBootstrapTemplate(ctx context.Context, cmd *cli.Command) error {
return err
}

fmt.Println("Installing template...")
if err := doInstall(ctx, bootstrap.TaskInstall, appName, cmd.Bool("verbose")); err != nil {
return err
if install {
fmt.Println("Installing template...")
return doInstall(ctx, bootstrap.TaskInstall, appName, verbose)
} else {
return doPostCreate(ctx, cmd, appName, false)
}

return nil
}

func setupSandboxTemplate(ctx context.Context, cmd *cli.Command) error {
verbose := cmd.Bool("verbose")
install := cmd.Bool("install")

if sandboxID == "" {
return errors.New("sandbox ID is required")
}
Expand All @@ -275,12 +292,12 @@ func setupSandboxTemplate(ctx context.Context, cmd *cli.Command) error {
return err
}

fmt.Println("Installing template...")
if err := doInstall(ctx, bootstrap.TaskInstallSandbox, details.Name, cmd.Bool("verbose")); err != nil {
return err
if install {
fmt.Println("Installing template...")
return doInstall(ctx, bootstrap.TaskInstallSandbox, details.Name, verbose)
} else {
return doPostCreate(ctx, cmd, details.Name, false)
}

return nil
}

func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) error {
Expand Down Expand Up @@ -334,44 +351,54 @@ func installTemplate(ctx context.Context, cmd *cli.Command) error {
return doInstall(ctx, bootstrap.TaskInstall, rootPath, verbose)
}

func doInstall(ctx context.Context, task bootstrap.KnownTask, rootPath string, verbose bool) error {
func doPostCreate(ctx context.Context, _ *cli.Command, rootPath string, isBootstrap bool) error {
tf, err := bootstrap.ParseTaskfile(rootPath)
if err != nil {
return err
}

install, err := bootstrap.NewTask(ctx, tf, rootPath, string(task), verbose)
taskName := string(bootstrap.TaskPostCreateSandbox)
if isBootstrap {
taskName = string(bootstrap.TaskPostCreate)
}

task, err := bootstrap.NewTask(ctx, tf, rootPath, taskName, true)
if err != nil {
return err
}
var cmdErr error
if err := spinner.New().
Title("Running task " + taskName + "...").
Action(func() { cmdErr = task() }).
Style(theme.Focused.Title).
Accessible(true).
Run(); err != nil {
return err
}
return cmdErr
}

if verbose {
if err := install(); err != nil {
return err
}
} else {
var cmdErr error
if err := spinner.New().
Title("Installing...").
Action(func() { cmdErr = install() }).
Style(theme.Focused.Title).
Run(); err != nil {
return err
}
if cmdErr != nil {
return cmdErr
}
func doInstall(ctx context.Context, task bootstrap.KnownTask, rootPath string, verbose bool) error {
tf, err := bootstrap.ParseTaskfile(rootPath)
if err != nil {
return err
}

fullPath, err := filepath.Abs(rootPath)
if fullPath != "" {
fmt.Println("Installed template to " + fullPath + ". To start your sandbox:\n")
fmt.Println(" cd " + fullPath)
fmt.Println(" lk app run dev_sandbox")
fmt.Println("")
install, err := bootstrap.NewTask(ctx, tf, rootPath, string(task), verbose)
if err != nil {
return err
}

return err
var cmdErr error
if err := spinner.New().
Title("Installing...").
Action(func() { cmdErr = install() }).
Style(theme.Focused.Title).
Accessible(verbose).
Run(); err != nil {
return err
}
return cmdErr
}

func runTask(ctx context.Context, cmd *cli.Command) error {
Expand Down Expand Up @@ -399,21 +426,18 @@ func runTask(ctx context.Context, cmd *cli.Command) error {
}
}

task, err := bootstrap.NewTask(ctx, tf, rootDir, taskName, cmd.Bool("verbose"))
task, err := bootstrap.NewTask(ctx, tf, rootDir, taskName, verbose)
if err != nil {
return err
}
if verbose {
return task()
} else {
var cmdErr error
if err := spinner.New().
Title("Running task " + taskName + "...").
Action(func() { cmdErr = task() }).
Style(theme.Focused.Title).
Run(); err != nil {
return err
}
return cmdErr
var cmdErr error
if err := spinner.New().
Title("Running task " + taskName + "...").
Action(func() { cmdErr = task() }).
Style(theme.Focused.Title).
Accessible(verbose).
Run(); err != nil {
return err
}
return cmdErr
}
21 changes: 14 additions & 7 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ const (
type KnownTask string

const (
TaskInstall KnownTask = "install"
TaskInstallSandbox KnownTask = "install_sandbox"
TaskDev KnownTask = "dev"
TaskDevSandbox KnownTask = "dev_sandbox"
TaskPostCreate KnownTask = "post_create"
TaskPostCreateSandbox KnownTask = "post_create_sandbox"
TaskInstall KnownTask = "install"
TaskInstallSandbox KnownTask = "install_sandbox"
TaskDev KnownTask = "dev"
TaskDevSandbox KnownTask = "dev_sandbox"
)

type Template struct {
Expand Down Expand Up @@ -159,10 +161,15 @@ func NewTask(ctx context.Context, tf *ast.Taskfile, dir, taskName string, verbos
return nil, err
}

task := &ast.Call{
Task: taskName,
}
if _, err := exe.GetTask(task); err != nil {
return nil, err
}

return func() error {
return exe.Run(ctx, &ast.Call{
Task: taskName,
})
return exe.Run(ctx, task)
}, nil
}

Expand Down

0 comments on commit 0cb1f15

Please sign in to comment.