Make sure ~ is expanded in NewServer; BroadcastReceiver uses temp path

This commit is contained in:
Yuce Tekol 2018-05-02 17:06:58 +03:00
parent 437fcc5438
commit 806437bd23
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573
2 changed files with 24 additions and 1 deletions

View file

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

View file

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