-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
47 lines (39 loc) · 944 Bytes
/
formatter.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
package formatter
import (
"encoding/json"
"errors"
"bunnyshell.com/sdk"
"gopkg.in/yaml.v3"
)
func Formatter(data interface{}, format string) ([]byte, error) {
if format == "stylish" {
return stylish(data)
}
// not really useful
// maybe update the hal spec ?
switch t := data.(type) {
case *sdk.PaginatedOrganizationCollection:
t.Links = nil
case *sdk.PaginatedProjectCollection:
t.Links = nil
case *sdk.PaginatedEnvironmentCollection:
t.Links = nil
case *sdk.PaginatedComponentCollection:
t.Links = nil
case *sdk.PaginatedEventCollection:
t.Links = nil
}
switch format {
case "json":
return JsonFormatter(data)
case "yaml", "yml":
return YamlFormatter(data)
}
return nil, errors.New("Unknown format: " + format)
}
func JsonFormatter(data interface{}) ([]byte, error) {
return json.MarshalIndent(data, "", " ")
}
func YamlFormatter(data interface{}) ([]byte, error) {
return yaml.Marshal(data)
}