I'm trying to make a really simple ssh address book program. Takes some info about ssh addresses and stores them in a yaml document. I'm doing it partly to learn a bit about Go and am having a small problem. I can serialize the data and put a document into a file but I'm getting this error when I try to read it back:yaml: control characters are not allowed
I'm not sure what this error message means, googling didn't yield any helpful results. Any ideas?
These are the structs I'm using to organize the data:
type EntriesList struct {
SSHEntries []SSHEntry `yaml:"sshentries"`
}
type SSHEntry struct {
Name string `yaml:"name"`
Command SSHCmd `yaml:"command"`
}
type SSHCmd struct {
Addr string `yaml:"addr"`
Port int `yaml:"port"`
Uname string `yaml:"uname"`
}
The format it puts my data into is:
---
entrieslist:
- name: entry1
command:
addr: somewhere
port: 22
uname: someone
- name: entry2 ... etc
I checked this ^^ with a YAML validator and it is legal YAML. Here is my function to read the file:
// CONF is the path to the file
func readConf(CONF string) *EntriesList {
configFile := openConfigFile(CONF)
defer configFile.Close()
buffer := make([]byte, 512, 512)
_, err := configFile.Read(buffer)
check(err)
var entries EntriesList
err = yaml.Unmarshal(buffer, &entries)
data, _ := yaml.Marshal(entries)
fmt.Println(string(data))
return &entries
}
Figured it out, the problem was that my buffer was too big. If you have a []byte that is too big then go-yaml will read those extra bytes as characters and throw errors. I changed my code to :
func readConf(CONF string) *EntriesList {
confiFile, err := ioutil.ReadFile(CONF)
check(err)
var entries EntriesList
err = yaml.Unmarshal(confiFile, &entries)
check(err)
return &entries
}
And it worked as expected.
From the sounds of it, you're hitting the error block at https://github.com/go-yaml/yaml/blob/53feefa2559fb8dfa8d81baad31be332c97d6c77/readerc.go#L347 , and it looks like that should also be telling you the offset (where in the file it's hitting the problematic character) and character code. If that info is enough to solve your problem, then great. If, on the other hand, the yaml library is spitting out validated yaml that it is not comfortable accepting as input, you should open an issue with the maintainer on Github at https://github.com/go-yaml/yaml/issues