Simplify command line config arguments

This commit is contained in:
Tobie Morgan Hitchcock 2019-06-16 08:05:59 +01:00
parent 78e4b3d6fa
commit 2ee1133a4b
10 changed files with 47 additions and 231 deletions

View file

@ -37,7 +37,7 @@ var (
var exportCmd = &cobra.Command{ var exportCmd = &cobra.Command{
Use: "export [flags] <file>", Use: "export [flags] <file>",
Short: "Export data from an existing database", Short: "Export an existing database into a SQL script",
Example: " surreal export --auth root:root backup.sql", Example: " surreal export --auth root:root backup.sql",
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: func(cmd *cobra.Command, args []string) (err error) {

View file

@ -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",
},
}

View file

@ -36,7 +36,7 @@ var (
var importCmd = &cobra.Command{ var importCmd = &cobra.Command{
Use: "import [flags] <file>", Use: "import [flags] <file>",
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", Example: " surreal import --auth root:root backup.sql",
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: func(cmd *cobra.Command, args []string) (err error) {

View file

@ -27,7 +27,6 @@ import (
"github.com/abcum/surreal/cnf" "github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/log" "github.com/abcum/surreal/log"
"github.com/abcum/surreal/util/rand" "github.com/abcum/surreal/util/rand"
"github.com/abcum/surreal/util/uuid"
) )
func setup() { 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 // Auth
// -------------------------------------------------- // --------------------------------------------------
@ -135,53 +156,6 @@ func setup() {
opts.Auth.Nets = append(opts.Auth.Nets, subn) 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 // Certs
// -------------------------------------------------- // --------------------------------------------------

View file

@ -15,8 +15,6 @@
package cli package cli
import ( import (
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/abcum/surreal/db" "github.com/abcum/surreal/db"
@ -66,32 +64,25 @@ var startCmd = &cobra.Command{
func init() { 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.DB.Path, "path", "", "Database path used for storing data")
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().IntVar(&opts.Port, "port", 8000, "The port on which to serve the web server")
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().StringVarP(&opts.Bind, "bind", "b", "0.0.0.0", "The hostname or ip address to listen for connections on")
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.Cert.Crt, "cert-crt", "", "Path to the server certificate. 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.Cert.Key, "cert-key", "", "Path to the server private key. Needed when running in secure mode.")
startCmd.PersistentFlags().StringVar(&opts.DB.Cert.CA, "db-ca", "", "Path to the CA file used to connect to the remote database.") startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Sync, "db-sync", 0, "A time duration to use when syncing data to persistent storage")
startCmd.PersistentFlags().StringVar(&opts.DB.Cert.Crt, "db-crt", "", "Path to the certificate file used to connect to the remote database.") startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Shrink, "db-shrink", 0, "A time duration to use when shrinking data on persistent storage")
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.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().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")
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.")
} }

View file

@ -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))
}

View file

@ -41,15 +41,11 @@ type Options struct {
} }
} }
Port struct { Port int // Port as an number
Web int // Web port as an number
Tcp int // Tcp port as an number
}
Conn struct { Conn string // Port as a string
Web string // Web port as a string
Tcp string // Tcp port as a string Bind string // Hostname of the node
}
Cert struct { Cert struct {
Crt string // File location of server crt Crt string // File location of server crt
@ -64,17 +60,6 @@ type Options struct {
Nets []*net.IPNet // Allowed cidr ranges for authentication 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 { Logging struct {
Level string // Stores the configured logging level Level string // Stores the configured logging level
Output string // Stores the configured logging output Output string // Stores the configured logging output

View file

@ -21,7 +21,6 @@ import (
"runtime/debug" "runtime/debug"
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/kvs" "github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/log" "github.com/abcum/surreal/log"
"github.com/abcum/surreal/mem" "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 // can monitor the running time, and ensure
// it runs no longer than specified. // 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, ok := stm.(sql.KillableStatement); ok {
if stm.Duration() > 0 { if stm.Duration() > 0 {
ctx, canc = context.WithTimeout(ctx, stm.Duration()) ctx, canc = context.WithTimeout(ctx, stm.Duration())

View file

@ -14,8 +14,6 @@ import:
version: ^1.4.0 version: ^1.4.0
- package: github.com/hjson/hjson-go - package: github.com/hjson/hjson-go
version: ^3.0.0 version: ^3.0.0
- package: github.com/kr/text
version: ^0.1.0
- package: github.com/mgutz/ansi - package: github.com/mgutz/ansi
- package: github.com/pkg/profile - package: github.com/pkg/profile
version: ^1.3.0 version: ^1.3.0

View file

@ -25,7 +25,7 @@ import (
// Setup sets up the server for remote connections // Setup sets up the server for remote connections
func Setup(opts *cnf.Options) (err error) { 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() s := fibre.Server()
@ -94,16 +94,16 @@ func Setup(opts *cnf.Options) (err error) {
// Log successful start // 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 // Run the server
if len(opts.Cert.Crt) == 0 || len(opts.Cert.Key) == 0 { 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 { 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 return nil