generated from VLADIMIR/template
clear
This commit is contained in:
+28
-11
@@ -1,19 +1,36 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func GetStoryFilepath() string {
|
||||
storyFilename := os.Getenv("STORY_FILENAME")
|
||||
if storyFilename != "" {
|
||||
return storyFilename
|
||||
}
|
||||
return "./data/story/story.json"
|
||||
return getFilepath("STORY_FILENAME", "data/story/story.json")
|
||||
}
|
||||
|
||||
func GetDBFilepath() string {
|
||||
storyFilename := os.Getenv("DB_FILENAME")
|
||||
if storyFilename != "" {
|
||||
return storyFilename
|
||||
}
|
||||
return "data/db/store.db"
|
||||
return getFilepath("DB_FILENAME", "data/db/store.db")
|
||||
}
|
||||
|
||||
func getFilepath(env string, defaultFilepath string) string {
|
||||
filepath := selectFilepath(env, defaultFilepath)
|
||||
ensureDirExists(filepath)
|
||||
return filepath
|
||||
}
|
||||
|
||||
func selectFilepath(env string, defaultFilepath string) string {
|
||||
filepath := os.Getenv(env)
|
||||
if filepath != "" {
|
||||
return filepath
|
||||
}
|
||||
return defaultFilepath
|
||||
}
|
||||
|
||||
func ensureDirExists(filePath string) error {
|
||||
dir := filepath.Dir(filePath)
|
||||
if dir == "" || dir == "." || dir == "/" {
|
||||
return nil
|
||||
}
|
||||
return os.MkdirAll(dir, 0755)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user