Compare commits
3 Commits
2b8b235752
...
v0.1.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d010c4b7b | |||
| 228d772219 | |||
| 5e1e8723eb |
51
ini.go
51
ini.go
@@ -13,7 +13,18 @@ type Configuration struct {
|
||||
file *ini.File
|
||||
}
|
||||
|
||||
func NewConfiguration(fileLocation string) *Configuration {
|
||||
func CreateConfigurationFile(fileLocation string, defaultConfig string) {
|
||||
createFile(fileLocation, []byte(defaultConfig))
|
||||
}
|
||||
|
||||
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.fileLocation = fileLocation
|
||||
conf.load()
|
||||
@@ -206,19 +217,12 @@ func (config *Configuration) IsSet(path string) bool {
|
||||
return key.Value() != ""
|
||||
}
|
||||
|
||||
func (config *Configuration) createAndLoad(defaultConfig string) {
|
||||
createFile(config.fileLocation, []byte(defaultConfig))
|
||||
config.load()
|
||||
}
|
||||
|
||||
func (config *Configuration) load() {
|
||||
if _, err := os.Stat(config.fileLocation); err != nil {
|
||||
file, err := os.Create(config.fileLocation)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create configuration file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = file.WriteString("")
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating configuration file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
var err error
|
||||
config.file, err = ini.Load(config.fileLocation)
|
||||
if err != nil {
|
||||
@@ -234,3 +238,24 @@ func (config *Configuration) Save() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func createFile(fileLocation string, data []byte) {
|
||||
_, err := os.Stat(fileLocation)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
file, err := os.Create(fileLocation)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = file.Write(data)
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing to file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else if err != nil {
|
||||
fmt.Printf("Error creating file: %v", err)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
fmt.Printf("File already exists...")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user