-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomlcfg.go
77 lines (64 loc) · 2.43 KB
/
tomlcfg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// /////////////////////////////////////////////////////////////
// 'tomlcfg.go' /
// /
// Copyright (c) 2018 Davsk℠. All Rights Reserved. /
// Use of this source code is governed by an ISC License (ISC) /
// that can be found in the LICENSE file. /
// /
// by David Skinner /
// on July 12, 2018 /
// for Davsk℠ Universe 4.0 project gbase /
// /
// /////////////////////////////////////////////////////////////
// Package tomlcfg provides helper functions
// to load and save toml config files.
package tomlcfg
import (
"log"
"os"
"davsk.net/gbase/pkg/custom_path"
"davsk.net/gbase/pkg/must"
"github.com/BurntSushi/toml"
)
// Config file type extension.
const kToml = ".toml"
// Load receives required title string and struct interface
// and builds file name path from title
// and returns error code after attempting to fill interface.
func Load(title string, v interface{}) error {
// create file path to config file.
fpath := custom_path.Join(title + kToml)
// attempt to read file.
_, err := toml.DecodeFile(fpath, v)
// return error code, nil if successful.
return err
}
// Save receives required title string and struct interface
// and builds file name path from title
// and returns error code after attempting to save interface.
func Save(title string, v interface{}) error {
// create file path to config file.
fpath := custom_path.Join(title + kToml)
// create config file.
f, err := os.Create(fpath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
enc := toml.NewEncoder(f) // create encoder.
err = enc.Encode(v) // write config file.
// return error code, nil if successful.
return err
}
// Load receives required title string and struct interface
// and builds file name path from title
// and after attempting to fill interface panics on failure.
func MustLoad(title string, v interface{}) {
must.Do(Load(title, v))
}
// Save receives required title string and struct interface
// and builds file name path from title
// and after attempting to save interface will panic on failure.
func MustSave(title string, v interface{}) {
must.Do(Save(title, v))
}