11package config
22
33import (
4+ "errors"
45 "fmt"
56 "io"
67 "os"
@@ -9,6 +10,21 @@ import (
910 "gopkg.in/yaml.v3"
1011)
1112
13+ var (
14+ ErrNoFilePathSpecified = errors .New ("no file path specified" )
15+ ErrNoPolicyDefined = errors .New ("at least one policy must be defined" )
16+ ErrPolicyNameEmpty = errors .New ("policy name cannot be empty" )
17+ ErrDuplicatePolicyName = errors .New ("duplicate policy name" )
18+ ErrPolicyNoRelayers = errors .New ("policy must have at least one relayer" )
19+ ErrRelayerNameEmpty = errors .New ("relayer name cannot be empty" )
20+ ErrRelayerURLEmpty = errors .New ("relayer URL cannot be empty" )
21+ ErrMappingNameEmpty = errors .New ("mapping name cannot be empty" )
22+ ErrMappingNoPolicySpecified = errors .New ("mapping must specify a policy" )
23+ ErrMappingUnknownPolicy = errors .New ("mapping references unknown policy" )
24+ ErrMappingNoPublicKeyFilter = errors .New ("mapping must specify at least one public key filter" )
25+ ErrPolicyNotFound = errors .New ("policy not found" )
26+ )
27+
1228type MuxConfig struct {
1329 Policies []Policy `yaml:"policies"`
1430 Mappings []Mapping `yaml:"mappings"`
@@ -38,7 +54,7 @@ type Filters struct {
3854// LoadMuxConfig loads the muxing configuration from a yaml file
3955func LoadMuxConfig (configPath string ) (* MuxConfig , error ) {
4056 if configPath == "" {
41- return nil , nil
57+ return nil , ErrNoFilePathSpecified
4258 }
4359
4460 file , err := os .Open (configPath )
@@ -66,31 +82,31 @@ func LoadMuxConfig(configPath string) (*MuxConfig, error) {
6682
6783func (c * MuxConfig ) validate () error {
6884 if len (c .Policies ) == 0 {
69- return fmt . Errorf ( "atleast one policy must be defined" )
85+ return ErrNoPolicyDefined
7086 }
7187
7288 policyNames := make (map [string ]bool )
7389 // check if policies are valid
7490 for _ , policy := range c .Policies {
7591 if policy .Name == "" {
76- return fmt . Errorf ( "policy name cant be empty" )
92+ return ErrPolicyNameEmpty
7793 }
7894 if policyNames [policy .Name ] {
79- return fmt .Errorf ("duplicate policy name : %s" , policy .Name )
95+ return fmt .Errorf ("%w : %s" , ErrDuplicatePolicyName , policy .Name )
8096 }
8197 policyNames [policy .Name ] = true
8298
8399 if len (policy .Relayers ) == 0 {
84- return fmt .Errorf ("policy %s must have atleast one relayer " , policy .Name )
100+ return fmt .Errorf ("policy %s: %w " , policy .Name , ErrPolicyNoRelayers )
85101 }
86102
87103 // check for the relayers if valid
88104 for _ , relayer := range policy .Relayers {
89105 if relayer .Name == "" {
90- return fmt .Errorf ("relayer name cant be empty in policy %s" , policy .Name )
106+ return fmt .Errorf ("policy %s: %w " , policy .Name , ErrRelayerNameEmpty )
91107 }
92108 if relayer .URL == "" {
93- return fmt .Errorf ("relayer url cant be empty for %s in policy %s " , relayer .Name , policy .Name )
109+ return fmt .Errorf ("policy %s, relayer %s: %w " , policy .Name , relayer .Name , ErrRelayerURLEmpty )
94110 }
95111 if _ , err := types .NewRelayEntry (relayer .URL ); err != nil {
96112 return err
@@ -102,16 +118,16 @@ func (c *MuxConfig) validate() error {
102118 // also check if they reference the correct policies
103119 for _ , mapping := range c .Mappings {
104120 if mapping .Name == "" {
105- return fmt . Errorf ( "mapping name cant be empty" )
121+ return ErrMappingNameEmpty
106122 }
107123 if mapping .Policy == "" {
108- return fmt .Errorf ("mapping %s must specify a policy " , mapping .Name )
124+ return fmt .Errorf ("mapping %s: %w " , mapping .Name , ErrMappingNoPolicySpecified )
109125 }
110126 if ! policyNames [mapping .Policy ] {
111- return fmt .Errorf ("mapping %s references unknown policy : %s" , mapping .Name , mapping .Policy )
127+ return fmt .Errorf ("mapping %s: %w : %s" , mapping .Name , ErrMappingUnknownPolicy , mapping .Policy )
112128 }
113129 if len (mapping .Filters .PublicKeys ) == 0 {
114- return fmt .Errorf ("mapping %s must specify atleast one public key filter " , mapping .Name )
130+ return fmt .Errorf ("mapping %s: %w " , mapping .Name , ErrMappingNoPublicKeyFilter )
115131 }
116132 }
117133
@@ -145,13 +161,13 @@ func (c *MuxConfig) GetRelaysForPolicy(policyName string) ([]types.RelayEntry, e
145161 return relays , nil
146162 }
147163 }
148- return nil , fmt .Errorf ("policy not found : %s" , policyName )
164+ return nil , fmt .Errorf ("%w : %s" , ErrPolicyNotFound , policyName )
149165}
150166
151167func (c * MuxConfig ) GetAllPolicies () []string {
152168 policies := make ([]string , len (c .Policies ))
153- for _ , policy := range c .Policies {
154- policies = append ( policies , policy .Name )
169+ for i , policy := range c .Policies {
170+ policies [ i ] = policy .Name
155171 }
156172 return policies
157173}
0 commit comments