forked from cloudfoundry/cli-plugin-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugins.go
164 lines (147 loc) · 3.76 KB
/
plugins.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
package models
import (
"fmt"
"io"
"sort"
"time"
)
type PluginModel interface {
PopulateModel(interface{}) []Plugin
}
type Plugins struct {
logger io.Writer
}
type Plugin struct {
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Company string `json:"company"`
Authors []Author `json:"authors"`
Homepage string `json:"homepage"`
Binaries []Binary `json:"binaries"`
}
type Binary struct {
Platform string `json:"platform"`
Url string `json:"url"`
Checksum string `json:"checksum"`
}
type PluginsJson struct {
Plugins []Plugin `json:"plugins"`
}
type Author struct {
Name string `json:"name"`
Homepage string `json:"homepage"`
Contact string `json:"contact"`
}
func NewPlugins(logger io.Writer) PluginModel {
return &Plugins{
logger: logger,
}
}
func (p *Plugins) PopulateModel(input interface{}) []Plugin {
plugins := []Plugin{}
if contents, ok := input.(map[interface{}]interface{})["plugins"].([]interface{}); ok {
for _, plugin := range contents {
plugins = append(plugins, p.extractPlugin(plugin))
}
} else {
p.logger.Write([]byte("unexpected yaml structure, 'plugins' field not found.\n"))
}
sortedJson := PluginsJson{Plugins: plugins}
sort.Sort(sortedJson)
return sortedJson.Plugins
}
func (p *Plugins) extractPlugin(rawData interface{}) Plugin {
plugin := Plugin{}
for k, v := range rawData.(map[interface{}]interface{}) {
switch k.(string) {
case "name":
plugin.Name = v.(string)
case "description":
plugin.Description = v.(string)
case "binaries":
for _, binary := range v.([]interface{}) {
plugin.Binaries = append(plugin.Binaries, p.extractBinaries(binary))
}
case "version":
plugin.Version = optionalStringField(v)
case "authors":
if v == nil {
plugin.Authors = []Author{}
} else {
for _, author := range v.([]interface{}) {
plugin.Authors = append(plugin.Authors, p.extractAuthors(author))
}
}
case "homepage":
plugin.Homepage = optionalStringField(v)
case "company":
plugin.Company = optionalStringField(v)
case "created":
plugin.Created = v.(time.Time)
case "updated":
plugin.Updated = v.(time.Time)
default:
p.logger.Write([]byte("unexpected field in plugins: " + k.(string) + "\n"))
}
}
return plugin
}
func (p *Plugins) extractBinaries(input interface{}) Binary {
binary := Binary{}
for k, v := range input.(map[interface{}]interface{}) {
switch k.(string) {
case "platform":
binary.Platform = v.(string)
case "url":
binary.Url = v.(string)
case "checksum":
binary.Checksum = v.(string)
default:
p.logger.Write([]byte("unexpected field in binaries: %s" + k.(string) + "\n"))
}
}
return binary
}
func (p *Plugins) extractAuthors(input interface{}) Author {
author := Author{}
for k, v := range input.(map[interface{}]interface{}) {
switch k.(string) {
case "name":
author.Name = v.(string)
case "homepage":
author.Homepage = optionalStringField(v)
case "contact":
author.Contact = optionalStringField(v)
default:
p.logger.Write([]byte("unexpected field in Authors: %s" + k.(string) + "\n"))
}
}
return author
}
func optionalStringField(v interface{}) string {
if v != nil {
switch v := v.(type) {
default:
return fmt.Sprintf("%v", v)
case float64:
return fmt.Sprintf("%.1f", v)
case int64:
return fmt.Sprintf("%d", v)
case bool:
return fmt.Sprintf("%t", v)
}
}
return ""
}
func (p PluginsJson) Len() int {
return len(p.Plugins)
}
func (p PluginsJson) Less(i, j int) bool {
return p.Plugins[i].Updated.After(p.Plugins[j].Updated)
}
func (p PluginsJson) Swap(i, j int) {
p.Plugins[i], p.Plugins[j] = p.Plugins[j], p.Plugins[i]
}