forked from wrenn/wrenn
v0.1.0 (#17)
This commit is contained in:
@ -1,11 +1,15 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.omukk.dev/wrenn/wrenn/internal/id"
|
||||
"git.omukk.dev/wrenn/wrenn/pkg/id"
|
||||
)
|
||||
|
||||
// IsMinimal reports whether the given team and template IDs represent the
|
||||
@ -47,6 +51,75 @@ func KernelPath(wrennDir string) string {
|
||||
return filepath.Join(wrennDir, "kernels", "vmlinux")
|
||||
}
|
||||
|
||||
// KernelPathVersioned returns the path to a specific kernel version.
|
||||
func KernelPathVersioned(wrennDir, version string) string {
|
||||
return filepath.Join(wrennDir, "kernels", "vmlinux-"+version)
|
||||
}
|
||||
|
||||
// LatestKernel scans the kernels directory for files matching vmlinux-{semver}
|
||||
// and returns the path and version of the latest one (by semver sort).
|
||||
func LatestKernel(wrennDir string) (path, version string, err error) {
|
||||
dir := filepath.Join(wrennDir, "kernels")
|
||||
return latestVersionedFile(dir, "vmlinux-")
|
||||
}
|
||||
|
||||
// latestVersionedFile scans dir for files with the given prefix, extracts the
|
||||
// version suffix, sorts by semver, and returns the path and version of the latest.
|
||||
func latestVersionedFile(dir, prefix string) (path, version string, err error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("read directory %s: %w", dir, err)
|
||||
}
|
||||
|
||||
var versions []string
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := e.Name()
|
||||
if v, ok := strings.CutPrefix(name, prefix); ok && v != "" {
|
||||
versions = append(versions, v)
|
||||
}
|
||||
}
|
||||
|
||||
if len(versions) == 0 {
|
||||
return "", "", fmt.Errorf("no %s* files found in %s", prefix, dir)
|
||||
}
|
||||
|
||||
sort.Slice(versions, func(i, j int) bool {
|
||||
return compareSemver(versions[i], versions[j]) < 0
|
||||
})
|
||||
|
||||
latest := versions[len(versions)-1]
|
||||
return filepath.Join(dir, prefix+latest), latest, nil
|
||||
}
|
||||
|
||||
// compareSemver compares two dotted-numeric version strings.
|
||||
// Returns -1 if a < b, 0 if equal, 1 if a > b.
|
||||
func compareSemver(a, b string) int {
|
||||
aParts := strings.Split(a, ".")
|
||||
bParts := strings.Split(b, ".")
|
||||
|
||||
maxLen := max(len(aParts), len(bParts))
|
||||
|
||||
for i := 0; i < maxLen; i++ {
|
||||
var av, bv int
|
||||
if i < len(aParts) {
|
||||
_, _ = fmt.Sscanf(aParts[i], "%d", &av)
|
||||
}
|
||||
if i < len(bParts) {
|
||||
_, _ = fmt.Sscanf(bParts[i], "%d", &bv)
|
||||
}
|
||||
if av < bv {
|
||||
return -1
|
||||
}
|
||||
if av > bv {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// ImagesRoot returns the root images directory.
|
||||
func ImagesRoot(wrennDir string) string {
|
||||
return filepath.Join(wrennDir, "images")
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.omukk.dev/wrenn/wrenn/internal/id"
|
||||
"git.omukk.dev/wrenn/wrenn/pkg/id"
|
||||
)
|
||||
|
||||
func TestIsMinimal(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user