package reposervice import ( "fmt" "os" "os/exec" "path" "path/filepath" "code.aides.space/aides-infra/aides-repo-api/internal/logger" ) 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.' 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 { log := logger.GetLogger() log.Debug(filepath.Dir(newPath)) if _, err := os.Stat(oldPath); err == nil { log.Debug(filepath.Dir(newPath)) if err = os.MkdirAll( filepath.Dir(newPath), os.ModePerm, ); err != nil { return err } return os.Rename(oldPath, newPath) } else if !os.IsNotExist(err) { return err } return nil }