While writing into TOML file using go-toml parser, I'm seeing all duplicate entries. Which one is correct about tree.WriteTo() function? a. overwrites the fields in the file b. appends the tree to the file? i.e., to the existing file, write the tree content again.
I wanted to achieve the update operation to the existing config parameter (present in TOML file).
I tried this:
//Read the config file
tree, _ := toml.LoadFile("/home/robot/test.toml")
//Read user input
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
//Check whether the input config parameter is present in the file or not
configArray := strings.Split(string(reqBody), ";")
for index, each := range configArray {
config := strings.Split(each, "=")
fmt.Println("Param name : ", config[0])
fmt.Println("Param value : ", config[1])
fmt.Println(index)
isPresent := tree.Has(config[0])
fmt.Println(isPresent)
if isPresent == true {
tree.Set(config[0], config[1])
}else {
fmt.Println("Config Parameter not found")
}
}
// Now the tree has updated values, update the file.
outputReader, err = os.OpenFile("/home/robot/test.toml",os.O_RDWR,0644)
if err != nil {
fmt.Println("Error opening the file")
}
var numBytes int64
numBytes, err = tree.WriteTo(outputReader)
if err != nil {
fmt.Println("Error writing to the file")
}
fmt.Println(numBytes)
If tree.WriteTo() is meant to write entire tree content to file, then is there any API or way to update the existing configurations in TOML file?
Output logs:
TOML content (i.e., dump of tree):
Sep 03 13:27:33 mn-0 janus[31157]: [http]
Sep 03 13:27:33 mn-0 janus[31157]: enableAudit = true
Sep 03 13:27:33 mn-0 janus[31157]: idleTimeout = "600s"
Sep 03 13:27:33 mn-0 janus[31157]: logLevel = "debug"
Sep 03 13:27:33 mn-0 janus[31157]: port = 443
Sep 03 13:27:33 mn-0 janus[31157]: readTimeout = "10s"
Sep 03 13:27:33 mn-0 janus[31157]: tlsMode = true
Sep 03 13:27:33 mn-0 janus[31157]: writeTimeout = "10s"
Sep 03 13:27:33 mn-0 janus[31157]: [http.cred]
Sep 03 13:27:33 mn-0 janus[31157]: sessionValidity = "1h"
Sep 03 13:27:33 mn-0 janus[31157]: strictSecureMode = false
Sep 03 13:27:33 mn-0 janus[31157]: users = ["robot"]
Sep 03 13:27:33 mn-0 janus[31157]: [http.ipConfig]
Sep 03 13:27:33 mn-0 janus[31157]: ipAddr = ""
Sep 03 13:27:33 mn-0 janus[31157]: role = "ssh"
Input invalid key:
Sep 03 13:27:33 mn-0 janus[31157]: Param name : http.enableAudt
Sep 03 13:27:33 mn-0 janus[31157]: Param value : true
Sep 03 13:27:33 mn-0 janus[31157]: 0
Sep 03 13:27:33 mn-0 janus[31157]: false
Input valid Key:
Sep 03 13:27:33 mn-0 janus[31157]: Param name : http.enableAudit
Sep 03 13:27:33 mn-0 janus[31157]: Param value : false
Sep 03 13:27:33 mn-0 janus[31157]: 1
Sep 03 13:27:33 mn-0 janus[31157]: false
One more question on unmarshal() or configuration validation while reading, Say my structure is like this.
type IPConfig struct { User string Role string IPAddr string }
type MyConfiguration struct { MyConfiguration MyConfiguration }
a. If the TOML file has this:
[ipConfig]
role = "ssh"
ipAddr = ""
i.e., it doesn't have one more parameter, "User". How do I catch this while Unmarshal? At least Unmarshal() will not throw any error here.
b. If the TOML file has this:
[ipConfig]
role = "ssh"
ipAddr = ""
user = "lauren"
abc = "xyz"
i.e., it has extra configuration parameter "abc". How to catch this? Unmarshal() didn't throw any error even for this.
Any way to get these validation errors?
For question 1:
It is hard to figure out what may be going on without having access to the content of the files. If you believe this is a bug, please open an issue on Github, including the reproduction steps. Thank you!
For question 2:
You need to remove or truncate the file before writing to it.
Go-toml does not know that you are writing to a file. WriteTo
takes a io.Writer
, which just signifies "something you can write to". So when you open the file for read/write, the "writer" part of the file has a 0 offset. So when toml writes to it (using WriteTo
), it will just be replacing bytes to the file.
If you want to overwrite the content of the file using WriteTo
, you need to call .truncate(0)
or similar on the file before writing to it.
Note: as of writing, comments are discarded during Load
. There is an issue on Github asking for that feature.
For question 3:
There is no support for erroring on missing keys or on extra keys. There is an issue open at the moment to support the latter.