-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (70 loc) · 1.67 KB
/
main.go
File metadata and controls
76 lines (70 loc) · 1.67 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"time"
)
func main(){
timeoutThreshold := 3
retryThreshold := 5
if len(os.Args) > 1 {
timeoutThreshold, _ = strconv.Atoi(os.Args[1])
}
if len(os.Args) > 2 {
retryThreshold, _ = strconv.Atoi(os.Args[2])
}
dir, _ := os.Getwd()
if checkFileExists(dir + "/package-lock.json") && checkFileExists(dir + "/package.json"){
npmPath, err := exec.LookPath("npm")
if err != nil {
log.Fatal(err)
}
completed := false
for i:= 0; i < retryThreshold; i++ {
fmt.Printf("Trying to npm ci in %s... This cmd will timeout in %v minutes\n", dir, timeoutThreshold)
cmd := exec.Command(npmPath, "ci")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <- time.After(time.Duration(timeoutThreshold) * time.Minute):
if err := cmd.Process.Kill(); err != nil {
log.Fatal("Failed to kill process: ", err)
} else {
log.Println("Timeout reached. Re-executing npm ci")
}
case err := <- done:
if err != nil{
waitErr := err.Error()
log.Println("Failed to execute npm ci. Err:", waitErr)
} else {
completed = true
}
}
if completed {
log.Println("Successfully executed npm ci")
return
}
}
log.Fatalf("Failed to execute npm ci for %v times", retryThreshold)
} else {
log.Fatal("package.json or package-lock.json not found in directory: ", dir)
}
}
func checkFileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}