code review tweaks

This commit is contained in:
Matt Jaffee 2017-03-07 11:30:31 -06:00
parent c7caea6b30
commit 555a514e37
8 changed files with 25 additions and 28 deletions

View file

@ -10,6 +10,6 @@ import (
func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
os.Exit(1)
}
}

View file

@ -14,7 +14,7 @@ import (
"github.com/pilosa/pilosa/server"
)
var serve = server.NewMain()
var serve = server.NewCommand()
var serveCmd = &cobra.Command{
Use: "server",

View file

@ -36,7 +36,7 @@ func NewBackupCommand(stdin io.Reader, stdout, stderr io.Writer) *BackupCommand
}
}
// Run executes the main program execution.
// Run executes the backup.
func (cmd *BackupCommand) Run(ctx context.Context) error {
// Validate arguments.
if cmd.Path == "" {

View file

@ -24,7 +24,7 @@ func NewConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *ConfigCommand
}
}
// Run executes the main program execution.
// Run prints out the default config.
func (cmd *ConfigCommand) Run(ctx context.Context) error {
fmt.Fprintln(cmd.Stdout, strings.TrimSpace(`
data-dir = "~/.pilosa"

View file

@ -36,7 +36,7 @@ func NewExportCommand(stdin io.Reader, stdout, stderr io.Writer) *ExportCommand
}
}
// Run executes the main program execution.
// Run executes the export.
func (cmd *ExportCommand) Run(ctx context.Context) error {
logger := log.New(cmd.Stderr, "", log.LstdFlags)

View file

@ -35,7 +35,7 @@ func NewSortCommand(stdin io.Reader, stdout, stderr io.Writer) *SortCommand {
}
}
// Run executes the main program execution.
// Run executes the sort command.
func (cmd *SortCommand) Run(ctx context.Context) error {
// Open file for reading.
f, err := os.Open(cmd.Path)

View file

@ -36,8 +36,8 @@ const (
DefaultDataDir = "~/.pilosa"
)
// Main represents the main program execution.
type Main struct {
// Command represents the state of the pilosa server command.
type Command struct {
Server *pilosa.Server
// Configuration options.
@ -55,8 +55,8 @@ type Main struct {
}
// NewMain returns a new instance of Main.
func NewMain() *Main {
return &Main{
func NewCommand() *Command {
return &Command{
Server: pilosa.NewServer(),
Config: pilosa.NewConfig(),
@ -66,8 +66,8 @@ func NewMain() *Main {
}
}
// Run executes the main program execution.
func (m *Main) Run(args ...string) error {
// Run executes the pilosa server.
func (m *Command) Run(args ...string) error {
// Notify user of config file.
if m.ConfigPath != "" {
fmt.Fprintf(m.Stdout, "Using config: %s\n", m.ConfigPath)
@ -99,12 +99,12 @@ func (m *Main) Run(args ...string) error {
}
// Close shuts down the server.
func (m *Main) Close() error {
func (m *Command) Close() error {
return m.Server.Close()
}
// ParseFlags parses command line flags from args.
func (m *Main) SetupConfig(args []string) error {
func (m *Command) SetupConfig(args []string) error {
// Load config, if specified.
if m.ConfigPath != "" {
if _, err := toml.DecodeFile(m.ConfigPath, &m.Config); err != nil {
@ -120,11 +120,8 @@ func (m *Main) SetupConfig(args []string) error {
// Expand home directory.
prefix := "~" + string(filepath.Separator)
if strings.HasPrefix(m.Config.DataDir, prefix) {
// u, err := user.Current()
HomeDir := os.Getenv("HOME")
/*if err != nil {
return err
} else*/if HomeDir == "" {
if HomeDir == "" {
return errors.New("data directory not specified and no home dir available")
}
m.Config.DataDir = filepath.Join(HomeDir, strings.TrimPrefix(m.Config.DataDir, prefix))

View file

@ -304,7 +304,7 @@ path = "/path/to/plugins"
// Main represents a test wrapper for main.Main.
type Main struct {
*server.Main
*server.Command
Stdin bytes.Buffer
Stdout bytes.Buffer
@ -318,16 +318,16 @@ func NewMain() *Main {
panic(err)
}
m := &Main{Main: server.NewMain()}
m := &Main{Command: server.NewCommand()}
m.Config.DataDir = path
m.Config.Host = "localhost:0"
m.Main.Stdin = &m.Stdin
m.Main.Stdout = &m.Stdout
m.Main.Stderr = &m.Stderr
m.Command.Stdin = &m.Stdin
m.Command.Stdout = &m.Stdout
m.Command.Stderr = &m.Stderr
if testing.Verbose() {
m.Main.Stdout = io.MultiWriter(os.Stdout, m.Main.Stdout)
m.Main.Stderr = io.MultiWriter(os.Stderr, m.Main.Stderr)
m.Command.Stdout = io.MultiWriter(os.Stdout, m.Command.Stdout)
m.Command.Stderr = io.MultiWriter(os.Stderr, m.Command.Stderr)
}
return m
@ -345,18 +345,18 @@ func MustRunMain() *Main {
// Close closes the program and removes the underlying data directory.
func (m *Main) Close() error {
defer os.RemoveAll(m.Config.DataDir)
return m.Main.Close()
return m.Command.Close()
}
// Reopen closes the program and reopens it.
func (m *Main) Reopen() error {
if err := m.Main.Close(); err != nil {
if err := m.Command.Close(); err != nil {
return err
}
// Create new main with the same config.
config := m.Config
m.Main = server.NewMain()
m.Command = server.NewCommand()
m.Config = config
// Run new program.