package config
|
|
import (
|
"bytes"
|
"encoding/json"
|
"fmt"
|
"io/ioutil"
|
"os"
|
)
|
|
type Config struct {
|
WebPort string `json:"web_port"`
|
SqlAddr string `json:"sql_addr"`
|
SqlDBName string `json:"sql_db_name"`
|
SqlUsername string `json:"sql_username"`
|
SqlPassword string `json:"sql_password"`
|
}
|
|
const configPath = "config.json"
|
|
var Options Config
|
|
// Init is an exported method that takes the environment starts the viper
|
// (external lib) and returns the configuration struct.
|
func init() {
|
}
|
|
func Load() {
|
if !pathExists(configPath) {
|
SaveConfig()
|
} else {
|
file, _ := os.Open(configPath)
|
defer file.Close()
|
|
fd := json.NewDecoder(file)
|
|
fd.Decode(&Options)
|
|
fmt.Printf("%v\n", Options)
|
}
|
}
|
|
func SaveConfig() error {
|
b, err := json.Marshal(Options)
|
if err == nil {
|
var out bytes.Buffer
|
err = json.Indent(&out, b, "", " ")
|
ioutil.WriteFile(configPath, out.Bytes(), 0644)
|
}
|
|
return err
|
}
|
|
func pathExists(path string) bool {
|
_, err := os.Stat(path)
|
if err == nil {
|
return true
|
}
|
|
fmt.Printf("%v", err.Error())
|
return false
|
}
|