forked from thaohuynh14zc/spf13-cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
33 lines (28 loc) · 796 Bytes
/
Copy pathcommand.go
File metadata and controls
33 lines (28 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cobra
import (
"context"
"os"
"os/exec"
)
// CloneContext clones a git repository with context cancellation support.
// If the context is canceled or times out, the operation is aborted, and the destination directory is cleaned up.
func CloneContext(ctx context.Context, repoURL string, destDir string) error {
// Check if context is already canceled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Use exec.CommandContext to ensure the git process is killed if the context is canceled
cmd := exec.CommandContext(ctx, "git", "clone", repoURL, destDir)
err := cmd.Run()
if err != nil {
// Clean up the destination directory on failure or cancellation
_ = os.RemoveAll(destDir)
if ctx.Err() != nil {
return ctx.Err()
}
return err
}
return nil
}