From 2aef65989edc483d5eee0c166e06a307e7075d4c Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Wed, 15 Mar 2017 22:39:10 -0500 Subject: [PATCH] convert all commands to use RunE and global Command var --- cmd/backup.go | 20 +++++++++++--------- cmd/bench.go | 22 ++++++++++++---------- cmd/check.go | 16 +++++++++------- cmd/config.go | 12 +++++++----- cmd/export.go | 20 +++++++++++--------- cmd/import.go | 16 +++++++++------- cmd/restore.go | 20 +++++++++++--------- cmd/sort.go | 19 ++++++++++--------- 8 files changed, 80 insertions(+), 65 deletions(-) diff --git a/cmd/backup.go b/cmd/backup.go index adc1cfcd8..f9b86b05d 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -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 } diff --git a/cmd/bench.go b/cmd/bench.go index a1d89637d..e6e723ac8 100644 --- a/cmd/bench.go +++ b/cmd/bench.go @@ -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 } diff --git a/cmd/check.go b/cmd/check.go index ae8a24c24..914d499fd 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -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 [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 diff --git a/cmd/config.go b/cmd/config.go index 88c8f2710..8191d5972 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 }, } diff --git a/cmd/export.go b/cmd/export.go index e833fe1ee..bc0348808 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -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 } diff --git a/cmd/import.go b/cmd/import.go index 99731ca16..4cad3ff28 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -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 } diff --git a/cmd/restore.go b/cmd/restore.go index afe5aade7..b990e482e 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -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 } diff --git a/cmd/sort.go b/cmd/sort.go index aafacf59e..0ea7d9007 100644 --- a/cmd/sort.go +++ b/cmd/sort.go @@ -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 ", @@ -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