Initial commit with config file parsing
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
|||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
#
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module github.com/restitux/ikinuki-server
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/pelletier/go-toml/v2 v2.0.0-beta.8
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 h1:dy81yyLYJDwMTifq24Oi/IslOslRrDSb3jwDggjz3Z0=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.0-beta.8/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
toml "github.com/pelletier/go-toml/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Address string
|
||||||
|
Port int
|
||||||
|
//Age int
|
||||||
|
//Cats []string
|
||||||
|
//Pi float64
|
||||||
|
//Perfection []int
|
||||||
|
//DOB time.Time // requires `import time`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func get_config() (Config, error) {
|
||||||
|
config := Config{
|
||||||
|
Address: "127.0.0.1",
|
||||||
|
Port: 32520,
|
||||||
|
}
|
||||||
|
// Find config path
|
||||||
|
|
||||||
|
// Linux (XDG base directory specification compliant)
|
||||||
|
xdg_config_home := os.Getenv("XDG_CONFIG_HOME")
|
||||||
|
|
||||||
|
var config_dir string
|
||||||
|
|
||||||
|
if xdg_config_home != "" {
|
||||||
|
config_dir = filepath.Join(xdg_config_home, "ikinuki")
|
||||||
|
} else {
|
||||||
|
home := os.Getenv("HOME")
|
||||||
|
config_dir = filepath.Join(home, ".config", "ikinuki")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := os.MkdirAll(config_dir, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
config_path := filepath.Join(config_dir, "server.toml")
|
||||||
|
|
||||||
|
fmt.Printf("Config File Dir: %v\n", config_path)
|
||||||
|
|
||||||
|
// open file, creating it if empty
|
||||||
|
config_file, err := os.OpenFile(config_path, os.O_RDWR | os.O_CREATE, 0755)
|
||||||
|
defer config_file.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := config_file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.Size() == 0 {
|
||||||
|
// if file is empty, write default config
|
||||||
|
data, err := toml.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
config_file.Write(data)
|
||||||
|
return config, nil
|
||||||
|
} else {
|
||||||
|
// try to parse config file
|
||||||
|
data, err := ioutil.ReadAll(config_file)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
err = toml.Unmarshal(data, &config)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
config, err := get_config()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Config: %v\n", config)
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user