Skip to content

Commit

Permalink
feat [close #111]: Add version key to recipe
Browse files Browse the repository at this point in the history
Adds a version key to the recipe to ensure the passed recipe is supported by the vib version being used
  • Loading branch information
axtloss committed Dec 20, 2024
1 parent 45896f2 commit 8ef23c1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
- name: Build
run: |
./set_new_version ${{ github.ref_name }}
go get ./...
make build
Expand Down
1 change: 1 addition & 0 deletions api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Source struct {
type Recipe struct {
Name string
Id string
Vibversion string
Stages []Stage
Path string
ParentPath string
Expand Down
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
"github.com/spf13/cobra"
)

var Version = "0.7.4"
var Version = "0.0.0"
var IsRoot = false
var OrigUID = 1000
var OrigGID = 1000
var OrigUser = "user"

var rootCmd = &cobra.Command{
Use: "vib",
Short: "Vib is a tool to build container images from recipes using modules",
Long: "Vib is a tool to build container images from YAML recipes using modules to define the steps to build the image.",
Use: "vib",
Short: "Vib is a tool to build container images from recipes using modules",
Long: "Vib is a tool to build container images from YAML recipes using modules to define the steps to build the image.",
SilenceUsage: true,
Version: Version,
Version: Version,
}

// Initialize the root command with build, test, and compile commands
Expand Down
30 changes: 30 additions & 0 deletions core/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package core
import (
"fmt"
"io"
"math"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/vanilla-os/vib/api"
"gopkg.in/yaml.v3"
)

var Min_Recipe_Version = []uint8{1, 0, 0}

// LoadRecipe loads a recipe from a file and returns a Recipe
// Does not validate the recipe but it will catch some errors
// a proper validation will be done in the future
Expand Down Expand Up @@ -44,6 +48,32 @@ func LoadRecipe(path string) (*api.Recipe, error) {
return nil, err
}

if len(strings.TrimSpace(recipe.Vibversion)) <= 0 {
return nil, fmt.Errorf("version key not found in recipe file, assuming outdated recipe")
}

recipeVersionS := strings.Split(recipe.Vibversion, ".")
if len(recipeVersionS) != 3 {
return nil, fmt.Errorf("invalid version format, expected x.x.x, got %s", recipe.Vibversion)
}

recipeVersion := []uint8{0, 0, 0}
for i := 0; i < len(recipeVersion); i++ {
versionInt, err := strconv.ParseUint(recipeVersionS[i], 10, 8)
if err != nil {
return nil, err
}
if versionInt > math.MaxUint8 {
recipeVersion[i] = math.MaxUint8
} else {
recipeVersion[i] = uint8(versionInt)
}
}

if recipeVersion[0] < Min_Recipe_Version[0] || recipeVersion[1] < Min_Recipe_Version[1] || recipeVersion[2] < Min_Recipe_Version[2] {
return nil, fmt.Errorf("outdated recipe, this version of vib supports recipes starting at version %s", strings.Join(strings.Fields(fmt.Sprint(Min_Recipe_Version)), "."))
}

// the recipe path is stored in the recipe itself
// for convenience
recipe.Path = recipePath
Expand Down
19 changes: 19 additions & 0 deletions set_new_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -eu

if [ $# -lt 1 ]; then
echo "usag: $0 <version>"
exit 1
fi

IFS='.' read -ra VERS <<< ${1##v}
if [ ${#VERS[@]} -lt 3 ]; then
echo "invalid version number"
exit 2
fi

sed -i "s|var Version = \"0.0.0\"|var Version = \"${1##v}\"|g" cmd/root.go

sed -i "s|0, 0, 0|${VERS[0]}, ${VERS[1]}, ${VERS[2]}|" core/loader.go

0 comments on commit 8ef23c1

Please sign in to comment.