-
Notifications
You must be signed in to change notification settings - Fork 5
/
project.go
92 lines (84 loc) · 2.23 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
package forecast
import (
"encoding/csv"
"io"
"strconv"
"strings"
"time"
)
type projectsContainer struct {
Projects Projects `json:"projects"`
}
// Projects is a list of projects
type Projects []Project
// Project is a Forecast project
type Project struct {
ID int `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
Code string `json:"code"`
Notes string `json:"notes"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
HarvestID int `json:"harvest_id"`
Archived bool `json:"archived"`
UpdatedAt time.Time `json:"updated_at"`
UpdatedByID int `json:"updated_by_id"`
ClientID int `json:"client_id"`
Tags []string `json:"tags"`
}
// Projects returns the list of projects in the Forecast Account
func (api *API) Projects() (Projects, error) {
var container projectsContainer
err := api.do("projects", &container)
if err != nil {
return nil, err
}
return container.Projects, nil
}
// ToCSV writes the projects to the supplied writer in CSV
// format
func (projects Projects) ToCSV(w io.Writer) error {
writer := csv.NewWriter(w)
header := []string{
"id",
"name",
"color",
"code",
"notes",
"start_date",
"end_date",
"harvest_id",
"archived",
"updated_at",
"updated_by_id",
"client_id",
"tags",
}
err := writer.Write(header)
if err != nil {
return err
}
for _, project := range projects {
var record []string
record = append(record, strconv.Itoa(project.ID))
record = append(record, project.Name)
record = append(record, project.Color)
record = append(record, project.Code)
record = append(record, project.Notes)
record = append(record, project.StartDate)
record = append(record, project.EndDate)
record = append(record, strconv.Itoa(project.HarvestID))
record = append(record, strconv.FormatBool(project.Archived))
record = append(record, project.UpdatedAt.UTC().Format(time.RFC822Z))
record = append(record, strconv.Itoa(project.UpdatedByID))
record = append(record, strconv.Itoa(project.ClientID))
record = append(record, strings.Join(project.Tags, "|"))
err := writer.Write(record)
if err != nil {
return err
}
}
writer.Flush()
return nil
}