-
Notifications
You must be signed in to change notification settings - Fork 3
/
project.go
167 lines (152 loc) · 3.53 KB
/
project.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
156
157
158
159
160
161
162
163
164
165
166
167
package reco
import (
"errors"
"net/http"
"strings"
"github.com/ReconfigureIO/reco/logger"
"github.com/ReconfigureIO/reco/printer"
)
var errNoActiveProject = errors.New("No active project is set, run 'reco project set' to set one")
var _ ProjectConfig = &platformProject{}
// ProjectConfig manages projects.
type ProjectConfig interface {
// List lists the projects.
List(filter M) (printer.Table, error)
// list lists information of projects.
list() ([]ProjectInfo, error)
// Create create a new project.
Create(name string) error
// Set sets the active project.
Set(name string) error
// Get gets the name of the active project.
Get() (string, error)
}
// ProjectInfo gives information about a project.
type ProjectInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Active bool `json:"-"`
}
type platformProject struct {
p *clientImpl
}
func (p platformProject) List(filter M) (printer.Table, error) {
var table printer.Table
req := p.p.apiRequest(endpoints.projects.String())
resp, err := req.Do("GET", nil)
if err != nil {
return table, err
}
var jsonResp struct {
Value []ProjectInfo `json:"value"`
Error string `json:"error"`
}
err = decodeJSON(resp.Body, &jsonResp)
if err != nil {
return table, err
}
active := false
// TODO remove set when platform handles distinct names
set := make(map[string]struct{})
var body [][]string
for _, v := range jsonResp.Value {
set[v.Name] = struct{}{}
row := []string{v.Name}
// active
if v.ID == p.p.ProjectID {
row = append(row, "[*]")
active = true
}
body = append(body, row)
}
table = printer.Table{
Header: []string{"name", "active"},
Body: body,
}
if !active {
logger.Std.Println(errNoActiveProject)
}
return table, nil
}
func (p platformProject) Create(name string) error {
req := p.p.apiRequest(endpoints.projects.String())
reqBody := M{"name": name}
resp, err := req.Do("POST", reqBody)
if err != nil {
return err
}
var jsonResp struct {
Value ProjectInfo `json:"value"`
Error string `json:"error"`
}
if resp.StatusCode != http.StatusCreated {
return errors.New("project not created")
}
if err := decodeJSON(resp.Body, &jsonResp); err != nil {
return err
}
// if no project is set, attempt to use this one
if p.p.ProjectID == "" {
p.p.ProjectID = jsonResp.Value.ID
if err := p.p.saveProject(); err != nil {
logger.Error.Println(err)
}
}
return nil
}
func (p platformProject) Set(name string) error {
projects, err := p.list()
if err != nil {
return err
}
if len(projects) == 0 {
return errProjectNotCreated
}
id := ""
for i := range projects {
if strings.ToLower(projects[i].Name) == strings.ToLower(name) {
id = projects[i].ID
break
}
}
if id == "" {
return errProjectNotFound
}
p.p.ProjectID = id
return p.p.saveProject()
}
func (p platformProject) list() ([]ProjectInfo, error) {
req := p.p.apiRequest(endpoints.projects.String())
resp, err := req.Do("GET", nil)
if err != nil {
return nil, err
}
var jsonResp struct {
Value []ProjectInfo `json:"value"`
Error string `json:"error"`
}
err = decodeJSON(resp.Body, &jsonResp)
if err != nil {
return nil, err
}
// set active project
for i, v := range jsonResp.Value {
if v.ID == p.p.ProjectID {
jsonResp.Value[i].Active = true
break
}
}
return jsonResp.Value, err
}
func (p platformProject) Get() (string, error) {
projects, err := p.list()
if err != nil {
return "", err
}
for _, prj := range projects {
if prj.Active {
return prj.Name, nil
}
}
return "", errNoActiveProject
}