diff --git a/cmd.go b/cmd.go new file mode 100644 index 000000000..993a42946 --- /dev/null +++ b/cmd.go @@ -0,0 +1,17 @@ +package pilosa + +import "io" + +type CmdIO struct { + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer +} + +func NewCmdIO(stdin io.Reader, stdout, stderr io.Writer) *CmdIO { + return &CmdIO{ + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + } +} diff --git a/cmd/server.go b/cmd/server.go index 55b695cf3..5d5035ba7 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -17,8 +17,7 @@ import ( var Server *server.Command func NewServeCmd(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command { - Server = server.NewCommand() - Server.Stdin, Server.Stdout, Server.Stderr = stdin, stdout, stderr + Server = server.NewCommand(stdin, stdout, stderr) serveCmd := &cobra.Command{ Use: "server", Short: "Run Pilosa.", diff --git a/ctl/backup.go b/ctl/backup.go index f0adf5766..061982aa8 100644 --- a/ctl/backup.go +++ b/ctl/backup.go @@ -22,17 +22,13 @@ type BackupCommand struct { Path string // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewBackupCommand returns a new instance of BackupCommand. func NewBackupCommand(stdin io.Reader, stdout, stderr io.Writer) *BackupCommand { return &BackupCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/bench.go b/ctl/bench.go index 2afe7bb27..2216bace0 100644 --- a/ctl/bench.go +++ b/ctl/bench.go @@ -25,17 +25,13 @@ type BenchCommand struct { N int // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewBenchCommand returns a new instance of BenchCommand. func NewBenchCommand(stdin io.Reader, stdout, stderr io.Writer) *BenchCommand { return &BenchCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/check.go b/ctl/check.go index 0893790f2..40be60499 100644 --- a/ctl/check.go +++ b/ctl/check.go @@ -8,6 +8,7 @@ import ( "path/filepath" "syscall" + "github.com/pilosa/pilosa" "github.com/pilosa/pilosa/roaring" ) @@ -17,17 +18,13 @@ type CheckCommand struct { Paths []string // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewCheckCommand returns a new instance of CheckCommand. func NewCheckCommand(stdin io.Reader, stdout, stderr io.Writer) *CheckCommand { return &CheckCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/config.go b/ctl/config.go index 998c17b47..238972b1b 100644 --- a/ctl/config.go +++ b/ctl/config.go @@ -5,22 +5,19 @@ import ( "fmt" "io" "strings" + + "github.com/pilosa/pilosa" ) // ConfigCommand represents a command for printing a default config. type ConfigCommand struct { - // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewConfigCommand returns a new instance of ConfigCommand. func NewConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *ConfigCommand { return &ConfigCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/export.go b/ctl/export.go index 4eead3239..f4b0efd0a 100644 --- a/ctl/export.go +++ b/ctl/export.go @@ -22,17 +22,13 @@ type ExportCommand struct { Path string // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewExportCommand returns a new instance of ExportCommand. func NewExportCommand(stdin io.Reader, stdout, stderr io.Writer) *ExportCommand { return &ExportCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/import.go b/ctl/import.go index 107ddd84a..ad3281813 100644 --- a/ctl/import.go +++ b/ctl/import.go @@ -33,17 +33,13 @@ type ImportCommand struct { Client *pilosa.Client `json:"-"` // Standard input/output - Stdin io.Reader `json:"-"` - Stdout io.Writer `json:"-"` - Stderr io.Writer `json:"-"` + *pilosa.CmdIO } // NewImportCommand returns a new instance of ImportCommand. func NewImportCommand(stdin io.Reader, stdout, stderr io.Writer) *ImportCommand { return &ImportCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), BufferSize: 10000000, } diff --git a/ctl/inspect.go b/ctl/inspect.go index 86434b131..fa99ba464 100644 --- a/ctl/inspect.go +++ b/ctl/inspect.go @@ -10,6 +10,7 @@ import ( "time" "unsafe" + "github.com/pilosa/pilosa" "github.com/pilosa/pilosa/roaring" ) @@ -19,17 +20,13 @@ type InspectCommand struct { Path string // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewInspectCommand returns a new instance of InspectCommand. func NewInspectCommand(stdin io.Reader, stdout, stderr io.Writer) *InspectCommand { return &InspectCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/restore.go b/ctl/restore.go index a650df495..c764c213c 100644 --- a/ctl/restore.go +++ b/ctl/restore.go @@ -22,17 +22,13 @@ type RestoreCommand struct { Path string // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewRestoreCommand returns a new instance of RestoreCommand. func NewRestoreCommand(stdin io.Reader, stdout, stderr io.Writer) *RestoreCommand { return &RestoreCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/ctl/sort.go b/ctl/sort.go index 66bd16372..9d4b14277 100644 --- a/ctl/sort.go +++ b/ctl/sort.go @@ -21,17 +21,13 @@ type SortCommand struct { Path string // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO } // NewSortCommand returns a new instance of SortCommand. func NewSortCommand(stdin io.Reader, stdout, stderr io.Writer) *SortCommand { return &SortCommand{ - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), } } diff --git a/server/server.go b/server/server.go index 37ee1f7c3..cd9aa3cb0 100644 --- a/server/server.go +++ b/server/server.go @@ -34,9 +34,7 @@ type Command struct { CPUTime time.Duration // Standard input/output - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + *pilosa.CmdIO // running will be closed once Command.Run is finished. Started chan struct{} @@ -45,14 +43,12 @@ type Command struct { } // NewMain returns a new instance of Main. -func NewCommand() *Command { +func NewCommand(stdin io.Reader, stdout, stderr io.Writer) *Command { return &Command{ Server: pilosa.NewServer(), Config: pilosa.NewConfig(), - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), Started: make(chan struct{}), Done: make(chan struct{}), diff --git a/server/server_test.go b/server/server_test.go index 9e907522a..56ff9c109 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -318,7 +318,7 @@ func NewMain() *Main { panic(err) } - m := &Main{Command: server.NewCommand()} + m := &Main{Command: server.NewCommand(os.Stdin, os.Stdout, os.Stderr)} m.Config.DataDir = path m.Config.Host = "localhost:0" m.Command.Stdin = &m.Stdin @@ -356,7 +356,7 @@ func (m *Main) Reopen() error { // Create new main with the same config. config := m.Config - m.Command = server.NewCommand() + m.Command = server.NewCommand(os.Stdin, os.Stdout, os.Stderr) m.Config = config // Run new program.