-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.go
More file actions
71 lines (54 loc) · 1.01 KB
/
csv.go
File metadata and controls
71 lines (54 loc) · 1.01 KB
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
package main
import (
"encoding/csv"
"io"
"os"
"strings"
)
// // //
func writeCSVFile(collection projectCollection, filename string) error {
var (
file *os.File
output io.Writer
err error
)
if filename != "-" {
file, err = os.Create(filename)
if err != nil {
return err
}
defer file.Close()
output = file
} else {
output = os.Stdout
}
csvWriter := csv.NewWriter(output)
defer csvWriter.Flush()
tags := collection.getAllTags()
tagsCount := len(tags)
record := make([]string, tagsCount+2)
// Encabezado
record[0] = "NAME"
record[1] = "PATH"
for i, tag := range tags {
record[i+2] = strings.ToUpper(tag)
}
if err = csvWriter.Write(record); err != nil {
return err
}
for _, project := range collection.projects {
record[0] = project.Name
record[1] = project.Path
for i, tag := range tags {
if project.hasTag(tag) {
record[i+2] = tag
} else {
record[i+2] = ""
}
}
if err = csvWriter.Write(record); err != nil {
return err
}
}
return nil
}