-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
48 lines (40 loc) · 846 Bytes
/
config.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
package command
import (
"errors"
"fmt"
"net/url"
"github.com/rjeczalik/bigstruct/internal/types"
)
type Endpoint struct {
URI string `yaml:"uri,omitempty"`
}
func (e Endpoint) Validate() error {
if e.URI == "" {
return errors.New(`"uri" is missing or empty`)
}
if _, err := url.Parse(e.URI); err != nil {
return fmt.Errorf(`"uri" is invalid: %w`, err)
}
return nil
}
type Config struct {
Backend Endpoint `yaml:"backend,omitempty"`
}
func (c Config) Validate() error {
if err := c.Backend.Validate(); err != nil {
return fmt.Errorf(`"backend" is invalid: %w`, err)
}
return nil
}
func (c Config) YAML() types.YAML {
return types.MakeYAML(&c)
}
func (c *Config) FromYAML(s types.YAML) error {
if err := s.Unmarshal(c); err != nil {
return err
}
if err := c.Validate(); err != nil {
return err
}
return nil
}