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 cc3e8d608571c47c0e02330d3a15011b4427cf0e 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 | 99 +++++++++++++++++++++++++++++--------- registry/file/service_discovery.go | 2 +- 2 files changed, 76 insertions(+), 25 deletions(-) diff --cc config_center/file/impl.go index 9afe7c6,0233f63..ee2ed9c --- 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 } @@@ -264,7 -269,7 +261,7 @@@ func Home() (string, error) } // cross compile support -- if "windows" == runtime.GOOS { ++ if windowsOS == osType { return homeWindows() } @@@ -307,3 -312,57 +304,57 @@@ func homeWindows() (string, error) 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 + }
