forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
155 lines (142 loc) · 3.69 KB
/
model.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package quickstarts
import (
"fmt"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
)
const (
JenkinsXQuickstartsOwner = "jenkins-x-quickstarts"
)
// GitHubQuickstart returns a github based quickstart
func GitHubQuickstart(owner string, repo string, language string, framework string, tags ...string) *Quickstart {
u := "https://github.com/" + owner + "/" + repo + "/archive/master.zip"
return &Quickstart{
ID: owner + "/" + repo,
Owner: owner,
Name: repo,
Language: language,
Framework: framework,
Tags: tags,
DownloadZipURL: u,
}
}
func toGitHubQuickstart(owner string, repo *gits.GitRepository) *Quickstart {
language := repo.Language
// TODO find this from GitHub???
framework := ""
tags := []string{}
return GitHubQuickstart(owner, repo.Name, language, framework, tags...)
}
func (m *QuickstartModel) LoadGithubQuickstarts(provider gits.GitProvider, owners []string) error {
for _, owner := range owners {
repos, err := provider.ListRepositories(owner)
if err != nil {
return err
}
for _, repo := range repos {
m.Add(toGitHubQuickstart(owner, repo))
}
}
return nil
}
func NewQuickstartModel() *QuickstartModel {
return &QuickstartModel{
Quickstarts: map[string]*Quickstart{},
}
}
// Add adds the given quickstart to this mode. Returns true if it was added
func (m *QuickstartModel) Add(q *Quickstart) bool {
if q != nil {
id := q.ID
if id != "" {
m.Quickstarts[id] = q
return true
}
}
return false
}
// CreateSurvey creates a survey to query pick a quickstart
func (model *QuickstartModel) CreateSurvey(filter *QuickstartFilter) (*QuickstartForm, error) {
language := filter.Language
if language != "" {
languages := model.Languages()
if len(languages) == 0 {
// lets ignore this filter as there are none available
filter.Language = ""
} else {
lower := strings.ToLower(language)
lowerLanguages := util.StringArrayToLower(languages)
if util.StringArrayIndex(lowerLanguages, lower) < 0 {
return nil, util.InvalidOption("language", language, languages)
}
}
}
quickstarts := model.Filter(filter)
names := []string{}
m := map[string]*Quickstart{}
for _, q := range quickstarts {
name := q.SurveyName()
m[name] = q
names = append(names, name)
}
sort.Strings(names)
if len(names) == 0 {
return nil, fmt.Errorf("No quickstarts match filter")
}
answer := ""
if len(names) == 1 {
answer = names[0]
} else {
prompt := &survey.Select{
Message: "select the quickstart you wish to create",
Options: names,
}
err := survey.AskOne(prompt, &answer, survey.Required)
if err != nil {
return nil, err
}
}
if answer == "" {
return nil, fmt.Errorf("No quickstart chosen")
}
q := m[answer]
if q == nil {
return nil, fmt.Errorf("Could not find chosen quickstart for %s", answer)
}
name, err := util.PickValue("Project name", q.Name, true)
if err != nil {
return nil, err
}
if name == "" {
return nil, fmt.Errorf("No project name")
}
form := &QuickstartForm{
Quickstart: q,
Name: name,
}
return form, nil
}
// Filter filters all the available quickstarts with the filter and return the matches
func (model *QuickstartModel) Filter(filter *QuickstartFilter) []*Quickstart {
answer := []*Quickstart{}
for _, q := range model.Quickstarts {
if filter.Matches(q) {
answer = append(answer, q)
}
}
return answer
}
// Languages returns all the languages in the quickstarts sorted
func (model *QuickstartModel) Languages() []string {
m := map[string]string{}
for _, q := range model.Quickstarts {
l := q.Language
if l != "" {
m[l] = l
}
}
return util.SortedMapKeys(m)
}