Add CreateConfigurationFile and util funcs

This commit is contained in:
Steven Tracey 2025-09-24 07:15:34 -04:00
parent 228d772219
commit 7d010c4b7b

44
ini.go
View File

@ -13,6 +13,10 @@ type Configuration struct {
file *ini.File
}
func CreateConfigurationFile(fileLocation string, defaultConfig string) {
createFile(fileLocation, []byte(defaultConfig))
}
func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
conf := new(Configuration)
conf.fileLocation = fileLocation
@ -214,24 +218,7 @@ func (config *Configuration) IsSet(path string) bool {
}
func (config *Configuration) createAndLoad(defaultConfig string) {
_, err := os.Stat(config.fileLocation)
if errors.Is(err, os.ErrNotExist) {
file, err := os.Create(config.fileLocation)
if err != nil {
fmt.Printf("Failed to create configuration file: %v", err)
os.Exit(1)
}
_, err = file.WriteString(defaultConfig)
if err != nil {
fmt.Printf("Error creating configuration file: %v", err)
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")
}
createFile(config.fileLocation, []byte(defaultConfig))
config.load()
}
@ -251,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...")
}
}