-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.go
108 lines (96 loc) · 3.08 KB
/
target.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
package pipe
import (
"github.com/abemedia/appcast/target"
"github.com/abemedia/appcast/target/azureblob"
"github.com/abemedia/appcast/target/file"
"github.com/abemedia/appcast/target/gcs"
"github.com/abemedia/appcast/target/github"
"github.com/abemedia/appcast/target/s3"
"github.com/invopop/jsonschema"
"gopkg.in/yaml.v3"
)
type targetConfig struct {
*azureblobTarget
*gcsTarget
*s3Target
*fileTarget
*githubTarget
}
func (tc *targetConfig) 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.azureblobTarget)
case "gcs":
return node.Decode(&tc.gcsTarget)
case "s3":
return node.Decode(&tc.s3Target)
case "file":
return node.Decode(&tc.fileTarget)
case "github":
return node.Decode(&tc.githubTarget)
default:
return nil
}
}
var _ yaml.Unmarshaler = (*targetConfig)(nil)
func (tc targetConfig) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
withType(tc.azureblobTarget, "azureblob"),
withType(tc.gcsTarget, "gcs"),
withType(tc.s3Target, "s3"),
withType(tc.fileTarget, "file"),
withType(tc.githubTarget, "github"),
},
}
}
type azureblobTarget 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 gcsTarget 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 s3Target 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 fileTarget struct {
Path string `yaml:"path" validate:"required"`
URL string `yaml:"url,omitempty" validate:"omitempty,http_url"`
}
type githubTarget struct {
Owner string `yaml:"owner" validate:"required"`
Repo string `yaml:"repo" validate:"required"`
Branch string `yaml:"branch,omitempty"`
Folder string `yaml:"folder,omitempty" validate:"omitempty,dirname"`
}
func getTarget(c *targetConfig) (target.Target, error) {
switch {
case c.azureblobTarget != nil:
return azureblob.New(azureblob.Config(*c.azureblobTarget))
case c.gcsTarget != nil:
return gcs.New(gcs.Config(*c.gcsTarget))
case c.s3Target != nil:
return s3.New(s3.Config(*c.s3Target))
case c.fileTarget != nil:
return file.New(file.Config(*c.fileTarget))
case c.githubTarget != nil:
return github.New(github.Config(*c.githubTarget))
default:
return nil, &Error{Errors: []string{"target.type must be one of [azureblob gcs s3 file github]"}}
}
}