68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package globals
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// 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
|
|
|
|
// COPCacheFileName is the filename for the cache
|
|
var COPCacheFileName string = "cop-cache.json"
|
|
|
|
////////////// Global Mockings
|
|
|
|
// MockUserCurrent is a variable to be able to mock the function call to `user.Current`
|
|
var MockUserCurrent = user.Current
|
|
|
|
// 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
|
|
}
|