From 943e8ece1e32d2b0ea6f981aab7d12f23f1c2fd1 Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Mon, 13 Mar 2017 11:30:33 -0500 Subject: [PATCH] add data-dir flag and simplify server/server.go Now that the default data dir is defined by the flag, we don't need to set it explicitly if it isn't set. We also stop reading the config file explicitly in server.go since viper will read it - we do need to define all the config options though before the config file will work properly. --- cmd/server.go | 12 ++++-------- server/server.go | 40 +++++++--------------------------------- 2 files changed, 11 insertions(+), 41 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index c9d5659d5..2d686b2ea 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -26,11 +26,6 @@ on the configured port.`, serve.Server.Handler.Version = Version fmt.Fprintf(serve.Stderr, "Pilosa %s, build time %s\n", Version, BuildTime) - // Parse command line arguments. - if err := serve.SetupConfig(args); err != nil { - return err - } - // Start CPU profiling. if serve.CPUProfile != "" { f, err := os.Create(serve.CPUProfile) @@ -72,9 +67,10 @@ on the configured port.`, func init() { flags := serveCmd.Flags() - flags.StringVarP(&serve.ConfigPath, "config", "c", "", "Configuration file to read from") - flags.StringVarP(&serve.CPUProfile, "cpu-profile", "", "", "Where to store CPU profile") - flags.DurationVarP(&serve.CPUTime, "cpu-time", "", 30*time.Second, "CPU profile duration") + flags.StringVarP(&serve.ConfigPath, "config", "c", "", "Configuration file to read from.") + flags.StringVarP(&serve.Config.DataDir, "data-dir", "d", "~/.pilosa", "Directory to store pilosa data files.") + flags.StringVarP(&serve.CPUProfile, "cpu-profile", "", "", "Where to store CPU profile.") + flags.DurationVarP(&serve.CPUTime, "cpu-time", "", 30*time.Second, "CPU profile duration.") RootCmd.AddCommand(serveCmd) } diff --git a/server/server.go b/server/server.go index 85b3dbb77..15f2ad9b4 100644 --- a/server/server.go +++ b/server/server.go @@ -10,7 +10,6 @@ import ( "strings" "time" - "github.com/BurntSushi/toml" "github.com/pilosa/pilosa" ) @@ -55,9 +54,13 @@ func NewCommand() *Command { // 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) + prefix := "~" + string(filepath.Separator) + if strings.HasPrefix(m.Config.DataDir, prefix) { + HomeDir := os.Getenv("HOME") + 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)) } // Setup logging output. @@ -79,9 +82,7 @@ func (m *Command) Run(args ...string) error { if err := m.Server.Open(); err != nil { return err } - fmt.Fprintf(m.Stderr, "Listening as http://%s\n", m.Server.Host) - return nil } @@ -89,30 +90,3 @@ func (m *Command) Run(args ...string) error { func (m *Command) Close() error { return m.Server.Close() } - -// SetupConfig loads the config file if specified and sets state on the Command. -func (m *Command) SetupConfig(args []string) error { - // Load config, if specified. - if m.ConfigPath != "" { - if _, err := toml.DecodeFile(m.ConfigPath, &m.Config); err != nil { - return err - } - } - - // Use default data directory if one is not specified. - if m.Config.DataDir == "" { - m.Config.DataDir = DefaultDataDir - } - - // Expand home directory. - prefix := "~" + string(filepath.Separator) - if strings.HasPrefix(m.Config.DataDir, prefix) { - HomeDir := os.Getenv("HOME") - 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)) - } - - return nil -}