mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
app/config/config.go: refactor yaml and file methods into their own files.
This commit is contained in:
parent
a36c3f7853
commit
57cc55b601
3 changed files with 39 additions and 32 deletions
|
|
@ -1,13 +1,10 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/eosswedenorg/thalos/app/log"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
|
||||
)
|
||||
|
||||
|
|
@ -65,32 +62,3 @@ func New() Config {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) error {
|
||||
var err error
|
||||
|
||||
if value.Kind == yaml.ScalarNode {
|
||||
ship.Url = value.Value
|
||||
} else {
|
||||
type ShipConfigRaw ShipConfig
|
||||
raw := ShipConfigRaw(*ship)
|
||||
if err = value.Decode(&raw); err == nil {
|
||||
*ship = ShipConfig(raw)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (cfg *Config) ReadYAML(data []byte) error {
|
||||
return yaml.Unmarshal(data, cfg)
|
||||
}
|
||||
|
||||
func (cfg *Config) ReadFile(filename string) error {
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return cfg.ReadYAML(bytes)
|
||||
}
|
||||
|
|
|
|||
14
app/config/file.go
Normal file
14
app/config/file.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func (cfg *Config) ReadFile(filename string) error {
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return cfg.ReadYAML(bytes)
|
||||
}
|
||||
25
app/config/yaml.go
Normal file
25
app/config/yaml.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) error {
|
||||
var err error
|
||||
|
||||
if value.Kind == yaml.ScalarNode {
|
||||
ship.Url = value.Value
|
||||
} else {
|
||||
type ShipConfigRaw ShipConfig
|
||||
raw := ShipConfigRaw(*ship)
|
||||
if err = value.Decode(&raw); err == nil {
|
||||
*ship = ShipConfig(raw)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (cfg *Config) ReadYAML(data []byte) error {
|
||||
return yaml.Unmarshal(data, cfg)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue