Move flag handling out of config into main
This places all the flag definitions together, making them easier to find, and makes it possible (in the future) to use db for convenience functions when we have a Go API, without having flag space contaminated by cayley main flags.
This commit is contained in:
parent
cce0f88803
commit
df2c5e3c2a
2 changed files with 77 additions and 84 deletions
73
cayley.go
73
cayley.go
|
|
@ -29,6 +29,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/barakmich/glog"
|
"github.com/barakmich/glog"
|
||||||
|
|
||||||
|
|
@ -50,11 +51,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tripleFile = flag.String("triples", "", "Triple File to load before going to REPL.")
|
tripleFile = flag.String("triples", "", "Triple File to load before going to REPL.")
|
||||||
tripleType = flag.String("format", "cquad", `Triple format to use for loading ("cquad" or "nquad").`)
|
tripleType = flag.String("format", "cquad", `Triple format to use for loading ("cquad" or "nquad").`)
|
||||||
cpuprofile = flag.String("prof", "", "Output profiling file.")
|
cpuprofile = flag.String("prof", "", "Output profiling file.")
|
||||||
queryLanguage = flag.String("query_lang", "gremlin", "Use this parser as the query language.")
|
queryLanguage = flag.String("query_lang", "gremlin", "Use this parser as the query language.")
|
||||||
configFile = flag.String("config", "", "Path to an explicit configuration file.")
|
configFile = flag.String("config", "", "Path to an explicit configuration file.")
|
||||||
|
databasePath = flag.String("dbpath", "/tmp/testdb", "Path to the database.")
|
||||||
|
databaseBackend = flag.String("db", "memstore", "Database Backend.")
|
||||||
|
replicationBackend = flag.String("replication", "single", "Replication method.")
|
||||||
|
host = flag.String("host", "127.0.0.1", "Host to listen on (defaults to all).")
|
||||||
|
loadSize = flag.Int("load_size", 10000, "Size of triplesets to load")
|
||||||
|
port = flag.String("port", "64210", "Port to listen on.")
|
||||||
|
readOnly = flag.Bool("read_only", false, "Disable writing via HTTP.")
|
||||||
|
timeout = flag.Duration("timeout", 30*time.Second, "Elapsed time until an individual query times out.")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Filled in by `go build ldflags="-X main.VERSION `ver`"`.
|
// Filled in by `go build ldflags="-X main.VERSION `ver`"`.
|
||||||
|
|
@ -78,6 +87,58 @@ func Usage() {
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func configFrom(file string) *config.Config {
|
||||||
|
// Find the file...
|
||||||
|
if file != "" {
|
||||||
|
if _, err := os.Stat(file); os.IsNotExist(err) {
|
||||||
|
glog.Fatalln("Cannot find specified configuration file", file, ", aborting.")
|
||||||
|
}
|
||||||
|
} else if _, err := os.Stat(os.Getenv("CAYLEY_CFG")); err == nil {
|
||||||
|
file = os.Getenv("CAYLEY_CFG")
|
||||||
|
} else if _, err := os.Stat("/etc/cayley.cfg"); err == nil {
|
||||||
|
file = "/etc/cayley.cfg"
|
||||||
|
}
|
||||||
|
if file == "" {
|
||||||
|
glog.Infoln("Couldn't find a config file in either $CAYLEY_CFG or /etc/cayley.cfg. Going by flag defaults only.")
|
||||||
|
}
|
||||||
|
cfg, err := config.Load(file)
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.DatabasePath == "" {
|
||||||
|
cfg.DatabasePath = *databasePath
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.DatabaseType == "" {
|
||||||
|
cfg.DatabaseType = *databaseBackend
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ReplicationType == "" {
|
||||||
|
cfg.ReplicationType = *replicationBackend
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ListenHost == "" {
|
||||||
|
cfg.ListenHost = *host
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ListenPort == "" {
|
||||||
|
cfg.ListenPort = *port
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Timeout == 0 {
|
||||||
|
cfg.Timeout = *timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.LoadSize == 0 {
|
||||||
|
cfg.LoadSize = *loadSize
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.ReadOnly = cfg.ReadOnly || *readOnly
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// No command? It's time for usage.
|
// No command? It's time for usage.
|
||||||
if len(os.Args) == 1 {
|
if len(os.Args) == 1 {
|
||||||
|
|
@ -98,7 +159,7 @@ func main() {
|
||||||
glog.Infoln(buildString)
|
glog.Infoln(buildString)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := config.ParseConfigFromFlagsAndFile(*configFile)
|
cfg := configFrom(*configFile)
|
||||||
|
|
||||||
if os.Getenv("GOMAXPROCS") == "" {
|
if os.Getenv("GOMAXPROCS") == "" {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,13 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/barakmich/glog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config defines the behavior of cayley database instances.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DatabaseType string
|
DatabaseType string
|
||||||
DatabasePath string
|
DatabasePath string
|
||||||
|
|
@ -122,89 +120,23 @@ func (d *duration) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(fmt.Sprintf("%q", *d)), nil
|
return []byte(fmt.Sprintf("%q", *d)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// Load reads a JSON-encoded config contained in the given file. A zero value
|
||||||
databasePath = flag.String("dbpath", "/tmp/testdb", "Path to the database.")
|
// config is returned if the filename is empty.
|
||||||
databaseBackend = flag.String("db", "memstore", "Database Backend.")
|
func Load(file string) (*Config, error) {
|
||||||
replicationBackend = flag.String("replication", "single", "Replication method.")
|
|
||||||
host = flag.String("host", "127.0.0.1", "Host to listen on (defaults to all).")
|
|
||||||
loadSize = flag.Int("load_size", 10000, "Size of triplesets to load")
|
|
||||||
port = flag.String("port", "64210", "Port to listen on.")
|
|
||||||
readOnly = flag.Bool("read_only", false, "Disable writing via HTTP.")
|
|
||||||
timeout = flag.Duration("timeout", 30*time.Second, "Elapsed time until an individual query times out.")
|
|
||||||
)
|
|
||||||
|
|
||||||
func ParseConfigFromFile(filename string) *Config {
|
|
||||||
config := &Config{}
|
config := &Config{}
|
||||||
if filename == "" {
|
if file == "" {
|
||||||
return config
|
return config, nil
|
||||||
}
|
}
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Fatalln("Couldn't open config file", filename)
|
return nil, fmt.Errorf("could not open config file %q: %v", file, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
dec := json.NewDecoder(f)
|
dec := json.NewDecoder(f)
|
||||||
err = dec.Decode(config)
|
err = dec.Decode(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Fatalln("Couldn't read config file:", err)
|
return nil, fmt.Errorf("could not parse config file %q: %v", file, err)
|
||||||
}
|
}
|
||||||
return config
|
return config, nil
|
||||||
}
|
|
||||||
|
|
||||||
func ParseConfigFromFlagsAndFile(fileFlag string) *Config {
|
|
||||||
// Find the file...
|
|
||||||
var trueFilename string
|
|
||||||
if fileFlag != "" {
|
|
||||||
if _, err := os.Stat(fileFlag); os.IsNotExist(err) {
|
|
||||||
glog.Fatalln("Cannot find specified configuration file", fileFlag, ", aborting.")
|
|
||||||
} else {
|
|
||||||
trueFilename = fileFlag
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if _, err := os.Stat(os.Getenv("CAYLEY_CFG")); err == nil {
|
|
||||||
trueFilename = os.Getenv("CAYLEY_CFG")
|
|
||||||
} else {
|
|
||||||
if _, err := os.Stat("/etc/cayley.cfg"); err == nil {
|
|
||||||
trueFilename = "/etc/cayley.cfg"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if trueFilename == "" {
|
|
||||||
glog.Infoln("Couldn't find a config file in either $CAYLEY_CFG or /etc/cayley.cfg. Going by flag defaults only.")
|
|
||||||
}
|
|
||||||
config := ParseConfigFromFile(trueFilename)
|
|
||||||
|
|
||||||
if config.DatabasePath == "" {
|
|
||||||
config.DatabasePath = *databasePath
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.DatabaseType == "" {
|
|
||||||
config.DatabaseType = *databaseBackend
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.ReplicationType == "" {
|
|
||||||
config.ReplicationType = *replicationBackend
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.ListenHost == "" {
|
|
||||||
config.ListenHost = *host
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.ListenPort == "" {
|
|
||||||
config.ListenPort = *port
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Timeout == 0 {
|
|
||||||
config.Timeout = *timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.LoadSize == 0 {
|
|
||||||
config.LoadSize = *loadSize
|
|
||||||
}
|
|
||||||
|
|
||||||
config.ReadOnly = config.ReadOnly || *readOnly
|
|
||||||
|
|
||||||
return config
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue