-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
retriever.go
91 lines (85 loc) · 4.09 KB
/
retriever.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
package config
import "fmt"
// RetrieverConf contains all the field to configure a retriever
type RetrieverConf struct {
Kind RetrieverKind `mapstructure:"kind" koanf:"kind"`
RepositorySlug string `mapstructure:"repositorySlug" koanf:"repositoryslug"`
Branch string `mapstructure:"branch" koanf:"branch"`
Path string `mapstructure:"path" koanf:"path"`
// Deprecated: Please use AuthToken instead
GithubToken string `mapstructure:"githubToken" koanf:"githubtoken"`
URL string `mapstructure:"url" koanf:"url"`
Timeout int64 `mapstructure:"timeout" koanf:"timeout"`
HTTPMethod string `mapstructure:"method" koanf:"method"`
HTTPBody string `mapstructure:"body" koanf:"body"`
HTTPHeaders map[string][]string `mapstructure:"headers" koanf:"headers"`
Bucket string `mapstructure:"bucket" koanf:"bucket"`
Object string `mapstructure:"bucket" koanf:"bucket"`
Item string `mapstructure:"item" koanf:"item"`
Namespace string `mapstructure:"namespace" koanf:"namespace"`
ConfigMap string `mapstructure:"configmap" koanf:"configmap"`
Key string `mapstructure:"key" koanf:"key"`
BaseURL string `mapstructure:"baseUrl" koanf:"baseurl"`
AuthToken string `mapstructure:"token" koanf:"token"`
}
// IsValid validate the configuration of the retriever
// nolint:gocognit
func (c *RetrieverConf) IsValid() error {
if err := c.Kind.IsValid(); err != nil {
return err
}
if c.Kind == GitHubRetriever && c.RepositorySlug == "" {
return fmt.Errorf("invalid retriever: no \"repositorySlug\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == GitlabRetriever && c.URL == "" {
return fmt.Errorf("invalid retriever: no \"URL\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == GitlabRetriever && c.RepositorySlug == "" {
return fmt.Errorf("invalid retriever: no \"repositorySlug\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == S3Retriever && c.Item == "" {
return fmt.Errorf("invalid retriever: no \"item\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == HTTPRetriever && c.URL == "" {
return fmt.Errorf("invalid retriever: no \"url\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == GoogleStorageRetriever && c.Object == "" {
return fmt.Errorf("invalid retriever: no \"object\" property found for kind \"%s\"", c.Kind)
}
if (c.Kind == GitHubRetriever || c.Kind == FileRetriever || c.Kind == GitlabRetriever) && c.Path == "" {
return fmt.Errorf("invalid retriever: no \"path\" property found for kind \"%s\"", c.Kind)
}
if (c.Kind == S3Retriever || c.Kind == GoogleStorageRetriever) && c.Bucket == "" {
return fmt.Errorf("invalid retriever: no \"bucket\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == KubernetesRetriever && c.ConfigMap == "" {
return fmt.Errorf("invalid retriever: no \"configmap\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == KubernetesRetriever && c.Namespace == "" {
return fmt.Errorf("invalid retriever: no \"namespace\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == KubernetesRetriever && c.Key == "" {
return fmt.Errorf("invalid retriever: no \"key\" property found for kind \"%s\"", c.Kind)
}
return nil
}
// RetrieverKind is an enum containing all accepted Retriever kind
type RetrieverKind string
const (
HTTPRetriever RetrieverKind = "http"
GitHubRetriever RetrieverKind = "github"
GitlabRetriever RetrieverKind = "gitlab"
S3Retriever RetrieverKind = "s3"
FileRetriever RetrieverKind = "file"
GoogleStorageRetriever RetrieverKind = "googleStorage"
KubernetesRetriever RetrieverKind = "configmap"
)
// IsValid is checking if the value is part of the enum
func (r RetrieverKind) IsValid() error {
switch r {
case HTTPRetriever, GitHubRetriever, GitlabRetriever, S3Retriever,
FileRetriever, GoogleStorageRetriever, KubernetesRetriever:
return nil
}
return fmt.Errorf("invalid retriever: kind \"%s\" is not supported", r)
}