Users must now edit config file and relaunch
This commit is contained in:
@@ -2,15 +2,16 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
toml "github.com/pelletier/go-toml/v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
toml "github.com/pelletier/go-toml/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Address string
|
Address string
|
||||||
Port int
|
Port int
|
||||||
|
ScanDirectory string
|
||||||
//Age int
|
//Age int
|
||||||
//Cats []string
|
//Cats []string
|
||||||
//Pi float64
|
//Pi float64
|
||||||
@@ -18,29 +19,30 @@ type Config struct {
|
|||||||
//DOB time.Time // requires `import time`
|
//DOB time.Time // requires `import time`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func get_config_dir() string {
|
||||||
func get_config() (Config, error) {
|
// Find config directory
|
||||||
config := Config{
|
|
||||||
Address: "127.0.0.1",
|
|
||||||
Port: 32520,
|
|
||||||
}
|
|
||||||
// Find config path
|
|
||||||
|
|
||||||
// Linux (XDG base directory specification compliant)
|
// Linux (XDG base directory specification compliant)
|
||||||
xdg_config_home := os.Getenv("XDG_CONFIG_HOME")
|
xdg_config_home := os.Getenv("XDG_CONFIG_HOME")
|
||||||
|
|
||||||
var config_dir string
|
|
||||||
|
|
||||||
if xdg_config_home != "" {
|
if xdg_config_home != "" {
|
||||||
config_dir = filepath.Join(xdg_config_home, "ikinuki")
|
return filepath.Join(xdg_config_home, "ikinuki")
|
||||||
} else {
|
} else {
|
||||||
home := os.Getenv("HOME")
|
home := os.Getenv("HOME")
|
||||||
config_dir = filepath.Join(home, ".config", "ikinuki")
|
return filepath.Join(home, ".config", "ikinuki")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func get_config(config_dir string) (*Config, error) {
|
||||||
|
config := Config{
|
||||||
|
Address: "127.0.0.1",
|
||||||
|
Port: 32520,
|
||||||
|
ScanDirectory: "FILL IN",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := os.MkdirAll(config_dir, 0755)
|
err := os.MkdirAll(config_dir, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return &config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
config_path := filepath.Join(config_dir, "server.toml")
|
config_path := filepath.Join(config_dir, "server.toml")
|
||||||
@@ -48,48 +50,54 @@ func get_config() (Config, error) {
|
|||||||
fmt.Printf("Config File Dir: %v\n", config_path)
|
fmt.Printf("Config File Dir: %v\n", config_path)
|
||||||
|
|
||||||
// open file, creating it if empty
|
// open file, creating it if empty
|
||||||
config_file, err := os.OpenFile(config_path, os.O_RDWR | os.O_CREATE, 0755)
|
config_file, err := os.OpenFile(config_path, os.O_RDWR|os.O_CREATE, 0755)
|
||||||
defer config_file.Close()
|
defer config_file.Close()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return &config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fi, err := config_file.Stat()
|
fi, err := config_file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return &config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.Size() == 0 {
|
if fi.Size() == 0 {
|
||||||
// if file is empty, write default config
|
// if file is empty, write default config
|
||||||
data, err := toml.Marshal(config)
|
data, err := toml.Marshal(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return &config, err
|
||||||
}
|
}
|
||||||
config_file.Write(data)
|
config_file.Write(data)
|
||||||
return config, nil
|
// return nil as the user must edit the config file
|
||||||
|
return nil, nil
|
||||||
} else {
|
} else {
|
||||||
// try to parse config file
|
// try to parse config file
|
||||||
data, err := ioutil.ReadAll(config_file)
|
data, err := ioutil.ReadAll(config_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return &config, err
|
||||||
}
|
}
|
||||||
err = toml.Unmarshal(data, &config)
|
err = toml.Unmarshal(data, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return &config, err
|
||||||
}
|
}
|
||||||
return config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config, err := get_config()
|
|
||||||
|
config_dir := get_config_dir()
|
||||||
|
|
||||||
|
config, err := get_config(config_dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
if config == nil {
|
||||||
fmt.Printf("Config: %v\n", config)
|
fmt.Printf("Created config file at %v, please edit and launch again\n", config_dir)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Config: %v\n", config)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user