Made expandDirName generic.

This commit is contained in:
Yuce Tekol 2018-05-03 00:07:56 +03:00
parent 244c4e894e
commit beaa69a7bd
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573

View file

@ -231,14 +231,21 @@ func NewServer(opts ...ServerOption) (*Server, error) {
}
}
err := s.expandDataDirName()
path, err := expandDirName(s.Holder.Path)
if err != nil {
return nil, err
}
s.Holder.Path = path
s.Holder.Logger = s.logger
s.Holder.Stats.SetLogger(s.logger)
path, err = expandDirName(s.Cluster.Path)
if err != nil {
return nil, err
}
s.Cluster.Path = path
s.Cluster.Logger = s.logger
s.Cluster.Holder = s.Holder
@ -266,18 +273,6 @@ 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")
@ -796,3 +791,15 @@ type StatusHandler interface {
ClusterStatus() (proto.Message, error)
HandleRemoteStatus(proto.Message) error
}
func expandDirName(path string) (string, error) {
prefix := "~" + string(filepath.Separator)
if strings.HasPrefix(path, prefix) {
HomeDir := os.Getenv("HOME")
if HomeDir == "" {
return "", errors.New("data directory not specified and no home dir available")
}
return filepath.Join(HomeDir, strings.TrimPrefix(path, prefix)), nil
}
return path, nil
}