-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.go
129 lines (116 loc) · 3.72 KB
/
source.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
package pipe
import (
"github.com/abemedia/appcast/source"
"github.com/abemedia/appcast/source/azureblob"
"github.com/abemedia/appcast/source/file"
"github.com/abemedia/appcast/source/gcs"
"github.com/abemedia/appcast/source/github"
"github.com/abemedia/appcast/source/gitlab"
"github.com/abemedia/appcast/source/local"
"github.com/abemedia/appcast/source/s3"
"github.com/invopop/jsonschema"
"gopkg.in/yaml.v3"
)
type sourceConfig struct {
*azureblobSource
*gcsSource
*s3Source
*fileSource
*githubSource
*gitlabSource
*localSource
}
func (tc *sourceConfig) UnmarshalYAML(node *yaml.Node) error {
var typ struct {
Type string `yaml:"type"`
}
if err := node.Decode(&typ); err != nil {
return err
}
switch typ.Type {
case "azureblob":
return node.Decode(&tc.azureblobSource)
case "gcs":
return node.Decode(&tc.gcsSource)
case "s3":
return node.Decode(&tc.s3Source)
case "file":
return node.Decode(&tc.fileSource)
case "github":
return node.Decode(&tc.githubSource)
case "gitlab":
return node.Decode(&tc.gitlabSource)
case "local":
return node.Decode(&tc.localSource)
default:
return nil
}
}
func (tc sourceConfig) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
withType(tc.azureblobSource, "azureblob"),
withType(tc.gcsSource, "gcs"),
withType(tc.s3Source, "s3"),
withType(tc.fileSource, "file"),
withType(tc.githubSource, "github"),
withType(tc.gitlabSource, "gitlab"),
withType(tc.localSource, "local"),
},
}
}
type azureblobSource struct {
Bucket string `yaml:"bucket" validate:"required"`
Folder string `yaml:"folder,omitempty" validate:"omitempty,dirname"`
URL string `yaml:"url,omitempty" validate:"omitempty,http_url"`
}
type gcsSource struct {
Bucket string `yaml:"bucket" validate:"required"`
Folder string `yaml:"folder,omitempty" validate:"omitempty,dirname"`
URL string `yaml:"url,omitempty" validate:"omitempty,http_url"`
}
type s3Source struct {
Bucket string `yaml:"bucket" validate:"required"`
Folder string `yaml:"folder,omitempty" validate:"omitempty,dirname"`
Endpoint string `yaml:"endpoint,omitempty" validate:"omitempty,fqdn|http_url"`
Region string `yaml:"region,omitempty"`
DisableSSL bool `yaml:"disable-ssl,omitempty"`
URL string `yaml:"url,omitempty" validate:"omitempty,http_url"`
}
type fileSource struct {
Path string `yaml:"path" validate:"required,dir"`
URL string `yaml:"url,omitempty" validate:"omitempty,http_url"`
}
type githubSource struct {
Owner string `yaml:"owner" validate:"required"`
Repo string `yaml:"repo" validate:"required"`
}
type gitlabSource struct {
Owner string `yaml:"owner" validate:"required"`
Repo string `yaml:"repo" validate:"required"`
URL string `yaml:"url,omitempty" validate:"omitempty,http_url"`
}
type localSource struct {
Path string `yaml:"path" validate:"required,dir"`
Version string `yaml:"version" validate:"required,version"`
}
func getSource(c *sourceConfig) (*source.Source, error) {
switch {
case c.azureblobSource != nil:
return azureblob.New(azureblob.Config(*c.azureblobSource))
case c.gcsSource != nil:
return gcs.New(gcs.Config(*c.gcsSource))
case c.s3Source != nil:
return s3.New(s3.Config(*c.s3Source))
case c.fileSource != nil:
return file.New(file.Config(*c.fileSource))
case c.githubSource != nil:
return github.New(github.Config(*c.githubSource))
case c.gitlabSource != nil:
return gitlab.New(gitlab.Config(*c.gitlabSource))
case c.localSource != nil:
return local.New(local.Config(*c.localSource))
default:
return nil, &Error{Errors: []string{"source.type must be one of [azureblob gcs s3 file github gitlab local]"}}
}
}