52 lines
		
	
	
		
			836 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			836 B
		
	
	
	
		
			Go
		
	
	
	
	
	
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
 | 
						|
}
 |