From c84be6c322c78c302489fdd5d28ac00c718ad506 Mon Sep 17 00:00:00 2001 From: Alexander Vankov Date: Sun, 30 Apr 2023 18:54:07 +0400 Subject: [PATCH] file paths input fixes --- cmd/edm/main.go | 14 ++++++++----- internal/config/config.go | 41 ++++++++++++++++++++------------------- pkg/accs/accessories.go | 2 +- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/cmd/edm/main.go b/cmd/edm/main.go index 5b30e50..fb904ba 100644 --- a/cmd/edm/main.go +++ b/cmd/edm/main.go @@ -59,11 +59,11 @@ func main() { // Reading command-line arguments filldb := false consolelog := false - createdb := "false" - runbrowser := "false" + createdb := "" + runbrowser := "" onlybrowser := false config := CONFIGPATH - createdb, filldb, runbrowser, onlybrowser, consolelog, config, err = processCmdLineArgs(cfg.CreateDB, filldb, cfg.RunBrowser, onlybrowser, consolelog) + createdb, filldb, runbrowser, onlybrowser, consolelog, config, err = processCmdLineArgs(createdb, filldb, runbrowser, onlybrowser, consolelog) if err != nil { fmt.Println(err) os.Exit(1) @@ -73,8 +73,12 @@ func main() { if err != nil { log.Println(accs.CurrentFunction()+":", err) } - cfg.CreateDB = createdb - cfg.RunBrowser = runbrowser + if createdb != "" { + cfg.CreateDB = createdb + } + if runbrowser != "" { + cfg.RunBrowser = runbrowser + } // Server root path: if accs.FileExists(cfg.ServerRoot) != true { diff --git a/internal/config/config.go b/internal/config/config.go index 2cca4a3..0930649 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -53,31 +53,39 @@ func (cfg *Config) ReadConfig(configPath string, serverRoot string) error { homeDir = "." // trying to write to the current dir if getting homedir failed } if configPath == "" { - configPath = filepath.Join(homeDir, appDir) //default config path + configPath = filepath.Join(homeDir, appDir, defaultiniFname) //default config path } if serverRoot == "" { cfg.ServerRoot = filepath.Join(homeDir, appDir) //default server root path } else { cfg.ServerRoot = serverRoot } - ConfigFile := configPath - if !strings.HasSuffix(configPath, "cfg") || !strings.HasSuffix(configPath, "conf") || !strings.HasSuffix(configPath, "ini") { - ConfigFile = filepath.Join(configPath, defaultiniFname) + ConfigFileFull := configPath + fileInfo, _ := os.Stat(ConfigFileFull) + isDir := false + if fileInfo != nil { + isDir = fileInfo.IsDir() + } + if strings.HasSuffix(configPath, "\\") || strings.HasSuffix(configPath, "/") || isDir { + ConfigFileFull = filepath.Join(configPath, defaultiniFname) } if core.DEBUG { - log.Println("Using config file: ", ConfigFile) + log.Println("Using config file:", ConfigFileFull) } - if _, err := os.Stat(configPath); err != nil { - if os.IsNotExist(err) { - err := os.Mkdir(configPath, 0700) - if err != nil { - return err + path, _ := filepath.Split(ConfigFileFull) + if path != "." && path != "" { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + err := os.Mkdir(path, 0700) + if err != nil { + return err + } } } } - if _, err := os.Stat(ConfigFile); err != nil { + if _, err := os.Stat(ConfigFileFull); err != nil { if os.IsNotExist(err) { - f, err := os.Create(ConfigFile) + f, err := os.Create(ConfigFileFull) if err != nil { return err } @@ -88,17 +96,10 @@ func (cfg *Config) ReadConfig(configPath string, serverRoot string) error { f.Sync() } } else { - mapOfConfig, err := readini(ConfigFile) + mapOfConfig, err := readini(ConfigFileFull) if err != nil { return err } - // If Config file has no valid config lines, then remove the file - if len(mapOfConfig) == 0 { - err := os.Remove(ConfigFile) - if err != nil { - return err - } - } // Reading Config from a map to a struct readMapToCfgStruct(mapOfConfig, cfg) diff --git a/pkg/accs/accessories.go b/pkg/accs/accessories.go index b36ff6a..46ca623 100644 --- a/pkg/accs/accessories.go +++ b/pkg/accs/accessories.go @@ -155,7 +155,7 @@ func GetTextIDfromURL(path string) string { } func GetAbsoluteOrRelativePath(defaultPath string, targetPath string) string { - if strings.HasPrefix(targetPath, "/") || strings.HasPrefix(targetPath, "C:") { + if filepath.IsAbs(targetPath) { return targetPath } return filepath.Join(defaultPath, targetPath)