Skip to content

Commit

Permalink
feat: Support writing the configuration to an ini file
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Oct 15, 2023
1 parent 770700e commit d7628d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/write.v
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
module config

import os { write_file }
import prantlf.ini
import prantlf.json
import prantlf.path { extname }

pub fn write_config[T](file string, cfg &T) ! {
d.log('write configuration to "%s"', file)
ext := extname(file).to_lower()
match ext {
'.ini' {
d.log_str('marshal ini')
contents := ini.marshal[T](cfg)!
d.log_str('write file')
write_file(file, contents)!
}
'.json' {
d.log_str('marshal json')
contents := json.marshal[T](cfg, json.MarshalOpts{
Expand Down
11 changes: 11 additions & 0 deletions src/write_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ fn testsuite_begin() {
}
}

fn test_write_ini() {
file := '${config.outdir}/config.ini'
if exists(file) {
rm(file)!
}
write_config(file, Test{})!
contents := read_file(file)!
assert contents == 'test = 42
'
}

fn test_write_json() {
file := '${config.outdir}/config.json'
if exists(file) {
Expand Down

0 comments on commit d7628d1

Please sign in to comment.