chore: move everything out from src
This commit is contained in:
62
helpers/osinfo.go
Normal file
62
helpers/osinfo.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const OsIdentifierDefault = "default"
|
||||
const OsIdentifierLinux = "linux"
|
||||
const OsIdentifierMac = "macos"
|
||||
const OsIdentifierWindows = "windows"
|
||||
|
||||
const osReleasePathLinux = "/etc/os-release"
|
||||
|
||||
func GetOsIdentifier() string {
|
||||
baseIdentifier := getBaseOsIdentifier()
|
||||
|
||||
return baseIdentifier + "-" + getNormalizedArch()
|
||||
}
|
||||
|
||||
func IsOnPlatform(platform string) bool {
|
||||
return strings.Index(GetOsIdentifier(), platform) == 0
|
||||
}
|
||||
|
||||
func getBaseOsIdentifier() string {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
return OsIdentifierWindows
|
||||
case "darwin":
|
||||
return OsIdentifierMac
|
||||
case "linux":
|
||||
file, err := os.Open(osReleasePathLinux)
|
||||
|
||||
if err != nil {
|
||||
return OsIdentifierLinux
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.ToLower(scanner.Text())
|
||||
if strings.HasPrefix(line, "id=") {
|
||||
return OsIdentifierLinux + "-" + strings.Trim(line[3:], "\"")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return runtime.GOOS
|
||||
}
|
||||
|
||||
func getNormalizedArch() string {
|
||||
switch runtime.GOARCH {
|
||||
case "amd64":
|
||||
return "x64"
|
||||
case "386":
|
||||
return "x86"
|
||||
}
|
||||
|
||||
return runtime.GOARCH
|
||||
}
|
||||
28
helpers/path.go
Normal file
28
helpers/path.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ResolvePath(path string) string {
|
||||
return ResolvePathWithDefault(path, "")
|
||||
}
|
||||
func ResolvePathWithDefault(path string, currentDirectory string) string {
|
||||
skipCombiningCurrentDirectory := false
|
||||
if strings.Contains(path, "~") {
|
||||
home, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
path = strings.ReplaceAll(path, "~", home)
|
||||
skipCombiningCurrentDirectory = true
|
||||
}
|
||||
}
|
||||
|
||||
if currentDirectory != "" && !skipCombiningCurrentDirectory {
|
||||
path = filepath.Join(currentDirectory, path)
|
||||
}
|
||||
|
||||
path = filepath.FromSlash(path)
|
||||
return path
|
||||
}
|
||||
75
helpers/repo.go
Normal file
75
helpers/repo.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"alma/models"
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func GetModuleInfo(args []string) (moduleInfo models.ModuleInfo, error error) {
|
||||
repoName, moduleName := GetRepositoryAndModuleName(args)
|
||||
|
||||
if moduleName == "" {
|
||||
return models.ModuleInfo{}, errors.New("module name is required")
|
||||
}
|
||||
|
||||
sourceDirectory, targetDirectory := GetRepositorySourceAndTargetDirectory(repoName)
|
||||
if sourceDirectory == "" {
|
||||
return models.ModuleInfo{}, errors.New("source directory not exists")
|
||||
}
|
||||
|
||||
sourceDirectoryFolderInfo, err := os.Stat(sourceDirectory)
|
||||
if err != nil || !sourceDirectoryFolderInfo.IsDir() {
|
||||
return models.ModuleInfo{}, errors.New("source directory not exists")
|
||||
}
|
||||
|
||||
moduleNameAsPath := strings.ReplaceAll(moduleName, "/", string(filepath.Separator))
|
||||
moduleDirectory := filepath.Join(sourceDirectory, moduleNameAsPath)
|
||||
|
||||
return models.ModuleInfo{
|
||||
RepositoryName: repoName,
|
||||
ModuleName: moduleName,
|
||||
SourceDirectory: sourceDirectory,
|
||||
TargetDirectory: targetDirectory,
|
||||
ModuleDirectory: moduleDirectory}, nil
|
||||
}
|
||||
|
||||
func GetRepositoryAndModuleName(args []string) (string, string) {
|
||||
return getRepositoryAndModuleName(args, false)
|
||||
}
|
||||
|
||||
func getRepositoryAndModuleName(args []string, singleParamIsRepository bool) (string, string) {
|
||||
var repositoryName, moduleName string = "", ""
|
||||
|
||||
usefulArgs := lo.Filter(args, func(arg string, index int) bool { return !strings.HasPrefix(arg, "-") })
|
||||
if len(usefulArgs) == 1 {
|
||||
if singleParamIsRepository {
|
||||
repositoryName = usefulArgs[0]
|
||||
} else {
|
||||
moduleName = usefulArgs[0]
|
||||
}
|
||||
} else if len(usefulArgs) >= 1 {
|
||||
repositoryName = usefulArgs[0]
|
||||
moduleName = usefulArgs[1]
|
||||
}
|
||||
|
||||
return repositoryName, moduleName
|
||||
}
|
||||
|
||||
func GetRepositorySourceAndTargetDirectory(repoName string) (string, string) {
|
||||
repoSourceDirectory, _ := os.Getwd()
|
||||
repoTargetDirectory, _ := os.Getwd()
|
||||
repoTargetDirectory = path.Join(repoTargetDirectory, "..")
|
||||
|
||||
return GetRepositorySourceAndTargetDirectoryWithFallback(repoName, repoSourceDirectory, repoTargetDirectory)
|
||||
}
|
||||
|
||||
func GetRepositorySourceAndTargetDirectoryWithFallback(repoName string, sourceFallback string, targetFallback string) (string, string) {
|
||||
//TODO: Use repository configuration
|
||||
return sourceFallback, targetFallback
|
||||
}
|
||||
Reference in New Issue
Block a user