-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
tag_table_builder.go
59 lines (52 loc) · 1.61 KB
/
tag_table_builder.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
package datatable
import (
"sort"
"github.com/git-town/git-town/v13/test/helpers"
)
// TagTableBuilder collects data about tags in Git repositories
// in the same way that our Gherkin tables describing tags in repos are organized.
type TagTableBuilder struct {
// tagsToLocations stores data about what locations tags are.
//
// Structure:
// tag1: [local]
// tag2: [local, remote]
tagToLocations map[string]helpers.OrderedSet[string]
}
// NewTagTableBuilder provides a fully initialized instance of TagTableBuilder.
func NewTagTableBuilder() TagTableBuilder {
return TagTableBuilder{
tagToLocations: make(map[string]helpers.OrderedSet[string]),
}
}
// Add registers the given tag from the given location into this table.
func (self *TagTableBuilder) Add(tag, location string) {
locations, exists := self.tagToLocations[tag]
if exists {
self.tagToLocations[tag] = locations.Add(location)
} else {
self.tagToLocations[tag] = helpers.NewOrderedSet(location)
}
}
// AddMany registers the given tags from the given location into this table.
func (self *TagTableBuilder) AddMany(tags []string, location string) {
for _, tag := range tags {
self.Add(tag, location)
}
}
// Table provides the data accumulated by this TagTableBuilder as a DataTable.
func (self *TagTableBuilder) Table() DataTable {
result := DataTable{}
result.AddRow("NAME", "LOCATION")
tags := make([]string, len(self.tagToLocations))
index := 0
for tag := range self.tagToLocations {
tags[index] = tag
index++
}
sort.Strings(tags)
for _, tag := range tags {
result.AddRow(tag, self.tagToLocations[tag].Join(", "))
}
return result
}