From ee7f13b205091e703c281f703f0770d86667ca65 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 19 Dec 2013 14:18:55 -0800 Subject: [PATCH] Refactor ensurePath to util --- consul/server.go | 12 ++---------- consul/util.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/consul/server.go b/consul/server.go index 4fa8cc069b..65aba16479 100644 --- a/consul/server.go +++ b/consul/server.go @@ -141,14 +141,6 @@ func NewServer(config *Config) (*Server, error) { return s, nil } -// ensurePath is used to make sure a path exists -func (s *Server) ensurePath(path string, dir bool) error { - if !dir { - path = filepath.Dir(path) - } - return os.MkdirAll(path, 0755) -} - // setupSerf is used to setup and initialize a Serf func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string) (*serf.Serf, error) { addr := s.rpcListener.Addr().(*net.TCPAddr) @@ -158,7 +150,7 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string) ( conf.LogOutput = s.config.LogOutput conf.EventCh = ch conf.SnapshotPath = filepath.Join(s.config.DataDir, path) - if err := s.ensurePath(conf.SnapshotPath, false); err != nil { + if err := ensurePath(conf.SnapshotPath, false); err != nil { return nil, err } return serf.Create(conf) @@ -168,7 +160,7 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string) ( func (s *Server) setupRaft() error { // Create the base path path := filepath.Join(s.config.DataDir, raftState) - if err := s.ensurePath(path, true); err != nil { + if err := ensurePath(path, true); err != nil { return err } diff --git a/consul/util.go b/consul/util.go index 6bc1940618..ff4d145e60 100644 --- a/consul/util.go +++ b/consul/util.go @@ -1,5 +1,10 @@ package consul +import ( + "os" + "path/filepath" +) + // strContains checks if a list contains a string func strContains(l []string, s string) bool { for _, v := range l { @@ -9,3 +14,11 @@ func strContains(l []string, s string) bool { } return false } + +// ensurePath is used to make sure a path exists +func ensurePath(path string, dir bool) error { + if !dir { + path = filepath.Dir(path) + } + return os.MkdirAll(path, 0755) +}