parent
229e8eb9da
commit
20c1bf2c21
@ -1,8 +1,11 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"coopcloud.tech/abra/pkg/log"
|
"coopcloud.tech/abra/pkg/log"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
@ -24,6 +27,13 @@ func gitCloneIgnoreErr(err error) bool {
|
|||||||
|
|
||||||
// Clone runs a git clone which accounts for different default branches.
|
// Clone runs a git clone which accounts for different default branches.
|
||||||
func Clone(dir, url string) error {
|
func Clone(dir, url string) error {
|
||||||
|
sigIntCh := make(chan os.Signal)
|
||||||
|
signal.Notify(sigIntCh, os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer signal.Stop(sigIntCh)
|
||||||
|
|
||||||
|
errCh := make(chan error)
|
||||||
|
|
||||||
|
go func() {
|
||||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||||
log.Debugf("git clone: %s", url)
|
log.Debugf("git clone: %s", url)
|
||||||
|
|
||||||
@ -36,7 +46,7 @@ func Clone(dir, url string) error {
|
|||||||
|
|
||||||
if err != nil && gitCloneIgnoreErr(err) {
|
if err != nil && gitCloneIgnoreErr(err) {
|
||||||
log.Debugf("git clone: %s cloned successfully", dir)
|
log.Debugf("git clone: %s cloned successfully", dir)
|
||||||
return nil
|
errCh <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -51,11 +61,11 @@ func Clone(dir, url string) error {
|
|||||||
|
|
||||||
if err != nil && gitCloneIgnoreErr(err) {
|
if err != nil && gitCloneIgnoreErr(err) {
|
||||||
log.Debugf("git clone: %s cloned successfully", dir)
|
log.Debugf("git clone: %s cloned successfully", dir)
|
||||||
return nil
|
errCh <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
errCh <- err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +73,18 @@ func Clone(dir, url string) error {
|
|||||||
} else {
|
} else {
|
||||||
log.Debugf("git clone: %s already exists", dir)
|
log.Debugf("git clone: %s already exists", dir)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-sigIntCh:
|
||||||
|
fmt.Println() // NOTE(d1): newline after ^C
|
||||||
|
if err := os.RemoveAll(dir); err != nil {
|
||||||
|
return fmt.Errorf("unable to clean up git clone of %s: %s", url, err)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("git clone %s: cancelled due to interrupt", url)
|
||||||
|
case err := <-errCh:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
9
pkg/git/clone_test.go
Normal file
9
pkg/git/clone_test.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCancelGitClone(t *testing.T) {
|
||||||
|
// TODO
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user