Add initial cli commands
This commit is contained in:
parent
c09f1e1dfe
commit
79b756dde0
9 changed files with 350 additions and 0 deletions
71
cli/cli.go
Normal file
71
cli/cli.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
// 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 (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/abcum/surreal/server"
|
||||
"github.com/abcum/surreal/stores"
|
||||
|
||||
// Load all backend stores
|
||||
_ "github.com/abcum/surreal/stores/boltdb"
|
||||
_ "github.com/abcum/surreal/stores/cockroachdb"
|
||||
_ "github.com/abcum/surreal/stores/leveldb"
|
||||
_ "github.com/abcum/surreal/stores/memory"
|
||||
_ "github.com/abcum/surreal/stores/mongodb"
|
||||
_ "github.com/abcum/surreal/stores/rethinkdb"
|
||||
)
|
||||
|
||||
var mainCmd = &cobra.Command{
|
||||
Use: "surreal",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return stores.Setup(Config.Context)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return server.Setup(Config.Context)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
mainCmd.AddCommand(
|
||||
kvCmd,
|
||||
sqlCmd,
|
||||
importCmd,
|
||||
exportCmd,
|
||||
versionCmd,
|
||||
)
|
||||
|
||||
mainCmd.PersistentFlags().StringVarP(&Config.Db, "db", "d", "memory", "Set backend datastore")
|
||||
mainCmd.PersistentFlags().StringVarP(&Config.DbPath, "dbpath", "", "", "Set path to boltdb/leveldb datastore file")
|
||||
mainCmd.PersistentFlags().StringVarP(&Config.DbName, "dbname", "", "", "Set name of mongodb/rethinkdb database table")
|
||||
mainCmd.PersistentFlags().StringVarP(&Config.Http, "http", "", ":33693", "Host to listen for http connections on")
|
||||
mainCmd.PersistentFlags().StringVarP(&Config.Sock, "sock", "", ":33793", "Port to listen for sock connections on")
|
||||
mainCmd.PersistentFlags().StringVarP(&Config.Base, "base", "b", "surreal", "Name of the root database key")
|
||||
mainCmd.PersistentFlags().BoolVarP(&Config.Verbose, "verbose", "v", false, "Enable verbose output")
|
||||
|
||||
}
|
||||
|
||||
// Run runs the cli app
|
||||
func Run() {
|
||||
if err := mainCmd.Execute(); err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
23
cli/config.go
Normal file
23
cli/config.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
// 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 (
|
||||
"github.com/abcum/surreal/cnf"
|
||||
)
|
||||
|
||||
var Config struct {
|
||||
cnf.Context
|
||||
}
|
27
cli/export.go
Normal file
27
cli/export.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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 (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var exportCmd = &cobra.Command{
|
||||
Use: "export",
|
||||
Short: "Export data from an existing database into a JSON file",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
27
cli/import.go
Normal file
27
cli/import.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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 (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var importCmd = &cobra.Command{
|
||||
Use: "import",
|
||||
Short: "Import data into an existing database from a JSON file",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
85
cli/kv.go
Normal file
85
cli/kv.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
// 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 (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var kvCmd = &cobra.Command{
|
||||
Use: "kv",
|
||||
Short: "Open a kv connection to the database",
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
kvCmd.AddCommand(
|
||||
getCmd,
|
||||
putCmd,
|
||||
delCmd,
|
||||
delrCmd,
|
||||
scanCmd,
|
||||
rscanCmd,
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
var getCmd = &cobra.Command{
|
||||
Use: "get [options] <key>",
|
||||
Short: "Gets the value for a key",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
||||
|
||||
var putCmd = &cobra.Command{
|
||||
Use: "put [options] <key>",
|
||||
Short: "Puts the value for a key",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
||||
|
||||
var delCmd = &cobra.Command{
|
||||
Use: "del [options] <key>",
|
||||
Short: "Deletes a key",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
||||
|
||||
var delrCmd = &cobra.Command{
|
||||
Use: "delr [options] [<start-key> [<end-key>]]",
|
||||
Short: "Deletes a range of keys",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
||||
|
||||
var scanCmd = &cobra.Command{
|
||||
Use: "scan [options] [<start-key> [<end-key>]]",
|
||||
Short: "Scans a range of keys",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
||||
|
||||
var rscanCmd = &cobra.Command{
|
||||
Use: "rscan [options] [<start-key> [<end-key>]]",
|
||||
Short: "Scans a range of keys in reverse",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
27
cli/sql.go
Normal file
27
cli/sql.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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 (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var sqlCmd = &cobra.Command{
|
||||
Use: "sql",
|
||||
Short: "Open an sql connection to the database",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
42
cli/version.go
Normal file
42
cli/version.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
// 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 (
|
||||
"fmt"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/abcum/surreal/util/vers"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Output version information",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
info := vers.GetInfo()
|
||||
|
||||
tw := tabwriter.NewWriter(os.Stdout, 2, 1, 2, ' ', 0)
|
||||
fmt.Fprintf(tw, "Build Go: %s\n", info.Go)
|
||||
fmt.Fprintf(tw, "Build Ver: %s\n", info.Ver)
|
||||
fmt.Fprintf(tw, "Build Rev: %s\n", info.Rev)
|
||||
fmt.Fprintf(tw, "Build Time: %s\n", info.Time)
|
||||
|
||||
tw.Flush()
|
||||
|
||||
},
|
||||
}
|
27
cnf/cnf.go
Normal file
27
cnf/cnf.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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 cnf
|
||||
|
||||
type Context struct {
|
||||
Db string
|
||||
DbPath string
|
||||
DbName string
|
||||
|
||||
Http string
|
||||
Sock string
|
||||
Base string
|
||||
|
||||
Verbose bool
|
||||
}
|
21
main.go
Normal file
21
main.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// 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 main
|
||||
|
||||
import "github.com/abcum/surreal/cli"
|
||||
|
||||
func main() {
|
||||
cli.Run()
|
||||
}
|
Loading…
Reference in a new issue