Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
44
ini.go
44
ini.go
@ -13,10 +13,6 @@ type Configuration struct {
|
|||||||
file *ini.File
|
file *ini.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateConfigurationFile(fileLocation string, defaultConfig string) {
|
|
||||||
createFile(fileLocation, []byte(defaultConfig))
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
|
func NewConfiguration(fileLocation string, defaultConfig string) *Configuration {
|
||||||
conf := new(Configuration)
|
conf := new(Configuration)
|
||||||
conf.fileLocation = fileLocation
|
conf.fileLocation = fileLocation
|
||||||
@ -218,7 +214,24 @@ func (config *Configuration) IsSet(path string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (config *Configuration) createAndLoad(defaultConfig string) {
|
func (config *Configuration) createAndLoad(defaultConfig string) {
|
||||||
createFile(config.fileLocation, []byte(defaultConfig))
|
_, 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")
|
||||||
|
}
|
||||||
config.load()
|
config.load()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,24 +251,3 @@ func (config *Configuration) Save() {
|
|||||||
os.Exit(1)
|
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...")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user