This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git

commit 603f989acfee426e024023826cc654cd41e7d569
Merge: 374a01a cc7fb2e
Author: Xin.Zh <[email protected]>
AuthorDate: Sun Jan 10 17:07:31 2021 +0800

    Merge pull request #932 from cityiron/feature/fix-config_center-file
    
    [Fix] file service discovery path can run in windows

 config_center/file/impl.go         | 110 +++++++++++++++++++++++++++----------
 registry/file/service_discovery.go |   2 +-
 2 files changed, 81 insertions(+), 31 deletions(-)

diff --cc config_center/file/impl.go
index 9afe7c6,0233f63..6489a07
--- a/config_center/file/impl.go
+++ b/config_center/file/impl.go
@@@ -41,13 -40,29 +40,21 @@@ import 
        "github.com/apache/dubbo-go/config_center/parser"
  )
  
 -var osType string
 -var path string
++var (
++      osType = runtime.GOOS
++)
+ 
+ const (
 -      windows = "windows"
++      windowsOS = "windows"
+ )
+ 
  const (
-       PARAM_NAME_PREFIX                 = "dubbo.config-center."
-       CONFIG_CENTER_DIR_PARAM_NAME      = PARAM_NAME_PREFIX + "dir"
-       CONFIG_CENTER_ENCODING_PARAM_NAME = PARAM_NAME_PREFIX + "encoding"
-       DEFAULT_CONFIG_CENTER_ENCODING    = "UTF-8"
+       ParamNamePrefix               = "dubbo.config-center."
+       ConfigCenterDirParamName      = ParamNamePrefix + "dir"
+       ConfigCenterEncodingParamName = ParamNamePrefix + "encoding"
+       defaultConfigCenterEncoding   = "UTF-8"
  )
  
 -func init() {
 -      osType = runtime.GOOS
 -      if os.IsPathSeparator('\\') {
 -              path = "\\"
 -      } else {
 -              path = "/"
 -      }
 -}
 -
  // FileSystemDynamicConfiguration
  type FileSystemDynamicConfiguration struct {
        config_center.BaseDynamicConfiguration
@@@ -206,7 -211,7 +203,7 @@@ func (fsdc *FileSystemDynamicConfigurat
  }
  
  func (fsdc *FileSystemDynamicConfiguration) deleteDelay(path string) (bool, 
error) {
--      if path == "" {
++      if len(path) == 0 {
                return false, nil
        }
  
@@@ -226,9 -231,9 +223,7 @@@ func (fsdc *FileSystemDynamicConfigurat
  }
  
  func forceMkdirParent(fp string) error {
--      pd := getParentDirectory(fp)
--
--      return createDir(pd)
++      return createDir(getParentDirectory(fp))
  }
  
  func createDir(path string) error {
@@@ -250,6 -255,6 +245,7 @@@ func substr(s string, pos, length int) 
        if l > len(runes) {
                l = len(runes)
        }
++
        return string(runes[pos:l])
  }
  
@@@ -264,7 -269,7 +260,7 @@@ func Home() (string, error) 
        }
  
        // cross compile support
--      if "windows" == runtime.GOOS {
++      if windowsOS == osType {
                return homeWindows()
        }
  
@@@ -287,7 -292,7 +283,7 @@@ func homeUnix() (string, error) 
        }
  
        result := strings.TrimSpace(stdout.String())
--      if result == "" {
++      if len(result) == 0 {
                return "", errors.New("blank output when reading home 
directory")
        }
  
@@@ -298,12 -303,66 +294,66 @@@ func homeWindows() (string, error) 
        drive := os.Getenv("HOMEDRIVE")
        homePath := os.Getenv("HOMEPATH")
        home := drive + homePath
--      if drive == "" || homePath == "" {
++      if len(drive) == 0 || len(homePath) == 0 {
                home = os.Getenv("USERPROFILE")
        }
--      if home == "" {
++      if len(home) == 0 {
                return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are 
blank")
        }
  
        return home, nil
  }
+ 
+ func mkdirIfNecessary(urlRoot string) (string, error) {
+       if !legalPath(urlRoot) {
+               // not exist, use default, mac is: /XXX/xx/.dubbo/config-center
+               rp, err := Home()
+               if err != nil {
+                       return "", perrors.WithStack(err)
+               }
+ 
+               urlRoot = adapterUrl(rp)
+       }
+ 
+       if _, err := os.Stat(urlRoot); err != nil {
+               // it must be dir, if not exist, will create
+               if err = createDir(urlRoot); err != nil {
+                       return "", perrors.WithStack(err)
+               }
+       }
+ 
+       return urlRoot, nil
+ }
+ 
+ func legalPath(path string) bool {
+       if len(path) == 0 {
+               return false
+       }
+       if _, err := os.Stat(path); err != nil {
+               return false
+       }
+ 
+       return true
+ }
+ 
+ func adapterUrl(rp string) string {
 -      if osType == windows {
++      if osType == windowsOS {
+               return filepath.Join(rp, "_dubbo", "config-center")
+       }
+ 
+       return filepath.Join(rp, ".dubbo", "config-center")
+ }
+ 
+ // used for GetPath. param key default is instance's id.
+ // e.g: (ip:port) 127.0.0.1:20081, in windows env, will change to 
127_0_0_1_20081
+ func adapterKey(key string) string {
+       if len(key) == 0 {
+               return ""
+       }
+ 
 -      if osType == windows {
++      if osType == windowsOS {
+               return strings.ReplaceAll(strings.ReplaceAll(key, ".", "_"), 
":", "_")
+       }
+ 
+       return key
+ }

Reply via email to