Skip to content

Commit

Permalink
file paths input fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxcode committed Apr 30, 2023
1 parent 7a170c4 commit c84be6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
14 changes: 9 additions & 5 deletions cmd/edm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
41 changes: 21 additions & 20 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/accs/accessories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c84be6c

Please sign in to comment.