48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
"os"
|
|
)
|
|
|
|
type Configuration struct {
|
|
file *ini.File
|
|
fileLocation string
|
|
}
|
|
|
|
func NewConfig(fileLocation string) *Configuration {
|
|
conf := new(Configuration)
|
|
conf.fileLocation = fileLocation
|
|
return conf
|
|
}
|
|
|
|
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("[General]\nserver = sfs.example.com\nport = 7392\nusername = user\nauth-token = token\n; http:// or https:// is necessary\n; port is optional\nhttp-server = http://sfs.example.com:80\npublic-key =\n\n[Share]\nsync-interval = 60")
|
|
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 {
|
|
fmt.Printf("Failed to open configuration file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func (config *Configuration) Save() {
|
|
err := config.file.SaveTo(config.fileLocation)
|
|
if err != nil {
|
|
fmt.Printf("Failed to save configuration file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|