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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package cmd
import (
"encoding/json"
"net/http"
"sort"
"time"
"github.com/vespa-engine/vespa/client/go/util"
)
func listSampleApps() ([]string, error) {
return listSampleAppsAt("https://api.github.com/repos/vespa-engine/sample-apps/contents/")
}
func listSampleAppsAt(url string) ([]string, error) {
rfs, err := getRepositoryFiles(url)
if err != nil {
return nil, err
}
var apps []string
for _, rf := range rfs {
isApp, follow := isApp(rf)
if isApp {
apps = append(apps, rf.Path)
} else if follow {
apps2, err := listSampleAppsAt(rf.URL)
if err != nil {
return nil, err
}
apps = append(apps, apps2...)
}
}
sort.Strings(apps)
return apps, nil
}
func getRepositoryFiles(url string) ([]repositoryFile, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
response, err := util.HttpDo(req, time.Minute, "GitHub")
if err != nil {
return nil, err
}
defer response.Body.Close()
var files []repositoryFile
dec := json.NewDecoder(response.Body)
if err := dec.Decode(&files); err != nil {
return nil, err
}
return files, nil
}
func isApp(rf repositoryFile) (ok bool, follow bool) {
if rf.Type != "dir" {
return false, false
}
if rf.Path == "" {
return false, false
}
if rf.Path[0] == '_' || rf.Path[0] == '.' {
return false, false
}
// These are just heuristics and must be updated if we add more directories that are not applications, or that
// contain multiple applications inside
switch rf.Name {
case "test", "bin", "src":
return false, false
}
switch rf.Path {
case "news", "examples", "examples/operations", "operations", "vespa-cloud":
return false, true
}
return true, false
}
type repositoryFile struct {
Path string `json:"path"`
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
HtmlURL string `json:"html_url"`
}
|