Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a configuration loader that reuses the existing format from the python sdk #1

Open
dietb opened this issue Jan 7, 2021 · 1 comment
Labels
good first issue Good for newcomers

Comments

@dietb
Copy link
Contributor

dietb commented Jan 7, 2021

While I'm a huge fan of environment variables, not everyone is. There should be a hook, initializer, or something else that'll load the variables from ~/.anki_vector/sdk_config.ini

For the unitiated, the file format is as followed

[00507778]
cert = location_of_bot_cert.cert
ip = bot_ip_address
name =bot_name
guid = guid_aka_token
@dietb dietb added the good first issue Good for newcomers label Jan 7, 2021
@markoxley
Copy link

I haven't added it to the repo, as it is very rough and ready, and probably not for Windows, but I wrapped the vector.New method in this function to do just what you suggest:

func getVector() (*vector.Vector, error) {
	botToken := ""
	botTarget := ""

	home := os.Getenv("HOME")
	f, err := ioutil.ReadFile(home + "/.anki_vector/sdk_config.ini")
	if err != nil {
		return nil, err
	}

	var re = regexp.MustCompile(`(?m)^\s*([^\s=]+)\s*=\s*(\S+)$`)
	for _, m := range re.FindAllStringSubmatch(string(f), -1) {
		switch m[1] {
		case "ip":
			botTarget = m[2]
			if !strings.HasPrefix(botTarget, ":443") {
				botTarget += ":443"
			}
		case "guid":
			botToken = m[2]
		}
	}

	return vector.New(
		vector.WithTarget(botTarget),
		vector.WithToken(botToken),
	)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants