convert all commands to use RunE and global Command var

This commit is contained in:
Matt Jaffee 2017-03-15 22:39:10 -05:00
parent 601e8478da
commit 2aef65989e
8 changed files with 80 additions and 65 deletions

View file

@ -2,7 +2,6 @@ package cmd
import (
"context"
"fmt"
"io"
"os"
@ -11,25 +10,28 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Backuper *ctl.BackupCommand
func NewBackupCmd(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
backuper := ctl.NewBackupCommand(os.Stdin, os.Stdout, os.Stderr)
Backuper = ctl.NewBackupCommand(os.Stdin, os.Stdout, os.Stderr)
backupCmd := &cobra.Command{
Use: "backup",
Short: "Backup data from pilosa.",
Long: `
Backs up the database and frame from across the cluster into a single file.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := backuper.Run(context.Background()); err != nil {
fmt.Println(err)
RunE: func(cmd *cobra.Command, args []string) error {
if err := Backuper.Run(context.Background()); err != nil {
return err
}
return nil
},
}
flags := backupCmd.Flags()
flags.StringVarP(&backuper.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&backuper.Database, "database", "d", "", "Pilosa database to backup into.")
flags.StringVarP(&backuper.Frame, "frame", "f", "", "Frame to backup into.")
flags.StringVarP(&backuper.Path, "output-file", "o", "", "File to write backup to - default stdout")
flags.StringVarP(&Backuper.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&Backuper.Database, "database", "d", "", "Pilosa database to backup into.")
flags.StringVarP(&Backuper.Frame, "frame", "f", "", "Frame to backup into.")
flags.StringVarP(&Backuper.Path, "output-file", "o", "", "File to write backup to - default stdout")
return backupCmd
}

View file

@ -2,7 +2,6 @@ package cmd
import (
"context"
"fmt"
"io"
"os"
@ -11,26 +10,29 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Bencher *ctl.BenchCommand
func NewBenchCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
bencher := ctl.NewBenchCommand(os.Stdin, os.Stdout, os.Stderr)
Bencher = ctl.NewBenchCommand(os.Stdin, os.Stdout, os.Stderr)
benchCmd := &cobra.Command{
Use: "bench",
Short: "Benchmark operations.",
Long: `
Executes a benchmark for a given operation against the database.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := bencher.Run(context.Background()); err != nil {
fmt.Println(err)
RunE: func(cmd *cobra.Command, args []string) error {
if err := Bencher.Run(context.Background()); err != nil {
return err
}
return nil
},
}
flags := benchCmd.Flags()
flags.StringVarP(&bencher.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&bencher.Database, "database", "d", "", "Pilosa database to benchmark.")
flags.StringVarP(&bencher.Frame, "frame", "f", "", "Frame to benchmark.")
flags.StringVarP(&bencher.Op, "operation", "o", "set-bit", "Operation to perform: choose from [set-bit]")
flags.IntVarP(&bencher.N, "num", "n", 0, "Number of operations to perform.")
flags.StringVarP(&Bencher.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&Bencher.Database, "database", "d", "", "Pilosa database to benchmark.")
flags.StringVarP(&Bencher.Frame, "frame", "f", "", "Frame to benchmark.")
flags.StringVarP(&Bencher.Op, "operation", "o", "set-bit", "Operation to perform: choose from [set-bit]")
flags.IntVarP(&Bencher.N, "num", "n", 0, "Number of operations to perform.")
return benchCmd
}

View file

@ -11,23 +11,25 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Checker *ctl.CheckCommand
func NewCheckCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
checker := ctl.NewCheckCommand(os.Stdin, os.Stdout, os.Stderr)
Checker = ctl.NewCheckCommand(os.Stdin, os.Stdout, os.Stderr)
checkCmd := &cobra.Command{
Use: "check <path> [path2]...",
Short: "Do a consistency check on a pilosa data file.",
Long: `
Performs a consistency check on data files.
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
fmt.Println("path required")
return
return fmt.Errorf("path required")
}
checker.Paths = args
if err := checker.Run(context.Background()); err != nil {
fmt.Println(err)
Checker.Paths = args
if err := Checker.Run(context.Background()); err != nil {
return err
}
return nil
},
}
return checkCmd

View file

@ -2,7 +2,6 @@ package cmd
import (
"context"
"fmt"
"io"
"os"
@ -11,17 +10,20 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Conf *ctl.ConfigCommand
func NewConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
conf := ctl.NewConfigCommand(os.Stdin, os.Stdout, os.Stderr)
Conf = ctl.NewConfigCommand(os.Stdin, os.Stdout, os.Stderr)
confCmd := &cobra.Command{
Use: "config",
Short: "Print the default configuration.",
Long: `config prints the default configuration to stdout
`,
Run: func(cmd *cobra.Command, args []string) {
if err := conf.Run(context.Background()); err != nil {
fmt.Println(err)
RunE: func(cmd *cobra.Command, args []string) error {
if err := Conf.Run(context.Background()); err != nil {
return err
}
return nil
},
}

View file

@ -2,7 +2,6 @@ package cmd
import (
"context"
"fmt"
"io"
"os"
@ -11,8 +10,10 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Exporter *ctl.ExportCommand
func NewExportCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
exporter := ctl.NewExportCommand(os.Stdin, os.Stdout, os.Stderr)
Exporter = ctl.NewExportCommand(os.Stdin, os.Stdout, os.Stderr)
exportCmd := &cobra.Command{
Use: "export",
Short: "Export data from pilosa.",
@ -26,18 +27,19 @@ The format of the CSV file is:
The file does not contain any headers.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := exporter.Run(context.Background()); err != nil {
fmt.Println(err)
RunE: func(cmd *cobra.Command, args []string) error {
if err := Exporter.Run(context.Background()); err != nil {
return err
}
return nil
},
}
flags := exportCmd.Flags()
flags.StringVarP(&exporter.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&exporter.Database, "database", "d", "", "Pilosa database to export into.")
flags.StringVarP(&exporter.Frame, "frame", "f", "", "Frame to export into.")
flags.StringVarP(&exporter.Path, "output-file", "o", "", "File to write export to - default stdout")
flags.StringVarP(&Exporter.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&Exporter.Database, "database", "d", "", "Pilosa database to export into.")
flags.StringVarP(&Exporter.Frame, "frame", "f", "", "Frame to export into.")
flags.StringVarP(&Exporter.Path, "output-file", "o", "", "File to write export to - default stdout")
return exportCmd
}

View file

@ -9,8 +9,10 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Importer *ctl.ImportCommand
func NewImportCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
importer := ctl.NewImportCommand(stdin, stdout, stderr)
Importer := ctl.NewImportCommand(stdin, stdout, stderr)
importCmd := &cobra.Command{
Use: "import",
Short: "Bulk load data into pilosa.",
@ -25,18 +27,18 @@ The file should contain no headers. The TIME column is optional and can be
omitted. If it is present then its format should be YYYY-MM-DDTHH:MM.
`,
RunE: func(cmd *cobra.Command, args []string) error {
importer.Paths = args
if err := importer.Run(context.Background()); err != nil {
Importer.Paths = args
if err := Importer.Run(context.Background()); err != nil {
return err
}
return nil
},
}
flags := importCmd.Flags()
flags.StringVarP(&importer.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&importer.Database, "database", "d", "", "Pilosa database to import into.")
flags.StringVarP(&importer.Frame, "frame", "f", "", "Frame to import into.")
flags.IntVarP(&importer.BufferSize, "buffer-size", "s", 10000000, "Number of bits to buffer/sort before importing.")
flags.StringVarP(&Importer.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&Importer.Database, "database", "d", "", "Pilosa database to import into.")
flags.StringVarP(&Importer.Frame, "frame", "f", "", "Frame to import into.")
flags.IntVarP(&Importer.BufferSize, "buffer-size", "s", 10000000, "Number of bits to buffer/sort before importing.")
return importCmd
}

View file

@ -2,7 +2,6 @@ package cmd
import (
"context"
"fmt"
"io"
"os"
@ -11,8 +10,10 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Restorer *ctl.RestoreCommand
func NewRestoreCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
restorer := ctl.NewRestoreCommand(os.Stdin, os.Stdout, os.Stderr)
Restorer = ctl.NewRestoreCommand(os.Stdin, os.Stdout, os.Stderr)
restoreCmd := &cobra.Command{
Use: "restore",
@ -20,17 +21,18 @@ func NewRestoreCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command
Long: `
Restores a frame to the cluster from a backup file.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := restorer.Run(context.Background()); err != nil {
fmt.Println(err)
RunE: func(cmd *cobra.Command, args []string) error {
if err := Restorer.Run(context.Background()); err != nil {
return err
}
return nil
},
}
flags := restoreCmd.Flags()
flags.StringVarP(&restorer.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&restorer.Database, "database", "d", "", "Pilosa database to restore into.")
flags.StringVarP(&restorer.Frame, "frame", "f", "", "Frame to restore into.")
flags.StringVarP(&restorer.Path, "input-file", "i", "", "File to restore from.")
flags.StringVarP(&Restorer.Host, "host", "", "localhost:15000", "host:port of Pilosa.")
flags.StringVarP(&Restorer.Database, "database", "d", "", "Pilosa database to restore into.")
flags.StringVarP(&Restorer.Frame, "frame", "f", "", "Frame to restore into.")
flags.StringVarP(&Restorer.Path, "input-file", "i", "", "File to restore from.")
return restoreCmd
}

View file

@ -11,9 +11,11 @@ import (
"github.com/pilosa/pilosa/ctl"
)
var Sorter *ctl.SortCommand
func NewSortCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
sorter := ctl.NewSortCommand(os.Stdin, os.Stdout, os.Stderr)
Sorter = ctl.NewSortCommand(os.Stdin, os.Stdout, os.Stderr)
sortCmd := &cobra.Command{
Use: "sort <path>",
@ -27,18 +29,17 @@ The format of the CSV file is:
The file should contain no headers.
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
fmt.Println("path required")
return
return fmt.Errorf("path required")
} else if len(args) > 1 {
fmt.Println("only one path supported")
return
return fmt.Errorf("only one path supported")
}
sorter.Path = args[0]
if err := sorter.Run(context.Background()); err != nil {
fmt.Println(err)
Sorter.Path = args[0]
if err := Sorter.Run(context.Background()); err != nil {
return err
}
return nil
},
}
return sortCmd