2013-07-08 10:34:23 +04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
2013-08-01 18:19:20 +04:00
|
|
|
"fmt"
|
2013-07-25 13:28:16 +04:00
|
|
|
"log"
|
2013-07-08 10:34:23 +04:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2013-11-11 13:39:44 +04:00
|
|
|
CacheDir string `json:"cachedir"`
|
|
|
|
Connection string `json:"connection"`
|
|
|
|
MappingFile string `json:"mapping"`
|
|
|
|
LimitTo string `json:"limitto"`
|
2013-11-04 11:55:10 +04:00
|
|
|
LimitToCacheBuffer float64 `json:"limitto_cache_buffer"`
|
2013-11-11 13:39:44 +04:00
|
|
|
Srid int `json:"srid"`
|
2013-12-02 16:10:42 +04:00
|
|
|
Schemas Schemas `json:"schemas"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Schemas struct {
|
|
|
|
Import string `json:"import"`
|
|
|
|
Production string `json:"production"`
|
|
|
|
Backup string `json:"backup"`
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:01:32 +04:00
|
|
|
const defaultSrid = 3857
|
2013-08-29 17:44:15 +04:00
|
|
|
const defaultCacheDir = "/tmp/imposm3"
|
2013-12-02 16:10:42 +04:00
|
|
|
const defaultSchemaImport = "import"
|
|
|
|
const defaultSchemaProduction = "production"
|
|
|
|
const defaultSchemaBackup = "backup"
|
2013-07-10 11:01:32 +04:00
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
var ImportFlags = flag.NewFlagSet("import", flag.ExitOnError)
|
2013-08-01 18:19:20 +04:00
|
|
|
var DiffFlags = flag.NewFlagSet("diff", flag.ExitOnError)
|
2013-07-25 13:28:16 +04:00
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
type _BaseOptions struct {
|
2013-11-11 13:39:44 +04:00
|
|
|
Connection string
|
|
|
|
CacheDir string
|
|
|
|
MappingFile string
|
|
|
|
Srid int
|
|
|
|
LimitTo string
|
2013-11-04 11:55:10 +04:00
|
|
|
LimitToCacheBuffer float64
|
2013-11-11 13:39:44 +04:00
|
|
|
ConfigFile string
|
|
|
|
Httpprofile string
|
|
|
|
Quiet bool
|
2013-12-02 16:10:42 +04:00
|
|
|
Schemas Schemas
|
2013-07-25 13:28:16 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
func (o *_BaseOptions) updateFromConfig() error {
|
|
|
|
conf := &Config{
|
|
|
|
CacheDir: defaultCacheDir,
|
|
|
|
Srid: defaultSrid,
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.ConfigFile != "" {
|
|
|
|
f, err := os.Open(o.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(f)
|
|
|
|
|
|
|
|
err = decoder.Decode(&conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-12-02 16:10:42 +04:00
|
|
|
|
|
|
|
if o.Schemas.Import == defaultSchemaImport {
|
|
|
|
o.Schemas.Import = conf.Schemas.Import
|
|
|
|
}
|
|
|
|
if o.Schemas.Production == defaultSchemaProduction {
|
|
|
|
o.Schemas.Production = conf.Schemas.Production
|
|
|
|
}
|
|
|
|
if o.Schemas.Backup == defaultSchemaBackup {
|
|
|
|
o.Schemas.Backup = conf.Schemas.Backup
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
if o.Connection == "" {
|
|
|
|
o.Connection = conf.Connection
|
|
|
|
}
|
|
|
|
if conf.Srid == 0 {
|
|
|
|
conf.Srid = defaultSrid
|
|
|
|
}
|
|
|
|
if o.Srid != defaultSrid {
|
|
|
|
o.Srid = conf.Srid
|
|
|
|
}
|
|
|
|
if o.MappingFile == "" {
|
|
|
|
o.MappingFile = conf.MappingFile
|
|
|
|
}
|
|
|
|
if o.LimitTo == "" {
|
|
|
|
o.LimitTo = conf.LimitTo
|
|
|
|
}
|
2013-11-04 11:55:10 +04:00
|
|
|
if o.LimitToCacheBuffer == 0.0 {
|
|
|
|
o.LimitToCacheBuffer = conf.LimitToCacheBuffer
|
2013-08-01 18:19:20 +04:00
|
|
|
}
|
|
|
|
if o.CacheDir == defaultCacheDir {
|
|
|
|
o.CacheDir = conf.CacheDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *_BaseOptions) check() []error {
|
|
|
|
errs := []error{}
|
|
|
|
if o.Srid != 3857 {
|
|
|
|
errs = append(errs, errors.New("srid!=3857 not implemented"))
|
|
|
|
}
|
|
|
|
if o.MappingFile == "" {
|
|
|
|
errs = append(errs, errors.New("missing mapping"))
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2013-07-25 13:28:16 +04:00
|
|
|
type _ImportOptions struct {
|
|
|
|
Overwritecache bool
|
|
|
|
Appendcache bool
|
|
|
|
Read string
|
|
|
|
Write bool
|
|
|
|
Optimize bool
|
|
|
|
Diff bool
|
|
|
|
DeployProduction bool
|
|
|
|
RevertDeploy bool
|
|
|
|
RemoveBackup bool
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
var BaseOptions = _BaseOptions{}
|
2013-07-25 13:28:16 +04:00
|
|
|
var ImportOptions = _ImportOptions{}
|
2013-08-01 18:19:20 +04:00
|
|
|
|
|
|
|
func addBaseFlags(flags *flag.FlagSet) {
|
|
|
|
flags.StringVar(&BaseOptions.Connection, "connection", "", "connection parameters")
|
|
|
|
flags.StringVar(&BaseOptions.CacheDir, "cachedir", defaultCacheDir, "cache directory")
|
|
|
|
flags.StringVar(&BaseOptions.MappingFile, "mapping", "", "mapping file")
|
|
|
|
flags.IntVar(&BaseOptions.Srid, "srid", defaultSrid, "srs id")
|
|
|
|
flags.StringVar(&BaseOptions.LimitTo, "limitto", "", "limit to geometries")
|
2013-11-04 11:55:10 +04:00
|
|
|
flags.Float64Var(&BaseOptions.LimitToCacheBuffer, "limittocachebuffer", 0.0, "limit to buffer for cache")
|
2013-08-01 18:19:20 +04:00
|
|
|
flags.StringVar(&BaseOptions.ConfigFile, "config", "", "config (json)")
|
2013-10-30 14:14:34 +04:00
|
|
|
flags.StringVar(&BaseOptions.Httpprofile, "httpprofile", "", "bind address for profile server")
|
2013-11-11 13:39:44 +04:00
|
|
|
flags.BoolVar(&BaseOptions.Quiet, "quiet", false, "quiet log output")
|
2013-12-02 16:10:42 +04:00
|
|
|
flags.StringVar(&BaseOptions.Schemas.Import, "dbschema-import", defaultSchemaImport, "db schema for imports")
|
|
|
|
flags.StringVar(&BaseOptions.Schemas.Production, "dbschema-production", defaultSchemaProduction, "db schema for production")
|
|
|
|
flags.StringVar(&BaseOptions.Schemas.Backup, "dbschema-backup", defaultSchemaBackup, "db schema for backups")
|
2013-07-25 13:28:16 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
func UsageImport() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s %s [args]\n\n", os.Args[0], os.Args[1])
|
|
|
|
ImportFlags.PrintDefaults()
|
|
|
|
os.Exit(2)
|
2013-07-25 13:28:16 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
func UsageDiff() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s %s [args] [.osc.gz, ...]\n\n", os.Args[0], os.Args[1])
|
|
|
|
DiffFlags.PrintDefaults()
|
|
|
|
os.Exit(2)
|
2013-07-25 13:28:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2013-08-01 18:19:20 +04:00
|
|
|
ImportFlags.Usage = UsageImport
|
|
|
|
DiffFlags.Usage = UsageDiff
|
|
|
|
|
|
|
|
addBaseFlags(DiffFlags)
|
|
|
|
addBaseFlags(ImportFlags)
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.Overwritecache, "overwritecache", false, "overwritecache")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.Appendcache, "appendcache", false, "append cache")
|
|
|
|
ImportFlags.StringVar(&ImportOptions.Read, "read", "", "read")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.Write, "write", false, "write")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.Optimize, "optimize", false, "optimize")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.Diff, "diff", false, "enable diff support")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.DeployProduction, "deployproduction", false, "deploy production")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.RevertDeploy, "revertdeploy", false, "revert deploy to production")
|
|
|
|
ImportFlags.BoolVar(&ImportOptions.RemoveBackup, "removebackup", false, "remove backups from deploy")
|
2013-07-25 13:28:16 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
func ParseImport(args []string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
UsageImport()
|
|
|
|
}
|
2013-07-25 13:28:16 +04:00
|
|
|
err := ImportFlags.Parse(args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-08-01 18:19:20 +04:00
|
|
|
err = BaseOptions.updateFromConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2013-07-26 17:59:03 +04:00
|
|
|
}
|
2013-08-01 18:19:20 +04:00
|
|
|
errs := BaseOptions.check()
|
|
|
|
if len(errs) != 0 {
|
|
|
|
reportErrors(errs)
|
|
|
|
UsageImport()
|
2013-07-10 11:50:50 +04:00
|
|
|
}
|
2013-08-01 18:19:20 +04:00
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
func ParseDiffImport(args []string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
UsageDiff()
|
2013-07-10 11:01:32 +04:00
|
|
|
}
|
2013-08-01 18:19:20 +04:00
|
|
|
err := DiffFlags.Parse(args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2013-07-10 11:01:32 +04:00
|
|
|
}
|
2013-07-08 10:34:23 +04:00
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
err = BaseOptions.updateFromConfig()
|
2013-07-26 17:59:03 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
errs := BaseOptions.check()
|
|
|
|
if len(errs) != 0 {
|
|
|
|
reportErrors(errs)
|
|
|
|
UsageDiff()
|
2013-07-26 17:59:03 +04:00
|
|
|
}
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 18:19:20 +04:00
|
|
|
func reportErrors(errs []error) {
|
|
|
|
fmt.Println("errors in config/options:")
|
|
|
|
for _, err := range errs {
|
|
|
|
fmt.Printf("\t%s\n", err)
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
2013-08-01 18:19:20 +04:00
|
|
|
os.Exit(1)
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|