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

feat(cmd): use written install instructions instead of automatic #418

Merged
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
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
Loading