aides-repo-api/internal/services/reposervice/utils.go

103 lines
2.1 KiB
Go
Raw Normal View History

2024-12-24 19:40:37 +00:00
package reposervice
import (
"fmt"
"os"
"os/exec"
"path"
2025-01-07 15:32:20 +00:00
"path/filepath"
2024-12-24 19:40:37 +00:00
"code.aides.space/aides-infra/aides-repo-api/internal/logger"
2024-12-24 19:40:37 +00:00
)
func createRepoDirs(repoDir, repoName, arch string) error {
log := logger.GetLogger()
// Create the 'base' directory
baseDir := path.Join(repoDir, arch, "base")
err := os.MkdirAll(baseDir, os.ModePerm)
if err != nil {
log.Error("Failed to create directory", map[string]interface{}{
"directory": baseDir,
"err": err,
})
return err
}
// Create the 'RPMS.<REPO_NAME>' directory
rpmsDir := path.Join(repoDir, arch, fmt.Sprintf("RPMS.%s", repoName))
err = os.MkdirAll(rpmsDir, os.ModePerm)
if err != nil {
log.Error("Failed to create directory", map[string]interface{}{
"directory": rpmsDir,
"err": err,
})
return err
}
return nil
}
func runGenbasedir(repoDir, arch, repoName string) error {
log := logger.GetLogger()
cmd := exec.Command(
"genbasedir",
"--bloat",
"--progress",
fmt.Sprintf("--topdir=%s", repoDir),
arch,
repoName,
)
cmd.Stdout = nil
cmd.Stderr = nil
err := cmd.Run()
if err != nil {
log.Error(
"Failed to run genbasedir",
map[string]interface{}{
"arch": arch,
"error": err,
"cmd_error": cmd.Stderr,
},
)
return err
}
log.Debug("Successfully ran genbasedir", map[string]interface{}{
"arch": arch,
})
return nil
}
func createSymlink(target, link string) error {
if _, err := os.Lstat(link); err == nil {
if err := os.Remove(link); err != nil {
return fmt.Errorf("failed to remove existing file or symlink: %w", err)
}
}
if err := os.Symlink(target, link); err != nil {
return fmt.Errorf("failed to create symlink: %w", err)
}
return nil
}
func renameIfExists(oldPath, newPath string) error {
2025-01-07 15:32:20 +00:00
log := logger.GetLogger()
log.Debug(filepath.Dir(newPath))
2024-12-24 19:40:37 +00:00
if _, err := os.Stat(oldPath); err == nil {
2025-01-07 15:32:20 +00:00
log.Debug(filepath.Dir(newPath))
if err = os.MkdirAll(
filepath.Dir(newPath),
os.ModePerm,
); err != nil {
return err
}
2024-12-24 19:40:37 +00:00
return os.Rename(oldPath, newPath)
} else if !os.IsNotExist(err) {
return err
}
return nil
}