/
manifest.go
137 lines (121 loc) · 3.15 KB
/
manifest.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
/*
Copyright 2019 Adevinta
*/
package checktypes
import (
"encoding/json"
"errors"
"fmt"
"github.com/manelmontilla/toml"
)
// AssetType defines the valid types of assets a check can accept.
type AssetType int
const (
// IP represents an IP assettype.
IP AssetType = iota
// Hostname represents a hostname assettype.
Hostname
// DomainName represents an domain name assettype.
DomainName
// AWSAccount represents an AWS account assettype.
AWSAccount
// IPRange represents an IP range assettype.
IPRange
// DockerImage represents a DockerImage asset type.
DockerImage
// WebAddress represents a WebAddress asset type.
WebAddress
// GitRepository represents a git repo asset type.
GitRepository
)
var assetTypeStrings = map[AssetType]string{
IP: "IP",
Hostname: "Hostname",
DomainName: "DomainName",
AWSAccount: "AWSAccount",
IPRange: "IPRange",
DockerImage: "DockerImage",
WebAddress: "WebAddress",
GitRepository: "GitRepository",
}
// MarshalText returns string representation of a AssetType instance.
func (a *AssetType) MarshalText() (text []byte, err error) {
s, err := a.String()
if err != nil {
return nil, err
}
return []byte(s), nil
}
// UnmarshalText creates a AssetType from its string representation.
func (a *AssetType) UnmarshalText(text []byte) error {
val := string(text)
for k, v := range assetTypeStrings {
if v == val {
*a = k
return nil
}
}
return fmt.Errorf("Error value %s is not a valid AssetType value", val)
}
func (a *AssetType) String() (string, error) {
if _, ok := assetTypeStrings[*a]; !ok {
return "", fmt.Errorf("value: %d is not a valid string representation of AssetType", a)
}
return assetTypeStrings[*a], nil
}
// AssetTypes represents and array of asset types supported by a concrete
// checktype.
type AssetTypes []*AssetType
// Strings converts a slice of Assettypes into a slice of strings.
func (a AssetTypes) Strings() ([]string, error) {
res := []string{}
for _, s := range a {
txt, err := s.String()
if err != nil {
return nil, err
}
res = append(res, txt)
}
return res, nil
}
// Manifest contains all the data defined in the manifest.
type Manifest struct {
Description string
Timeout int
Options string
RequiredVars []string
QueueName string
AssetTypes AssetTypes
}
// UnmarshalOptions returns the options interpreted as json.
func (m Manifest) UnmarshalOptions() (map[string]interface{}, error) {
if m.Options == "" {
return nil, nil
}
var options = make(map[string]interface{})
err := json.Unmarshal([]byte(m.Options), &options)
if err != nil {
return nil, err
}
return options, nil
}
// ReadManifest reads a manifest file.
func ReadManifest(path string) (Manifest, error) {
d := Manifest{}
m, err := toml.DecodeFile(path, &d)
if err != nil {
return d, err
}
if !m.IsDefined("Description") {
return d, errors.New("Description field is mandatory")
}
if m.IsDefined("Options") {
dummy := make(map[string]interface{})
err = json.Unmarshal([]byte(d.Options), &dummy)
if err != nil {
err = fmt.Errorf("Error reading manifest file, Options field is not a valid json: %v", err)
return d, err
}
}
return d, nil
}