From 2ee1133a4b903a7165fa9f4297c7b63c61388ae0 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 16 Jun 2019 08:05:59 +0100 Subject: [PATCH] Simplify command line config arguments --- cli/export.go | 2 +- cli/flags.go | 51 ------------------------------------ cli/import.go | 2 +- cli/setup.go | 70 ++++++++++++++++---------------------------------- cli/start.go | 39 +++++++++++----------------- cli/util.go | 64 --------------------------------------------- cnf/cnf.go | 23 +++-------------- db/executor.go | 17 ------------ glide.yaml | 2 -- web/web.go | 8 +++--- 10 files changed, 47 insertions(+), 231 deletions(-) delete mode 100644 cli/flags.go delete mode 100644 cli/util.go diff --git a/cli/export.go b/cli/export.go index 64a02588..16936594 100644 --- a/cli/export.go +++ b/cli/export.go @@ -37,7 +37,7 @@ var ( var exportCmd = &cobra.Command{ Use: "export [flags] ", - Short: "Export data from an existing database", + Short: "Export an existing database into a SQL script", Example: " surreal export --auth root:root backup.sql", RunE: func(cmd *cobra.Command, args []string) (err error) { diff --git a/cli/flags.go b/cli/flags.go deleted file mode 100644 index a216b8e1..00000000 --- a/cli/flags.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright © 2016 Abcum Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cli - -var flags = map[string]string{ - "db": `Database configuration path used for storing data. Available backend stores are memory, file, logr, s3, gcs, or dendrodb. (default "memory").`, - "key": `Encryption key to use for intra-cluster communications, and on-disk encryption. For AES-128 encryption use a 16 bit key, for AES-192 encryption use a 24 bit key, and for AES-256 encryption use a 32 bit key.`, - "size": `A size in MB which determines the minimum or maximum file size for streaming data file storage. This is used for specifying maximum cached data sizes when using remote streaming storage. (default "5")`, - "sync": `A time duration to use when syncing data to persistent storage. To sync data with every write specify '0', otherwise the data will be persisted asynchronously after the specified duration. (default "0s")`, - "shrink": `A time duration to use when shrinking data on persistent storage. To shrink data asynchronously after a repeating period of time, specify a duration. Disabled by default. (default "0s")`, -} - -var usage = map[string][]string{ - "db": { - "--db-path memory", - "--db-path file://surreal.db", - "--db-path logr://path/to/surreal.db", - "--db-path s3://bucket/path/to/surreal.db", - "--db-path gcs://bucket/path/to/surreal.db", - "--db-path dendro://user:pass@192.168.1.100", - }, - "key": { - "--key 1hg7dbrma8ghe547", - "--key 1hg7dbrma8ghe5473kghvie6", - "--key 1hg7dbrma8ghe5473kghvie64jgi3ph4", - }, - "size": { - "--db-size 30", - }, - "sync": { - "--db-sync 0", - "--db-sync 5s", - "--db-sync 1m", - }, - "shrink": { - "--db-shrink 30m", - "--db-shrink 24h", - }, -} diff --git a/cli/import.go b/cli/import.go index a2b701d6..473d44c4 100644 --- a/cli/import.go +++ b/cli/import.go @@ -36,7 +36,7 @@ var ( var importCmd = &cobra.Command{ Use: "import [flags] ", - Short: "Execute a SQL script against an existing database", + Short: "Import a SQL script into an existing database", Example: " surreal import --auth root:root backup.sql", RunE: func(cmd *cobra.Command, args []string) (err error) { diff --git a/cli/setup.go b/cli/setup.go index 8f32f269..234b9c7c 100644 --- a/cli/setup.go +++ b/cli/setup.go @@ -27,7 +27,6 @@ import ( "github.com/abcum/surreal/cnf" "github.com/abcum/surreal/log" "github.com/abcum/surreal/util/rand" - "github.com/abcum/surreal/util/uuid" ) func setup() { @@ -89,6 +88,28 @@ func setup() { } + // -------------------------------------------------- + // Ports + // -------------------------------------------------- + + // Specify default port + if opts.Port == 0 { + opts.Port = 8000 + } + + // Specift default host + if opts.Bind == "" { + opts.Bind = "0.0.0.0" + } + + // Ensure port number is valid + if opts.Port < 0 || opts.Port > 65535 { + log.Fatalf("Invalid port %d. Please specify a valid port number for --port-web", opts.Port) + } + + // Store the ports in host:port string format + opts.Conn = fmt.Sprintf("%s:%d", opts.Bind, opts.Port) + // -------------------------------------------------- // Auth // -------------------------------------------------- @@ -135,53 +156,6 @@ func setup() { opts.Auth.Nets = append(opts.Auth.Nets, subn) } - // -------------------------------------------------- - // Nodes - // -------------------------------------------------- - - // Ensure that the default - // node details are defined - - if opts.Node.Host == "" { - opts.Node.Host, _ = os.Hostname() - } - - if opts.Node.Name == "" { - opts.Node.Name = opts.Node.Host - } - - if opts.Node.UUID == "" { - opts.Node.UUID = opts.Node.Name + "-" + uuid.New().String() - } - - // -------------------------------------------------- - // Ports - // -------------------------------------------------- - - // Specify default port - if opts.Port.Web == 0 { - opts.Port.Web = 8000 - } - - // Specify default port - if opts.Port.Tcp == 0 { - opts.Port.Tcp = 33693 - } - - // Ensure port number is valid - if opts.Port.Web < 0 || opts.Port.Web > 65535 { - log.Fatalf("Invalid port %d. Please specify a valid port number for --port-web", opts.Port.Web) - } - - // Ensure port number is valid - if opts.Port.Tcp < 0 || opts.Port.Tcp > 65535 { - log.Fatalf("Invalid port %d. Please specify a valid port number for --port-tcp", opts.Port.Tcp) - } - - // Store the ports in host:port string format - opts.Conn.Web = fmt.Sprintf("%s:%d", opts.Node.Host, opts.Port.Web) - opts.Conn.Tcp = fmt.Sprintf("%s:%d", opts.Node.Host, opts.Port.Tcp) - // -------------------------------------------------- // Certs // -------------------------------------------------- diff --git a/cli/start.go b/cli/start.go index 87bdb384..4b450076 100644 --- a/cli/start.go +++ b/cli/start.go @@ -15,8 +15,6 @@ package cli import ( - "os" - "github.com/spf13/cobra" "github.com/abcum/surreal/db" @@ -66,32 +64,25 @@ var startCmd = &cobra.Command{ func init() { - host, _ := os.Hostname() + startCmd.PersistentFlags().StringVarP(&opts.Auth.Auth, "auth", "a", "root:root", "Master database authentication details") + startCmd.PersistentFlags().StringVar(&opts.Auth.User, "auth-user", "", "The master username for the database. Use this as an alternative to the --auth flag") + startCmd.PersistentFlags().StringVar(&opts.Auth.Pass, "auth-pass", "", "The master password for the database. Use this as an alternative to the --auth flag") + startCmd.PersistentFlags().StringSliceVar(&opts.Auth.Addr, "auth-addr", []string{"0.0.0.0/0", "0:0:0:0:0:0:0:0/0"}, "The IP address ranges from which master authentication is possible") - startCmd.PersistentFlags().StringVarP(&opts.Auth.Auth, "auth", "a", "root:root", "Master database authentication details.") - startCmd.PersistentFlags().StringVar(&opts.Auth.User, "auth-user", "", "The master username for the database. Use this as an alternative to the --auth flag.") - startCmd.PersistentFlags().StringVar(&opts.Auth.Pass, "auth-pass", "", "The master password for the database. Use this as an alternative to the --auth flag.") - startCmd.PersistentFlags().StringSliceVar(&opts.Auth.Addr, "auth-addr", []string{"0.0.0.0/0", "0:0:0:0:0:0:0:0/0"}, "The IP address ranges from which master authentication is possible.") + startCmd.PersistentFlags().StringVar(&opts.DB.Path, "path", "", "Database path used for storing data") + startCmd.PersistentFlags().IntVar(&opts.Port, "port", 8000, "The port on which to serve the web server") + startCmd.PersistentFlags().StringVarP(&opts.Bind, "bind", "b", "0.0.0.0", "The hostname or ip address to listen for connections on") - startCmd.PersistentFlags().StringVar(&opts.Cert.Crt, "cert-crt", "", "Path to the server certificate. Needed when running in secure mode.") - startCmd.PersistentFlags().StringVar(&opts.Cert.Key, "cert-key", "", "Path to the server private key. Needed when running in secure mode.") + startCmd.PersistentFlags().StringVarP(&opts.DB.Code, "key", "k", "", "Encryption key to use for on-disk encryption") - startCmd.PersistentFlags().StringVar(&opts.DB.Cert.CA, "db-ca", "", "Path to the CA file used to connect to the remote database.") - startCmd.PersistentFlags().StringVar(&opts.DB.Cert.Crt, "db-crt", "", "Path to the certificate file used to connect to the remote database.") - startCmd.PersistentFlags().StringVar(&opts.DB.Cert.Key, "db-key", "", "Path to the private key file used to connect to the remote database.") - startCmd.PersistentFlags().StringVar(&opts.DB.Path, "db-path", "", flag("db")) - startCmd.PersistentFlags().IntVar(&opts.DB.Proc.Size, "db-size", 0, flag("size")) - startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Sync, "db-sync", 0, flag("sync")) - startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Shrink, "db-shrink", 0, flag("shrink")) + startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Sync, "db-sync", 0, "A time duration to use when syncing data to persistent storage") + startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Shrink, "db-shrink", 0, "A time duration to use when shrinking data on persistent storage") - startCmd.PersistentFlags().DurationVar(&opts.Query.Timeout, "query-timeout", 0, "") + startCmd.PersistentFlags().StringVar(&opts.DB.Cert.CA, "kvs-ca", "", "Path to the CA file used to connect to the remote database") + startCmd.PersistentFlags().StringVar(&opts.DB.Cert.Crt, "kvs-crt", "", "Path to the certificate file used to connect to the remote database") + startCmd.PersistentFlags().StringVar(&opts.DB.Cert.Key, "kvs-key", "", "Path to the private key file used to connect to the remote database") - startCmd.PersistentFlags().StringVarP(&opts.DB.Code, "key", "k", "", flag("key")) - - startCmd.PersistentFlags().StringVarP(&opts.Node.Host, "bind", "b", "0.0.0.0", "The hostname or ip address to listen for connections on.") - startCmd.PersistentFlags().StringVarP(&opts.Node.Name, "name", "n", host, "The name of this node, used for logs and statistics.") - - startCmd.PersistentFlags().IntVar(&opts.Port.Tcp, "port-tcp", 33693, "The port on which to serve the tcp server.") - startCmd.PersistentFlags().IntVar(&opts.Port.Web, "port-web", 8000, "The port on which to serve the web server.") + startCmd.PersistentFlags().StringVar(&opts.Cert.Crt, "web-crt", "", "Path to the server certificate. Needed when running in secure mode") + startCmd.PersistentFlags().StringVar(&opts.Cert.Key, "web-key", "", "Path to the server private key. Needed when running in secure mode") } diff --git a/cli/util.go b/cli/util.go deleted file mode 100644 index 65d788a6..00000000 --- a/cli/util.go +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright © 2016 Abcum Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cli - -import ( - "strings" - - "github.com/kr/text" -) - -func flag(n string) (s string) { - - if f, ok := flags[n]; ok { - - s += "\n\n" - - if u, ok := usage[n]; !ok { - - s += indent(8, wrap(f)) - - s += "\n" - - } else { - - s += indent(8, wrap(f+" For example:")) - - s += "\n" - - for _, i := range u { - s += "\n" + strings.Repeat(" ", 12) + i - } - - s += "\n" - - } - - // Indent default values - s += strings.Repeat(" ", 7) - - } - - return - -} - -func wrap(s string) string { - return text.Wrap(s, 71) -} - -func indent(i int, s string) string { - return text.Indent(s, strings.Repeat(" ", i)) -} diff --git a/cnf/cnf.go b/cnf/cnf.go index 70982eb4..00d178f4 100644 --- a/cnf/cnf.go +++ b/cnf/cnf.go @@ -41,15 +41,11 @@ type Options struct { } } - Port struct { - Web int // Web port as an number - Tcp int // Tcp port as an number - } + Port int // Port as an number - Conn struct { - Web string // Web port as a string - Tcp string // Tcp port as a string - } + Conn string // Port as a string + + Bind string // Hostname of the node Cert struct { Crt string // File location of server crt @@ -64,17 +60,6 @@ type Options struct { Nets []*net.IPNet // Allowed cidr ranges for authentication } - Node struct { - Host string // Node hostname - Name string // Name of this node - UUID string // UUID of this node - Join []string // Slice of cluster peers to join - } - - Query struct { - Timeout time.Duration // Fixed query timeout - } - Logging struct { Level string // Stores the configured logging level Output string // Stores the configured logging output diff --git a/db/executor.go b/db/executor.go index 00321efb..5cf23845 100644 --- a/db/executor.go +++ b/db/executor.go @@ -21,7 +21,6 @@ import ( "runtime/debug" - "github.com/abcum/surreal/cnf" "github.com/abcum/surreal/kvs" "github.com/abcum/surreal/log" "github.com/abcum/surreal/mem" @@ -269,22 +268,6 @@ func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interf // can monitor the running time, and ensure // it runs no longer than specified. - if cnf.Settings.Query.Timeout > 0 { - if perm(ctx) != cnf.AuthKV { - ctx, canc = context.WithTimeout(ctx, cnf.Settings.Query.Timeout) - defer func() { - if tim := ctx.Err(); err == nil && tim != nil { - res, err = nil, &TimerError{timer: cnf.Settings.Query.Timeout} - } - canc() - }() - } - } - - // Mark the beginning of this statement so we - // can monitor the running time, and ensure - // it runs no longer than specified. - if stm, ok := stm.(sql.KillableStatement); ok { if stm.Duration() > 0 { ctx, canc = context.WithTimeout(ctx, stm.Duration()) diff --git a/glide.yaml b/glide.yaml index d81863f9..ff894fdf 100644 --- a/glide.yaml +++ b/glide.yaml @@ -14,8 +14,6 @@ import: version: ^1.4.0 - package: github.com/hjson/hjson-go version: ^3.0.0 -- package: github.com/kr/text - version: ^0.1.0 - package: github.com/mgutz/ansi - package: github.com/pkg/profile version: ^1.3.0 diff --git a/web/web.go b/web/web.go index 823c1be7..699298a2 100644 --- a/web/web.go +++ b/web/web.go @@ -25,7 +25,7 @@ import ( // Setup sets up the server for remote connections func Setup(opts *cnf.Options) (err error) { - log.WithPrefix("web").Infof("Starting web server on %s", opts.Conn.Web) + log.WithPrefix("web").Infof("Starting web server on %s", opts.Conn) s := fibre.Server() @@ -94,16 +94,16 @@ func Setup(opts *cnf.Options) (err error) { // Log successful start - log.WithPrefix("web").Infof("Started web server on %s", opts.Conn.Web) + log.WithPrefix("web").Infof("Started web server on %s", opts.Conn) // Run the server if len(opts.Cert.Crt) == 0 || len(opts.Cert.Key) == 0 { - s.Run(opts.Conn.Web) + s.Run(opts.Conn) } if len(opts.Cert.Crt) != 0 && len(opts.Cert.Key) != 0 { - s.Run(opts.Conn.Web, opts.Cert.Crt, opts.Cert.Key) + s.Run(opts.Conn, opts.Cert.Crt, opts.Cert.Key) } return nil