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.
This commit is contained in:
Matt Jaffee 2017-03-13 11:30:33 -05:00
parent 26a900843a
commit 943e8ece1e
2 changed files with 11 additions and 41 deletions

View file

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

View file

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