From 555a514e37526ec6624a4b1e632f927f0c96ecb7 Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Tue, 7 Mar 2017 11:30:31 -0600 Subject: [PATCH] code review tweaks --- cmd/pilosa/main.go | 2 +- cmd/server.go | 2 +- ctl/backup.go | 2 +- ctl/config.go | 2 +- ctl/export.go | 2 +- ctl/sort.go | 2 +- server/server.go | 21 +++++++++------------ server/server_test.go | 20 ++++++++++---------- 8 files changed, 25 insertions(+), 28 deletions(-) diff --git a/cmd/pilosa/main.go b/cmd/pilosa/main.go index 255aef606..c3bef0622 100644 --- a/cmd/pilosa/main.go +++ b/cmd/pilosa/main.go @@ -10,6 +10,6 @@ import ( func main() { if err := cmd.RootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(-1) + os.Exit(1) } } diff --git a/cmd/server.go b/cmd/server.go index 6603a0989..64e45eed4 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -14,7 +14,7 @@ import ( "github.com/pilosa/pilosa/server" ) -var serve = server.NewMain() +var serve = server.NewCommand() var serveCmd = &cobra.Command{ Use: "server", diff --git a/ctl/backup.go b/ctl/backup.go index 23b34cd1f..f0adf5766 100644 --- a/ctl/backup.go +++ b/ctl/backup.go @@ -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 == "" { diff --git a/ctl/config.go b/ctl/config.go index 516069e2d..998c17b47 100644 --- a/ctl/config.go +++ b/ctl/config.go @@ -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" diff --git a/ctl/export.go b/ctl/export.go index f19c7123f..4eead3239 100644 --- a/ctl/export.go +++ b/ctl/export.go @@ -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) diff --git a/ctl/sort.go b/ctl/sort.go index eac68b7ab..66bd16372 100644 --- a/ctl/sort.go +++ b/ctl/sort.go @@ -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) diff --git a/server/server.go b/server/server.go index 247d0efa9..b7b8ca2f9 100644 --- a/server/server.go +++ b/server/server.go @@ -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)) diff --git a/server/server_test.go b/server/server_test.go index bb772ab45..9e907522a 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -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.