Initial commit
This commit is contained in:
10
internal/where/env.go
Normal file
10
internal/where/env.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package where
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"hub.cybercinch.nz/guisea/go-template/internal/app"
|
||||
)
|
||||
|
||||
// EnvConfigPath is the environment variable name for the config path
|
||||
var EnvConfigPath = strings.ToUpper(app.Name) + "_CONFIG_PATH"
|
||||
14
internal/where/util.go
Normal file
14
internal/where/util.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package where
|
||||
|
||||
import (
|
||||
"github.com/samber/lo"
|
||||
"hub.cybercinch.nz/guisea/go-template/internal/filesystem"
|
||||
"os"
|
||||
)
|
||||
|
||||
// mkdir creates a directory and all parent directories if they don't exist
|
||||
// will return the path of the directory
|
||||
func mkdir(path string) string {
|
||||
lo.Must0(filesystem.Api().MkdirAll(path, os.ModePerm))
|
||||
return path
|
||||
}
|
||||
68
internal/where/where.go
Normal file
68
internal/where/where.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package where
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"hub.cybercinch.nz/guisea/go-template/internal/app"
|
||||
)
|
||||
|
||||
func home() string {
|
||||
home, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
return home
|
||||
}
|
||||
|
||||
return "."
|
||||
}
|
||||
|
||||
// Config path
|
||||
// Will create the directory if it doesn't exist
|
||||
func Config() string {
|
||||
var path string
|
||||
|
||||
if customDir, present := os.LookupEnv(EnvConfigPath); present {
|
||||
return mkdir(customDir)
|
||||
}
|
||||
|
||||
var userConfigDir string
|
||||
|
||||
if runtime.GOOS == "darwin" {
|
||||
userConfigDir = filepath.Join(home(), ".config")
|
||||
} else {
|
||||
var err error
|
||||
userConfigDir, err = os.UserConfigDir()
|
||||
if err != nil {
|
||||
userConfigDir = filepath.Join(home(), ".config")
|
||||
}
|
||||
}
|
||||
|
||||
path = filepath.Join(userConfigDir, app.Name)
|
||||
return mkdir(path)
|
||||
}
|
||||
|
||||
// Logs path
|
||||
// Will create the directory if it doesn't exist
|
||||
func Logs() string {
|
||||
return mkdir(filepath.Join(Cache(), "logs"))
|
||||
}
|
||||
|
||||
// Cache path
|
||||
// Will create the directory if it doesn't exist
|
||||
func Cache() string {
|
||||
userCacheDir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
userCacheDir = "."
|
||||
}
|
||||
|
||||
cacheDir := filepath.Join(userCacheDir, app.Name)
|
||||
return mkdir(cacheDir)
|
||||
}
|
||||
|
||||
// Temp path
|
||||
// Will create the directory if it doesn't exist
|
||||
func Temp() string {
|
||||
tempDir := filepath.Join(os.TempDir(), app.Name)
|
||||
return mkdir(tempDir)
|
||||
}
|
||||
Reference in New Issue
Block a user