2013-07-08 10:34:23 +04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
2013-07-25 13:28:16 +04:00
|
|
|
"log"
|
2013-07-08 10:34:23 +04:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2013-07-30 10:30:06 +04:00
|
|
|
CacheDir string `json:"cachedir"`
|
|
|
|
Connection string `json:"connection"`
|
|
|
|
MappingFile string `json:"mapping"`
|
|
|
|
LimitTo string `json:"limitto"`
|
|
|
|
LimitToCacheBuffer float64 `json:"limitto_cache_buffer"`
|
|
|
|
Srid int `json:"srid"`
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:01:32 +04:00
|
|
|
const defaultSrid = 3857
|
|
|
|
const defaultCacheDir = "/tmp/goposm"
|
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
var ImportFlags = flag.NewFlagSet("import", flag.ExitOnError)
|
|
|
|
var DiffImportFlags = flag.NewFlagSet("diff", flag.ExitOnError)
|
2013-07-25 13:28:16 +04:00
|
|
|
|
|
|
|
type ImportBaseOptions struct {
|
2013-07-30 10:30:06 +04:00
|
|
|
Connection string
|
|
|
|
CacheDir string
|
|
|
|
MappingFile string
|
|
|
|
Srid int
|
|
|
|
LimitTo string
|
|
|
|
LimitToCacheBuffer float64
|
|
|
|
ConfigFile string
|
2013-07-25 13:28:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type _ImportOptions struct {
|
|
|
|
Base ImportBaseOptions
|
|
|
|
Cpuprofile string
|
|
|
|
Httpprofile string
|
|
|
|
Memprofile string
|
|
|
|
Overwritecache bool
|
|
|
|
Appendcache bool
|
|
|
|
Read string
|
|
|
|
Write bool
|
|
|
|
Optimize bool
|
|
|
|
Diff bool
|
|
|
|
DeployProduction bool
|
|
|
|
RevertDeploy bool
|
|
|
|
RemoveBackup bool
|
|
|
|
Quiet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type _DiffImportOptions struct {
|
|
|
|
Base ImportBaseOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
var ImportOptions = _ImportOptions{}
|
|
|
|
var DiffImportOptions = _DiffImportOptions{}
|
|
|
|
|
|
|
|
func addBaseFlags(flags *flag.FlagSet, baseOptions *ImportBaseOptions) {
|
|
|
|
flags.StringVar(&baseOptions.Connection, "connection", "", "connection parameters")
|
2013-07-26 17:59:03 +04:00
|
|
|
flags.StringVar(&baseOptions.CacheDir, "cachedir", defaultCacheDir, "cache directory")
|
2013-07-25 13:28:16 +04:00
|
|
|
flags.StringVar(&baseOptions.MappingFile, "mapping", "", "mapping file")
|
|
|
|
flags.IntVar(&baseOptions.Srid, "srid", defaultSrid, "srs id")
|
|
|
|
flags.StringVar(&baseOptions.LimitTo, "limitto", "", "limit to geometries")
|
|
|
|
flags.StringVar(&baseOptions.ConfigFile, "config", "", "config (json)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addImportFlags(flags *flag.FlagSet, options *_ImportOptions) {
|
|
|
|
flags.StringVar(&options.Cpuprofile, "cpuprofile", "", "filename of cpu profile output")
|
|
|
|
flags.StringVar(&options.Httpprofile, "httpprofile", "", "bind address for profile server")
|
|
|
|
flags.StringVar(&options.Memprofile, "memprofile", "", "dir name of mem profile output and interval (fname:interval)")
|
|
|
|
flags.BoolVar(&options.Overwritecache, "overwritecache", false, "overwritecache")
|
|
|
|
flags.BoolVar(&options.Appendcache, "appendcache", false, "append cache")
|
|
|
|
flags.StringVar(&options.Read, "read", "", "read")
|
|
|
|
flags.BoolVar(&options.Write, "write", false, "write")
|
|
|
|
flags.BoolVar(&options.Optimize, "optimize", false, "optimize")
|
|
|
|
flags.BoolVar(&options.Diff, "diff", false, "enable diff support")
|
|
|
|
flags.BoolVar(&options.DeployProduction, "deployproduction", false, "deploy production")
|
|
|
|
flags.BoolVar(&options.RevertDeploy, "revertdeploy", false, "revert deploy to production")
|
|
|
|
flags.BoolVar(&options.RemoveBackup, "removebackup", false, "remove backups from deploy")
|
|
|
|
flags.BoolVar(&options.Quiet, "quiet", false, "quiet log output")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addDiffImportFlags(flags *flag.FlagSet, options *_DiffImportOptions) {
|
|
|
|
// no options yet
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
addBaseFlags(ImportFlags, &ImportOptions.Base)
|
|
|
|
addImportFlags(ImportFlags, &ImportOptions)
|
|
|
|
addBaseFlags(DiffImportFlags, &DiffImportOptions.Base)
|
|
|
|
addDiffImportFlags(DiffImportFlags, &DiffImportOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// var (
|
|
|
|
// connection = flag.String("connection", "", "connection parameters")
|
|
|
|
// cachedir = flag.String("cachedir", defaultCacheDir, "cache directory")
|
|
|
|
// mappingFile = flag.String("mapping", "", "mapping file")
|
|
|
|
// srid = flag.Int("srid", defaultSrid, "srs id")
|
|
|
|
// limitTo = flag.String("limitto", "", "limit to geometries")
|
|
|
|
// configFile = flag.String("config", "", "config (json)")
|
|
|
|
// )
|
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
func ParseImport(args []string) []error {
|
2013-07-25 13:28:16 +04:00
|
|
|
err := ImportFlags.Parse(args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
errs := updateBaseOpts(&ImportOptions.Base)
|
|
|
|
if errs != nil {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = checkOptions(&ImportOptions.Base)
|
|
|
|
return errs
|
|
|
|
}
|
2013-07-08 10:34:23 +04:00
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
func updateBaseOpts(opts *ImportBaseOptions) []error {
|
|
|
|
|
|
|
|
conf := &Config{
|
2013-07-10 11:50:50 +04:00
|
|
|
CacheDir: defaultCacheDir,
|
|
|
|
Srid: defaultSrid,
|
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
|
|
|
|
if opts.ConfigFile != "" {
|
|
|
|
f, err := os.Open(opts.ConfigFile)
|
2013-07-08 10:34:23 +04:00
|
|
|
if err != nil {
|
2013-07-26 17:59:03 +04:00
|
|
|
return []error{err}
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(f)
|
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
err = decoder.Decode(&conf)
|
2013-07-08 10:34:23 +04:00
|
|
|
if err != nil {
|
2013-07-26 17:59:03 +04:00
|
|
|
return []error{err}
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.Connection == "" {
|
|
|
|
opts.Connection = conf.Connection
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if conf.Srid == 0 {
|
|
|
|
conf.Srid = defaultSrid
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.Srid != defaultSrid {
|
|
|
|
opts.Srid = conf.Srid
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.MappingFile == "" {
|
|
|
|
opts.MappingFile = conf.MappingFile
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.LimitTo == "" {
|
|
|
|
opts.LimitTo = conf.LimitTo
|
2013-07-10 11:01:32 +04:00
|
|
|
}
|
2013-07-30 10:30:06 +04:00
|
|
|
if opts.LimitToCacheBuffer == 0.0 {
|
|
|
|
opts.LimitToCacheBuffer = conf.LimitToCacheBuffer
|
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.CacheDir == defaultCacheDir {
|
|
|
|
opts.CacheDir = conf.CacheDir
|
2013-07-10 11:01:32 +04:00
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
return nil
|
|
|
|
}
|
2013-07-08 10:34:23 +04:00
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
func ParseDiffImport(args []string) []error {
|
|
|
|
err := DiffImportFlags.Parse(args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := updateBaseOpts(&DiffImportOptions.Base)
|
|
|
|
if errs != nil {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = checkOptions(&DiffImportOptions.Base)
|
|
|
|
|
|
|
|
return errs
|
2013-07-08 10:34:23 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:59:03 +04:00
|
|
|
func checkOptions(opts *ImportBaseOptions) []error {
|
2013-07-08 10:34:23 +04:00
|
|
|
errs := []error{}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.Srid != 3857 {
|
2013-07-08 10:34:23 +04:00
|
|
|
errs = append(errs, errors.New("srid!=3857 not implemented"))
|
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.MappingFile == "" {
|
2013-07-08 10:34:23 +04:00
|
|
|
errs = append(errs, errors.New("missing mapping"))
|
|
|
|
}
|
2013-07-26 17:59:03 +04:00
|
|
|
if opts.Connection == "" {
|
2013-07-08 10:34:23 +04:00
|
|
|
errs = append(errs, errors.New("missing connection"))
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|