-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.go
106 lines (93 loc) · 2.48 KB
/
repo.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package lib
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
// FindTests looks for all the cino.yml files under the given path,
// detects the type of package contains them and returns a slice of
// Test objects.
func FindTests(path string) ([]Test, error) {
var tests []Test
// Does the supplied path(s) exist?
if stat, err := os.Stat(path); os.IsNotExist(err) || !stat.IsDir() {
return nil, fmt.Errorf("Not a directory: %s", path)
}
// Do we have a cino.yml file?
if _, err := os.Stat(filepath.Join(path, "cino.yml")); !os.IsNotExist(err) {
// If we have a cino.yml file, path is a single test or a sketch
test, err := NewTest(path, path, Sketch)
if err != nil {
return nil, err
}
tests = append(tests, *test)
} else {
// No cino.yml file, look for tests in subdirectories
var testsInSubdirectories []string
err := filepath.Walk(path,
func(subpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if _, err := os.Stat(filepath.Join(subpath, "cino.yml")); !os.IsNotExist(err) {
testsInSubdirectories = append(testsInSubdirectories, subpath)
}
}
return nil
})
if err != nil {
fmt.Println(err)
}
if len(testsInSubdirectories) == 0 {
return nil, fmt.Errorf("No tests were found in %s", path)
}
// let's check if this is a library or a core
cType := Sketch
if IsLibrary(path) {
cType = Library
} else if IsCore(path) {
cType = Core
}
for _, subpath := range testsInSubdirectories {
test, err := NewTest(subpath, path, cType)
if err != nil {
return nil, err
}
tests = append(tests, *test)
}
}
return tests, nil
}
func IsLibrary(path string) bool {
_, err := os.Stat(filepath.Join(path, "library.properties"))
return !os.IsNotExist(err)
}
func IsCore(path string) bool {
_, err := os.Stat(filepath.Join(path, "boards.txt"))
return !os.IsNotExist(err)
}
func CloneRepo(cloneURL string, commitRef string) (string, error) {
repoDir, err := ioutil.TempDir("/tmp", ".cino-server")
if err != nil {
return "", err
}
cmds := [][]string{
{"init"},
{"remote", "add", "origin", cloneURL},
{"fetch", "--depth", "1", "origin", commitRef},
{"checkout", "FETCH_HEAD"},
}
for _, c := range cmds {
cmd := exec.Command("git", c...)
cmd.Dir = repoDir
if out, err := cmd.CombinedOutput(); err != nil {
os.Stderr.WriteString(fmt.Sprintf("%s: %s", strings.Join(c, " "), out))
return "", err
}
}
return repoDir, nil
}