Initial commit/push
This commit is contained in:
51
pkg/config/config.go
Normal file
51
pkg/config/config.go
Normal file
@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Hypervisor struct {
|
||||
Url string `yaml:"url"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
|
||||
Datacenter string `yaml:"datacenter"`
|
||||
} `yaml:"hypervisor"`
|
||||
}
|
||||
|
||||
func NewConfig() (*Config, error) {
|
||||
var configPath string
|
||||
|
||||
flag.StringVar(&configPath, "config", "./config.yml", "path to config file")
|
||||
flag.Parse()
|
||||
|
||||
s, err := os.Stat(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.IsDir() {
|
||||
return nil, fmt.Errorf("'%s' is a directory, not a regular file", configPath)
|
||||
}
|
||||
|
||||
|
||||
config := &Config{}
|
||||
|
||||
file, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
d := yaml.NewDecoder(file)
|
||||
|
||||
if err := d.Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
Reference in New Issue
Block a user