diff --git a/broadcast_test.go b/broadcast_test.go index 5654f7c28..970a249cb 100644 --- a/broadcast_test.go +++ b/broadcast_test.go @@ -56,9 +56,14 @@ func testMessageMarshal(t *testing.T, m proto.Message) { // Ensure that BroadcastReceiver can register a BroadcastHandler. func TestBroadcast_BroadcastReceiver(t *testing.T) { + path, err := ioutil.TempDir("", "pilosa-") + if err != nil { + panic(err) + } com := server.NewCommand(bytes.NewBuffer([]byte{}), ioutil.Discard, ioutil.Discard) com.Config.Bind = "localhost:0" - err := com.SetupServer() // this test shouldn't need to import pilosa/server just to set up the Server, but it really shouldn't need to setup the Server at all. The Server should not be the implementation of Broadcast* TODO + com.Config.DataDir = path + err = com.SetupServer() // this test shouldn't need to import pilosa/server just to set up the Server, but it really shouldn't need to setup the Server at all. The Server should not be the implementation of Broadcast* TODO if err != nil { t.Fatalf("setting up server: %v", err) } diff --git a/server.go b/server.go index 06260ae5d..46f8a0dda 100644 --- a/server.go +++ b/server.go @@ -21,6 +21,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "runtime" "strconv" "strings" @@ -230,6 +231,11 @@ func NewServer(opts ...ServerOption) (*Server, error) { } } + err := s.expandDataDirName() + if err != nil { + return nil, err + } + s.Holder.Logger = s.logger s.Holder.Stats.SetLogger(s.logger) @@ -260,6 +266,18 @@ func NewServer(opts ...ServerOption) (*Server, error) { return s, nil } +func (s *Server) expandDataDirName() error { + prefix := "~" + string(filepath.Separator) + if strings.HasPrefix(s.Holder.Path, prefix) { + HomeDir := os.Getenv("HOME") + if HomeDir == "" { + return errors.New("data directory not specified and no home dir available") + } + s.Holder.Path = filepath.Join(HomeDir, strings.TrimPrefix(s.Holder.Path, prefix)) + } + return nil +} + // Open opens and initializes the server. func (s *Server) Open() error { s.logger.Printf("open server")