add embedded struct to contain io for commands

per @tgruben's suggestion
This commit is contained in:
Matt Jaffee 2017-03-17 14:24:39 -05:00
parent 5f267c0a3a
commit 176861d702
13 changed files with 45 additions and 66 deletions

17
cmd.go Normal file
View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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.