add config file for reuse in import/diff cmd

master
Oliver Tonnhofer 2013-07-08 08:34:23 +02:00
parent db2c8b2cfb
commit 5cb96d0871
2 changed files with 104 additions and 12 deletions

86
config/config.go Normal file
View File

@ -0,0 +1,86 @@
package config
import (
"encoding/json"
"errors"
"flag"
"os"
)
type Config struct {
Cachedir string `json:"cachedir"`
Connection string `json:"connection"`
MappingFile string `json:"mapping"`
LimitTo string `json:"limitto"`
Srid int `json:"srid"`
}
var (
connection = flag.String("connection", "", "connection parameters")
mappingFile = flag.String("mapping", "", "mapping file")
srid = flag.Int("srid", 3857, "srs id")
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 {
config.Srid = 3857
}
if *srid != 3857 {
config.Srid = *srid
}
if *mappingFile != "" {
config.MappingFile = *mappingFile
}
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
}
// cpuprofile = flag.String("cpuprofile", "", "filename of cpu profile output")
// httpprofile = flag.String("httpprofile", "", "bind address for profile server")
// memprofile = flag.String("memprofile", "", "dir name of mem profile output and interval (fname:interval)")
// cachedir = flag.String("cachedir", "/tmp/goposm", "cache directory")
// overwritecache = flag.Bool("overwritecache", false, "overwritecache")
// appendcache = flag.Bool("appendcache", false, "append cache")
// read = flag.String("read", "", "read")
// write = flag.Bool("write", false, "write")
// optimize = flag.Bool("optimize", false, "optimize")
// diff = flag.Bool("diff", false, "enable diff support")
// mappingFile = flag.String("mapping", "", "mapping file")
// deployProduction = flag.Bool("deployproduction", false, "deploy production")
// revertDeploy = flag.Bool("revertdeploy", false, "revert deploy to production")
// removeBackup = flag.Bool("removebackup", false, "remove backups from deploy")
// limitTo = flag.String("limitto", "", "limit to geometries")
// quiet = flag.Bool("quiet", false, "quiet log output")

View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"goposm/cache"
"goposm/config"
"goposm/database"
_ "goposm/database/postgis"
"goposm/geom/clipper"
@ -31,9 +32,7 @@ var (
read = flag.String("read", "", "read")
write = flag.Bool("write", false, "write")
optimize = flag.Bool("optimize", false, "optimize")
connection = flag.String("connection", "", "connection parameters")
diff = flag.Bool("diff", false, "enable diff support")
mappingFile = flag.String("mapping", "", "mapping file")
deployProduction = flag.Bool("deployproduction", false, "deploy production")
revertDeploy = flag.Bool("revertdeploy", false, "revert deploy to production")
removeBackup = flag.Bool("removebackup", false, "remove backups from deploy")
@ -59,6 +58,15 @@ func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
flag.Parse()
conf, errs := config.Parse()
if len(errs) > 0 {
log.Fatal("errors in config/options:")
for _, err := range errs {
log.Fatalf("\t%s", err)
}
logging.Shutdown()
os.Exit(1)
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
@ -129,21 +137,19 @@ func main() {
progress := stats.StatsReporter()
tagmapping, err := mapping.NewMapping(*mappingFile)
tagmapping, err := mapping.NewMapping(conf.MappingFile)
if err != nil {
die(err)
die("mapping file: ", err)
}
var db database.DB
srid := 3857 // TODO
if *write || *deployProduction || *revertDeploy || *removeBackup || *optimize {
connType := database.ConnectionType(*connection)
connType := database.ConnectionType(conf.Connection)
conf := database.Config{
Type: connType,
ConnectionParams: *connection,
Srid: srid,
ConnectionParams: conf.Connection,
Srid: conf.Srid,
}
db, err = database.Open(conf, tagmapping)
if err != nil {
@ -209,7 +215,7 @@ func main() {
relations := osmCache.Relations.Iter()
relWriter := writer.NewRelationWriter(osmCache, diffCache, relations,
db, polygonsTagMatcher, progress, srid)
db, polygonsTagMatcher, progress, conf.Srid)
relWriter.SetClipper(geometryClipper)
relWriter.Start()
@ -219,7 +225,7 @@ func main() {
ways := osmCache.Ways.Iter()
wayWriter := writer.NewWayWriter(osmCache, diffCache, ways, db,
lineStringsTagMatcher, polygonsTagMatcher, progress, srid)
lineStringsTagMatcher, polygonsTagMatcher, progress, conf.Srid)
wayWriter.SetClipper(geometryClipper)
wayWriter.Start()
@ -229,7 +235,7 @@ func main() {
nodes := osmCache.Nodes.Iter()
nodeWriter := writer.NewNodeWriter(osmCache, nodes, db,
pointsTagMatcher, progress, srid)
pointsTagMatcher, progress, conf.Srid)
nodeWriter.SetClipper(geometryClipper)
nodeWriter.Start()