Skip to content

Commit 9e165e8

Browse files
authored
chore(cmd): strip additional files when degitting (#470)
* chore(cmd): strip additional files when degitting * chore(app): hide input when prompting for env variables * chore(cmd): remove taskfile after instantiation
1 parent 6c43f2b commit 9e165e8

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

cmd/lk/app.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import (
1919
"errors"
2020
"fmt"
2121
"os"
22-
"os/exec"
23-
"path"
2422
"regexp"
2523

2624
"github.com/charmbracelet/huh"
2725
"github.com/charmbracelet/huh/spinner"
26+
"github.com/charmbracelet/lipgloss"
2827
"github.com/livekit/livekit-cli/pkg/bootstrap"
2928
"github.com/livekit/livekit-cli/pkg/config"
3029
"github.com/urfave/cli/v3"
@@ -261,14 +260,21 @@ func setupTemplate(ctx context.Context, cmd *cli.Command) error {
261260

262261
if install {
263262
fmt.Println("Installing template...")
264-
return doInstall(ctx, bootstrap.TaskInstall, appName, verbose)
263+
if err := doInstall(ctx, bootstrap.TaskInstall, appName, verbose); err != nil {
264+
return err
265+
}
265266
} else {
266-
return doPostCreate(ctx, cmd, appName, verbose)
267+
if err := doPostCreate(ctx, cmd, appName, verbose); err != nil {
268+
return err
269+
}
267270
}
271+
272+
return cleanupTemplate(ctx, cmd, appName)
268273
}
269274

270275
func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) error {
271-
var out []byte
276+
var stdout string
277+
var stderr string
272278
var cmdErr error
273279

274280
tempName, relocate, cleanup := useTempPath(appName)
@@ -277,17 +283,18 @@ func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) err
277283
if err := spinner.New().
278284
Title("Cloning template from " + url).
279285
Action(func() {
280-
c := exec.Command("git", "clone", "--depth=1", url, tempName)
281-
out, cmdErr = c.CombinedOutput()
282-
os.RemoveAll(path.Join(tempName, ".git"))
286+
stdout, stderr, cmdErr = bootstrap.CloneTemplate(url, tempName)
283287
}).
284288
Style(theme.Focused.Title).
285289
Run(); err != nil {
286290
return err
287291
}
288292

289-
if len(out) > 0 && (cmdErr != nil || cmd.Bool("verbose")) {
290-
fmt.Println(string(out))
293+
if len(stdout) > 0 && cmd.Bool("verbose") {
294+
fmt.Println(string(stdout))
295+
}
296+
if len(stderr) > 0 && cmd.Bool("verbose") {
297+
fmt.Fprintln(os.Stderr, string(stderr))
291298
}
292299

293300
if cmdErr != nil {
@@ -296,6 +303,10 @@ func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) err
296303
return relocate()
297304
}
298305

306+
func cleanupTemplate(ctx context.Context, cmd *cli.Command, appName string) error {
307+
return bootstrap.CleanupTemplate(appName)
308+
}
309+
299310
func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addlEnv *map[string]string) error {
300311
env := map[string]string{
301312
"LIVEKIT_API_KEY": project.APIKey,
@@ -311,6 +322,7 @@ func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addl
311322
prompt := func(key, oldValue string) (string, error) {
312323
var newValue string
313324
if err := huh.NewInput().
325+
EchoMode(huh.EchoModePassword).
314326
Title("Enter " + key + "?").
315327
Placeholder(oldValue).
316328
Value(&newValue).
@@ -347,8 +359,9 @@ func doPostCreate(ctx context.Context, _ *cli.Command, rootPath string, verbose
347359
var cmdErr error
348360
if err := spinner.New().
349361
Title("Cleaning up...").
362+
TitleStyle(lipgloss.NewStyle()).
363+
Style(lipgloss.NewStyle()).
350364
Action(func() { cmdErr = task() }).
351-
Style(theme.Focused.Title).
352365
Accessible(true).
353366
Run(); err != nil {
354367
return err

pkg/bootstrap/bootstrap.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ const (
5858
TaskDev KnownTask = "dev"
5959
)
6060

61+
// Files to remove after cloning a template
62+
var templateIgnoreFiles = []string{
63+
".git",
64+
"renovate.json",
65+
"taskfile.yaml",
66+
"TEMPLATE.md",
67+
}
68+
6169
type Template struct {
6270
Name string `yaml:"name" json:"name"`
6371
Desc string `yaml:"desc" json:"description,omitempty"`
@@ -222,6 +230,26 @@ func InstantiateDotEnv(ctx context.Context, rootDir string, substitutions map[st
222230
})
223231
}
224232

233+
func CloneTemplate(url, dir string) (string, string, error) {
234+
var stdout = strings.Builder{}
235+
var stderr = strings.Builder{}
236+
237+
cmd := exec.Command("git", "clone", "--depth=1", url, dir)
238+
cmd.Stdout = &stdout
239+
cmd.Stderr = &stderr
240+
return stdout.String(), stderr.String(), cmd.Run()
241+
}
242+
243+
func CleanupTemplate(dir string) error {
244+
// Remove files that are only needed for template instantiation
245+
for _, cleanup := range templateIgnoreFiles {
246+
if err := os.RemoveAll(path.Join(dir, cleanup)); err != nil {
247+
return err
248+
}
249+
}
250+
return nil
251+
}
252+
225253
// Determine if `cmd` is a binary in PATH or a known alias
226254
func CommandExists(cmd string) bool {
227255
_, err := exec.LookPath(cmd)

0 commit comments

Comments
 (0)