Example #1
0
// ReadConfig parses the configuration file provided and returns
// ReadConfig reads config from file to structure
func ReadConfig(fname string) (Config, error) {
	// Created new...
	config := &Config{}
	yamlConfig := yamlConfig{}
	if fname != "" {
		data, err := ioutil.ReadFile(fname)
		if err != nil {
			return *config, err
		}
		err = yaml.Unmarshal([]byte(data), &yamlConfig)
		if err != nil {
			return *config, err
		}
		serviceConfigs := yamlConfig.Services
		config.Services = make(map[string]ServiceConfig)
		// Now convert this to map for easier reading...
		for i := range serviceConfigs {
			c := serviceConfigs[i]
			api := Api{Host: c.Api.Host, Port: c.Api.Port}

			cleanedConfig := cleanupMap(c.Config)
			config.Services[c.Service] = ServiceConfig{CommonConfig{&api}, cleanedConfig}

		}
		log.Println("Read configuration from", fname)
		return *config, nil
	} else {
		return *config, errors.New("Empty filename.")
	}
}
Example #2
0
func TestYamlLink(t *testing.T) {
	assertions := map[string]string{
		"platform.statsd":          "platform.statsd:statsd",
		"platform.statsd:metrics":  "platform.statsd:metrics",
		"/platform.statsd":         "platform.statsd:statsd",
		"/platform.statsd:metrics": "platform.statsd:metrics",
		"statsd":                   "statsd:statsd",
		"statsd:metrics":           "statsd:metrics",
		".statsd":                  "statsd:statsd",
		".statsd:metrics":          "statsd:metrics",
		"":                         "\"\"",
	}

	for inYaml, outYaml := range assertions {
		v := Link{}
		if err := yaml.Unmarshal([]byte(inYaml), &v); err != nil {
			t.Fatal(err)
		}
		data, err := yaml.Marshal(v)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, outYaml, strings.TrimSpace(string(data)))
	}
}
Example #3
0
File: main.go Project: tixu/mmjira
func newBot() (b MMJira) {

	b = MMJira{l: zap.NewJSON(zap.DebugLevel), reg: metrics.NewRegistry()}
	data, err := ioutil.ReadFile("config.yaml")
	if err != nil {
		b.l.Panic("not able to read the file", zap.Error(err))
	}
	var config InstanceConfig
	if err = yaml.Unmarshal(data, &config); err != nil {
		b.l.Panic("not able to marshal the file", zap.Error(err))
	}
	b.c = &config
	if !b.c.Debug {
		b.l.SetLevel(zap.ErrorLevel)
	}
	mmpost, err := mmcontroller.NewController(b.c.MMicon, b.c.MMuser, b.c.Hooks, b.c.Debug, metrics.NewPrefixedChildRegistry(b.reg, "mmc."))
	if err != nil {
		panic(err)
	}

	b.m = mmpost
	b.l.Debug("outputting config", zap.Object("config", b.c))
	b.r = mux.NewRouter()
	b.r.HandleFunc("/", b.homeHandler)
	b.r.HandleFunc("/hooks/", b.getHandler).Methods("GET")
	b.r.HandleFunc("/hooks/{hookid}", b.postHandler).Methods("POST")
	b.r.Handle("/metrics", exp.ExpHandler(b.reg))
	b.r.HandleFunc("/config/", b.configGetHandler).Methods("GET")

	return b

}
Example #4
0
func TestPathUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		device Path
		err    error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"/path"`},
			out: out{device: Path("/path")},
		},
		{
			in:  in{data: `"bad"`},
			out: out{device: Path("bad"), err: ErrPathRelative},
		},
	}

	for i, test := range tests {
		var device Path
		err := yaml.Unmarshal([]byte(test.in.data), &device)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.device, device) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.device, device)
		}
	}
}
Example #5
0
// VarsFromFile reads variables from either JSON or YAML file
func VarsFromFile(filename string) (vars Vars, err error) {
	log.Debugf("Load vars from file %s", filename)

	if filename, err = resolveFileName(filename); err != nil {
		return nil, err
	}

	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	vars = Vars{}

	switch filepath.Ext(filename) {
	case ".yaml", ".yml", ".":
		if err := yaml.Unmarshal(data, &vars); err != nil {
			return nil, err
		}
	case ".json":
		if err := json.Unmarshal(data, &vars); err != nil {
			return nil, err
		}
	}

	return vars, nil
}
func TestMkfsOptionsUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		options MkfsOptions
		err     error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `["--label=ROOT"]`},
			out: out{options: MkfsOptions([]string{"--label=ROOT"})},
		},
	}

	for i, test := range tests {
		var options MkfsOptions
		err := yaml.Unmarshal([]byte(test.in.data), &options)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.options, options) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.options, options)
		}
	}
}
Example #7
0
func TestHashUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		hash Hash
		err  error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef`},
			out: out{hash: Hash{Function: "sha512", Sum: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}},
		},
		{
			in:  in{data: `xor01234567`},
			out: out{err: ErrHashMalformed},
		},
	}

	for i, test := range tests {
		var hash Hash
		err := yaml.Unmarshal([]byte(test.in.data), &hash)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.hash, hash) {
			t.Errorf("#%d: bad hash: want %+v, got %+v", i, test.out.hash, hash)
		}
	}
}
func TestFilesystemUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		filesystem Filesystem
		err        error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: "mount:\n  device: /foo\n  format: ext4"},
			out: out{filesystem: Filesystem{Mount: &FilesystemMount{Device: "/foo", Format: "ext4"}}},
		},
		{
			in:  in{data: "mount:\n  format: ext4"},
			out: out{err: ErrPathRelative},
		},
	}

	for i, test := range tests {
		var filesystem Filesystem
		err := yaml.Unmarshal([]byte(test.in.data), &filesystem)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.filesystem, filesystem) {
			t.Errorf("#%d: bad filesystem: want %#v, got %#v", i, test.out.filesystem, filesystem)
		}
	}
}
func TestFilesystemFormatUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		format FilesystemFormat
		err    error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"ext4"`},
			out: out{format: FilesystemFormat("ext4")},
		},
		{
			in:  in{data: `"bad"`},
			out: out{format: FilesystemFormat("bad"), err: ErrFilesystemInvalidFormat},
		},
	}

	for i, test := range tests {
		var format FilesystemFormat
		err := yaml.Unmarshal([]byte(test.in.data), &format)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.format, format) {
			t.Errorf("#%d: bad format: want %#v, got %#v", i, test.out.format, format)
		}
	}
}
Example #10
0
func ParseAsV2_0_0(data []byte) (types.Config, error) {
	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return types.Config{}, err
	}

	var keyMap map[interface{}]interface{}
	if err := yaml.Unmarshal(data, &keyMap); err != nil {
		return types.Config{}, err
	}

	if err := assertKeysValid(keyMap, reflect.TypeOf(Config{})); err != nil {
		return types.Config{}, err
	}

	return ConvertAs2_0_0(cfg)
}
Example #11
0
// ReadConfigBytes reads the supplied configuration byte[] and returns the
// corresponding map or an error.
func ReadConfigBytes(configSetting []byte) (*map[interface{}]interface{}, error) {
	configData := make(map[interface{}]interface{})
	err := yaml.Unmarshal(configSetting, &configData)
	if err != nil {
		return nil, err
	}
	return &configData, nil
}
Example #12
0
func NewMigrationFromGull(name string, source string) (*Migration, error) {
	migration := newMigration(name)

	sourceBytes, err := ingestMigrationTemplate(source)
	if err != nil {
		return nil, err
	}

	err = yaml.Unmarshal(sourceBytes, &migration.Content)

	return migration, err
}
Example #13
0
// Load unmarshals YAML data from the filesystem
func Load(path string, result interface{}) error {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}

	if err := yaml.Unmarshal(data, result); err != nil {
		return err
	}

	return nil
}
Example #14
0
func (c *Config) parseConfFile() error {
	content, err := ioutil.ReadFile(c.confFile)
	if err != nil {
		return err
	}
	c.confParsed = make(map[interface{}]interface{})
	err = yaml.Unmarshal(content, &(c.confParsed))
	if err != nil {
		return err
	}
	return nil
}
Example #15
0
func ConfigFromFile(configPath string) (*Config, error) {
	configBytes, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, err
	}

	config := Config{}
	if err := goyaml.Unmarshal(configBytes, &config); err != nil {
		return nil, err
	}

	return &config, nil
}
Example #16
0
func newConfig(in io.Reader) (*config, error) {
	buf, err := ioutil.ReadAll(in)
	if err != nil {
		return nil, err
	}

	config := &config{}
	if err := yaml.Unmarshal(buf, config); err != nil {
		return nil, err
	}

	return config, nil
}
Example #17
0
func (a *yamlTestCases) run(t *testing.T) error {
	for inYaml, outYaml := range a.assertions {
		v := &Container{}
		if err := yaml.Unmarshal([]byte(inYaml), v); err != nil {
			return fmt.Errorf("Failed unmarshal for test %q, error: %s", inYaml, err)
		}
		data, err := yaml.Marshal(v)
		if err != nil {
			return fmt.Errorf("Failed marshal for test %q, error: %s", inYaml, err)
		}
		assert.Equal(t, outYaml, strings.TrimSpace(string(data)))
	}
	return nil
}
Example #18
0
func ParseConfig(configFile string) Config {
	configContent, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	parsedConfig := Config{}
	err = yaml.Unmarshal(configContent, &parsedConfig)
	if err != nil {
		log.Fatalf("error: %v", err)
	}

	return parsedConfig
}
Example #19
0
func TestNetworkdUnitNameUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		unit NetworkdUnitName
		err  error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"test.network"`},
			out: out{unit: NetworkdUnitName("test.network")},
		},
		{
			in:  in{data: `"test.link"`},
			out: out{unit: NetworkdUnitName("test.link")},
		},
		{
			in:  in{data: `"test.netdev"`},
			out: out{unit: NetworkdUnitName("test.netdev")},
		},
		{
			in:  in{data: `"test.blah"`},
			out: out{err: errors.New("invalid networkd unit extension")},
		},
	}

	for i, test := range tests {
		var unit NetworkdUnitName
		err := yaml.Unmarshal([]byte(test.in.data), &unit)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if err != nil {
			continue
		}

		if !reflect.DeepEqual(test.out.unit, unit) {
			t.Errorf("#%d: bad unit: want %#v, got %#v", i, test.out.unit, unit)
		}
	}
}
Example #20
0
// Check
// - Yaml format
// - Duplicated program
func (s *Supervisor) readConfigFromDB() (pgs []Program, err error) {
	data, err := ioutil.ReadFile(s.programPath())
	if err != nil {
		data = []byte("")
	}
	pgs = make([]Program, 0)
	if err = yaml.Unmarshal(data, &pgs); err != nil {
		return nil, err
	}
	visited := map[string]bool{}
	for _, pg := range pgs {
		if visited[pg.Name] {
			return nil, fmt.Errorf("Duplicated program name: %s", pg.Name)
		}
		visited[pg.Name] = true
	}
	return
}
Example #21
0
func ParseServerTemplate(ymlData io.Reader) (*ServerTemplate, error) {
	st := ServerTemplate{}
	bytes, err := ioutil.ReadAll(ymlData)
	if err != nil {
		return nil, err
	}
	err = yaml.Unmarshal(bytes, &st)
	if err != nil {
		return nil, err
	}
	for sequence, _ := range st.RightScripts {
		if sequence != "Boot" && sequence != "Operational" && sequence != "Decommission" {
			typeError := fmt.Errorf("%s is not a valid sequence name for RightScripts.  Must be Boot, Operational, or Decommission:", sequence)
			return nil, typeError
		}
	}
	return &st, nil
}
Example #22
0
func readConfig() (config Config) {
	config = make(Config)

	prefferedPaths := []string{
		"./cloud-ssh.yaml",
		userHomeDir() + "/.ssh/cloud-ssh.yaml",
		"/etc/cloud-ssh.yaml",
	}

	var content []byte

	for _, path := range prefferedPaths {
		if _, err := os.Stat(path); err == nil {
			fmt.Println("Found config:", path)
			content, err = ioutil.ReadFile(path)

			if err != nil {
				log.Fatal("Error while reading config: ", err)
			}

			break
		}
	}

	if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
		config["default"] = make(StrMap)
		config["default"]["access_key"] = os.Getenv("AWS_ACCESS_KEY_ID")
		config["default"]["secret_key"] = os.Getenv("AWS_SECRET_ACCESS_KEY")
		config["default"]["region"] = os.Getenv("AWS_REGION")
		config["default"]["provider"] = "aws"
	}

	if len(content) == 0 {
		if len(config) == 0 {
			fmt.Println("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation.")
		}
		return
	} else if err := yaml.Unmarshal(content, &config); err != nil {
		log.Fatal(err)
	}

	return
}
Example #23
0
func (self *Api) LoadToken() (oauth2.Token, error) {
	var token oauth2.Token
	if _, err := os.Stat(self.AuthFile); os.IsNotExist(err) {
		return token, fmt.Errorf("The authentication file doesn't exist! Maybe you need to run `graphatmo login`?")
	}

	authContent, err := ioutil.ReadFile(self.AuthFile)
	if err != nil {
		return token, err
	}

	token = oauth2.Token{}
	err = yaml.Unmarshal(authContent, &token)
	if err != nil {
		return token, err
	}

	return token, nil
}
Example #24
0
// UpdateGetLatestVersions gets the latest versions struct by downloading and parsing the version.yml file for right_st
// from the rsbin bucket. See version.sh and the Makefile upload target for how this file is created.
func UpdateGetLatestVersions() (*LatestVersions, error) {
	// get the version.yml file over HTTP(S)
	res, err := http.Get(UpdateGetVersionUrl())
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		return nil, fmt.Errorf("Unexpected HTTP response getting %s: %s", UpdateGetVersionUrl(), res.Status)
	}
	versions, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	// parse the version.yml file into a LatestVersions struct and return the result and any errors
	var latest LatestVersions
	err = yaml.Unmarshal(versions, &latest)
	return &latest, err
}
Example #25
0
func TestAssertKeysValid(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		err ErrKeysUnrecognized
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: "ignition:\n  config:"},
			out: out{},
		},
		{
			in:  in{data: "passwd:\n  groups:\n    - name: example"},
			out: out{},
		},
		{
			in:  in{data: "password:\n  groups:"},
			out: out{err: ErrKeysUnrecognized{"password"}},
		},
		{
			in:  in{data: "passwd:\n  groups:\n    - naem: example"},
			out: out{err: ErrKeysUnrecognized{"naem"}},
		},
	}

	for i, test := range tests {
		var cfg interface{}
		if err := yaml.Unmarshal([]byte(test.in.data), &cfg); err != nil {
			t.Errorf("#%d: unmarshal failed: %v", i, err)
			continue
		}
		if err := assertKeysValid(cfg, reflect.TypeOf(Config{})); !reflect.DeepEqual(err, test.out.err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
	}
}
Example #26
0
func (c *Config) Parse(yamlDoc string) (err error) {
	defer errs.PassE(&err)
	errs.CheckE(yaml.Unmarshal([]byte(yamlDoc), &c.ast))
	for _, block := range c.ast {
		for _, reg := range block.Regs {
			var currentLSB uint = 0
			for _, f := range reg.Fields {
				check := func(cond bool, s string) {
					if !cond {
						errs.CheckE(fmt.Errorf("`%s`/`%s`/`%s`: %s", block.Name, reg.Name, f.Name, s))
					}
				}

				// the following ensures that f.Width == f.Bits[1] - f.Bits[0] + 1
				l := len(f.Bits)
				if l == 1 {
					f.Bits = append(f.Bits, f.Bits[0])
				}
				check(l <= 2, "too many bits specified")
				if l == 0 {
					check(f.Width != 0, "missing field location")
					f.Bits = []uint{currentLSB, currentLSB + f.Width - 1}
				} else {
					if f.Bits[0] > f.Bits[1] {
						f.Bits = []uint{f.Bits[1], f.Bits[0]}
					}
					width := f.Bits[1] - f.Bits[0] + 1
					if f.Width == 0 {
						f.Width = width
					}
					check(f.Width == width, "field width inconsistent")
				}

				check(currentLSB <= f.Bits[0], "field is out of order")
				check(63 >= f.Bits[1], "field out of range")
				currentLSB = f.Bits[1] + 1
			}
		}
	}
	return
}
Example #27
0
func readConf(filename string) (c Configuration, err error) {
	// initial default value
	c.Server.Addr = ":11313" // in memory of 08-31 13:13
	c.Client.ServerURL = "http://localhost:11313"

	data, err := ioutil.ReadFile(filename)
	if err != nil {
		data = []byte("")
	}
	err = yaml.Unmarshal(data, &c)
	if err != nil {
		return
	}
	cfgDir := filepath.Dir(filename)
	if !IsDir(cfgDir) {
		os.MkdirAll(cfgDir, 0755)
	}
	data, _ = yaml.Marshal(c)
	err = ioutil.WriteFile(filename, data, 0644)
	return
}
Example #28
0
func LoadFormulaFromFile(path string) def.Formula {
	filename, _ := filepath.Abs(path)

	content, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(Error.Wrap(fmt.Errorf("Could not read formula file %q: %s", filename, err)))
	}
	content = tab2space(content)

	var raw interface{}
	if err := yaml.Unmarshal(content, &raw); err != nil {
		panic(Error.New("Could not parse formula file %q: %s", filename, err))
	}
	raw = stringifyMapKeys(raw)
	formula := def.Formula{}
	if err := formula.Unmarshal(raw); err != nil {
		panic(Error.New("Could not parse formula file %q: %s", filename, err))
	}

	return formula
}
Example #29
0
// NewFromDocker produces an container spec object from a docker.Container given by go-dockerclient.
func NewFromDocker(apiContainer *docker.Container) (*Container, error) {
	yamlData, ok := apiContainer.Config.Labels["rocker-compose-config"]
	if !ok {
		return nil, ErrNotRockerCompose{apiContainer.ID}
	}

	container := &Container{}

	if err := yaml.Unmarshal([]byte(yamlData), container); err != nil {
		return nil, fmt.Errorf("Failed to parse YAML config for container %s, error: %s", apiContainer.Name, err)
	}

	if container.Labels != nil {
		for k := range container.Labels {
			if strings.HasPrefix(k, "rocker-compose-") {
				delete(container.Labels, k)
			}
		}
	}

	return container, nil
}
Example #30
0
func TestFileModeUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		mode FileMode
		err  error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `0644`},
			out: out{mode: FileMode(0644)},
		},
		{
			in:  in{data: `0420`},
			out: out{mode: FileMode(0420)},
		},
		{
			in:  in{data: `017777`},
			out: out{mode: FileMode(017777), err: ErrFileIllegalMode},
		},
	}

	for i, test := range tests {
		var mode FileMode
		err := yaml.Unmarshal([]byte(test.in.data), &mode)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.mode, mode) {
			t.Errorf("#%d: bad mode: want %#o, got %#o", i, test.out.mode, mode)
		}
	}
}