Compare commits
2 Commits
2b8b235752
...
228d772219
| Author | SHA1 | Date | |
|---|---|---|---|
| 228d772219 | |||
| 5e1e8723eb |
25
ini.go
25
ini.go
@ -13,7 +13,14 @@ type Configuration struct {
|
|||||||
file *ini.File
|
file *ini.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfiguration(fileLocation string) *Configuration {
|
func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
|
||||||
|
conf := new(Configuration)
|
||||||
|
conf.fileLocation = fileLocation
|
||||||
|
conf.createAndLoad(defaultConfig)
|
||||||
|
return conf
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadConfiguration(fileLocation string) *Configuration {
|
||||||
conf := new(Configuration)
|
conf := new(Configuration)
|
||||||
conf.fileLocation = fileLocation
|
conf.fileLocation = fileLocation
|
||||||
conf.load()
|
conf.load()
|
||||||
@ -206,19 +213,29 @@ func (config *Configuration) IsSet(path string) bool {
|
|||||||
return key.Value() != ""
|
return key.Value() != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Configuration) load() {
|
func (config *Configuration) createAndLoad(defaultConfig string) {
|
||||||
if _, err := os.Stat(config.fileLocation); err != nil {
|
_, err := os.Stat(config.fileLocation)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
file, err := os.Create(config.fileLocation)
|
file, err := os.Create(config.fileLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to create configuration file: %v", err)
|
fmt.Printf("Failed to create configuration file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
_, err = file.WriteString("")
|
_, err = file.WriteString(defaultConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error creating configuration file: %v", err)
|
fmt.Printf("Error creating configuration file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Printf("Error loading configuration file: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Configuration File already exists... loading")
|
||||||
}
|
}
|
||||||
|
config.load()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Configuration) load() {
|
||||||
var err error
|
var err error
|
||||||
config.file, err = ini.Load(config.fileLocation)
|
config.file, err = ini.Load(config.fileLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user