ppforge/globals/globals.go

68 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-10-13 16:13:33 +02:00
package globals
import (
"errors"
"os"
"os/user"
"path"
"runtime"
"strings"
)
2023-10-27 13:38:35 +02:00
2023-10-13 16:13:33 +02:00
// Global variables, Mocks, etc.
// This package shall not have any dependency towards the application itself!
// [impl->dsn~properly-defined-globals~1>>utest]
// GLOBAL VARIABLES
// ConfigDirectoryList Configuration directory list
var ConfigDirectoryList []string
// ConfigDirectory Configuration directory string
var ConfigDirectory string
// CollectionOfProtocolsDir is the default directory for protocol collections
var CollectionOfProtocolsDir string
2023-10-27 13:38:35 +02:00
// COPCacheFileName is the filename for the cache
2023-10-13 16:13:33 +02:00
var COPCacheFileName string = "cop-cache.json"
2023-10-27 13:38:35 +02:00
2023-11-10 13:48:47 +01:00
////////////// Global Mockings
2023-10-27 13:38:35 +02:00
2023-11-10 13:48:47 +01:00
// MockUserCurrent is a variable to be able to mock the function call to `user.Current`
2023-10-27 13:38:35 +02:00
var MockUserCurrent = user.Current
2023-10-30 22:39:53 +01:00
// Marshaler interface to enrich structs which can be exported/imported even with private fields
/*
type Marshaler interface {
toJSON() ([]byte, error)
fromJSON() error
}
*/
// GetCurrentDir returns the current directory of the called function
func GetCurrentDir() string {
_, filename, _, _ := runtime.Caller(0)
return path.Dir(filename)
}
// GetRepoDir tries to find the parent git repository path
func GetRepoDir(start string) (string, error) {
var repoPath string
entries, err := os.ReadDir(start)
for _, v := range entries {
if strings.HasSuffix(v.Name(), ".git") {
repoPath = start
}
}
// linux
if start == "/" {
return "", errors.New("Parent repository dir couldn't be found")
}
// windows, how?
if repoPath == "" {
return GetRepoDir(path.Dir(start))
}
return repoPath, err
}