2013-07-08 10:34:23 +04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2013-07-10 11:01:32 +04:00
|
|
|
CacheDir string `json:"cachedir"`
|
2013-07-08 10:34:23 +04:00
|
|
|
Connection string `json:"connection"`
|
|
|
|
MappingFile string `json:"mapping"`
|
|
|
|
LimitTo string `json:"limitto"`
|
|
|
|
Srid int `json:"srid"`
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:01:32 +04:00
|
|
|
const defaultSrid = 3857
|
|
|
|
const defaultCacheDir = "/tmp/goposm"
|
|
|
|
|
2013-07-08 10:34:23 +04:00
|
|
|
var (
|
|
|
|
connection = flag.String("connection", "", "connection parameters")
|
2013-07-10 11:01:32 +04:00
|
|
|
cachedir = flag.String("cachedir", defaultCacheDir, "cache directory")
|
2013-07-08 10:34:23 +04:00
|
|
|
mappingFile = flag.String("mapping", "", "mapping file")
|
2013-07-10 11:01:32 +04:00
|
|
|
srid = flag.Int("srid", defaultSrid, "srs id")
|
|
|
|
limitTo = flag.String("limitto", "", "limit to geometries")
|
2013-07-08 10:34:23 +04:00
|
|
|
configFile = flag.String("config", "", "config (json)")
|
|
|
|
)
|
|
|
|
|
|
|
|
func Parse() (*Config, []error) {
|
|
|
|
config := &Config{}
|
|
|
|
if *configFile != "" {
|
|
|
|
f, err := os.Open(*configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(f)
|
|
|
|
|
|
|
|
err = decoder.Decode(&config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *connection != "" {
|
|
|
|
config.Connection = *connection
|
|
|
|
}
|
|
|
|
if config.Srid == 0 {
|
2013-07-10 11:01:32 +04:00
|
|
|
config.Srid = defaultSrid
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
2013-07-10 11:01:32 +04:00
|
|
|
if *srid != defaultSrid {
|
2013-07-08 10:34:23 +04:00
|
|
|
config.Srid = *srid
|
|
|
|
}
|
|
|
|
if *mappingFile != "" {
|
|
|
|
config.MappingFile = *mappingFile
|
|
|
|
}
|
2013-07-10 11:01:32 +04:00
|
|
|
if *limitTo != "" {
|
|
|
|
config.LimitTo = *limitTo
|
|
|
|
}
|
|
|
|
if *cachedir != defaultCacheDir {
|
|
|
|
config.CacheDir = *cachedir
|
|
|
|
}
|
2013-07-08 10:34:23 +04:00
|
|
|
|
|
|
|
errs := checkConfig(config)
|
|
|
|
return config, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkConfig(config *Config) []error {
|
|
|
|
errs := []error{}
|
|
|
|
if config.Srid != 3857 {
|
|
|
|
errs = append(errs, errors.New("srid!=3857 not implemented"))
|
|
|
|
}
|
|
|
|
if config.MappingFile == "" {
|
|
|
|
errs = append(errs, errors.New("missing mapping"))
|
|
|
|
}
|
|
|
|
if config.Connection == "" {
|
|
|
|
errs = append(errs, errors.New("missing connection"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|