38 lines
710 B
Go
38 lines
710 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type YamlVersionProvider struct {
|
|
File string
|
|
Key string
|
|
}
|
|
|
|
type YamlVersion string
|
|
|
|
func (y YamlVersion) String() string { return string(y) }
|
|
|
|
func (y YamlVersionProvider) GetVersion() (Version, error) {
|
|
f, err := os.Open(y.File)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("couldn't open yaml file for reading: %w", err)
|
|
}
|
|
defer f.Close()
|
|
var data map[string]interface{}
|
|
err = yaml.NewDecoder(f).Decode(&data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("couldn't parse yaml file: %w", err)
|
|
}
|
|
|
|
val, ok := data[y.Key]
|
|
if !ok {
|
|
return nil, fmt.Errorf("couldn't find key \"%s\"", y.Key)
|
|
}
|
|
|
|
return YamlVersion(fmt.Sprintf("%v", val)), nil
|
|
}
|