-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
62 lines (55 loc) · 1.33 KB
/
output.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
package cogs
import (
"encoding/json"
"fmt"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"
)
// OutputCfg returns the corresponding value for a given Link struct
func OutputCfg(link *Link, outputType Format) (interface{}, error) {
if outputType == Dotenv || outputType == Raw {
// don't try to marshal simple primitive types
if IsSimpleValue(link.Value) {
return SimpleValueToString(link.Value)
}
return marshalComplexValue(link.Value, FormatLinkInput(link))
}
return link.Value, nil
}
func marshalComplexValue(v interface{}, inputType Format) (output string, err error) {
var b []byte
switch inputType {
case JSON:
b, err = json.Marshal(v)
output = string(b)
case YAML:
b, err = yaml.Marshal(v)
output = string(b)
case TOML:
b, err = toml.Marshal(v)
output = string(b)
case Dotenv, Raw:
output = fmt.Sprintf("%s", v)
}
return output, err
}
// Exclude produces a laundered map with exclusionList values missing
func Exclude(exclusionList []string, linkMap LinkMap) LinkMap {
newLinkMap := make(LinkMap)
for k := range linkMap {
if InList(k, exclusionList) {
continue
}
newLinkMap[k] = linkMap[k]
}
return newLinkMap
}
// InList verifies that a given string is in a string slice
func InList(s string, ss []string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}