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(lk): use tmp directory while preparing files #441

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
24 changes: 17 additions & 7 deletions cmd/lk/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,32 @@ func setupTemplate(ctx context.Context, cmd *cli.Command) error {
}

func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) error {
var out []byte
var cmdErr error

tempName, relocate, cleanup := useTempPath(appName)
defer cleanup()

if err := spinner.New().
Title("Cloning template from " + url).
Action(func() {
c := exec.Command("git", "clone", "--depth=1", url, appName)
var out []byte
if out, cmdErr = c.CombinedOutput(); len(out) > 0 && cmd.Bool("verbose") {
fmt.Println(string(out))
}
os.RemoveAll(path.Join(appName, ".git"))
c := exec.Command("git", "clone", "--depth=1", url, tempName)
out, cmdErr = c.CombinedOutput()
os.RemoveAll(path.Join(tempName, ".git"))
}).
Style(theme.Focused.Title).
Run(); err != nil {
return err
}
return cmdErr

if len(out) > 0 && (cmdErr != nil || cmd.Bool("verbose")) {
fmt.Println(string(out))
}

if cmdErr != nil {
return cmdErr
}
return relocate()
}

func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addlEnv *map[string]string) error {
Expand Down
16 changes: 16 additions & 0 deletions cmd/lk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -30,6 +31,7 @@ import (
"github.com/twitchtv/twirp"
"github.com/urfave/cli/v3"

"github.com/livekit/protocol/utils/guid"
"github.com/livekit/protocol/utils/interceptors"

"github.com/livekit/livekit-cli/pkg/config"
Expand Down Expand Up @@ -153,6 +155,20 @@ func wrapWith(wrap string) func(string) string {
}
}

// Provides a temporary path, a function to relocate it to a permanent path,
// and a function to clean up the temporary path that should always be deferred
// in the case of a failure to relocate.
func useTempPath(permanentPath string) (string, func() error, func() error) {
tempPath := path.Join(os.TempDir(), guid.New("LK_"))
relocate := func() error {
return os.Rename(tempPath, permanentPath)
}
cleanup := func() error {
return os.RemoveAll(tempPath)
}
return tempPath, relocate, cleanup
}

func hashString(str string) (string, error) {
hash := sha256.New()
if _, err := hash.Write([]byte(str)); err != nil {
Expand Down
Loading