Skip to content

Commit

Permalink
recipe: Better json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Oct 12, 2023
1 parent 328d89a commit 6037681
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
2 changes: 1 addition & 1 deletion core/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (disk *Disk) LabelDisk(label DiskLabel) error {
// If fsType is an empty string, the function will skip creating the filesystem.
// This can be useful when creating LUKS-encrypted partitions, where the format
// operation needs to be executed first.
func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int64) (*Partition, error) {
func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int) (*Partition, error) {
createPartCmd := "parted -s %s unit MiB mkpart%s%s %s %d %s"

var partType string
Expand Down
29 changes: 3 additions & 26 deletions core/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -63,33 +62,11 @@ func ReadRecipe(path string) (*Recipe, error) {
}

var recipe Recipe
dec := json.NewDecoder(strings.NewReader(string(content)))
dec.DisallowUnknownFields()
dec.UseNumber()
err = dec.Decode(&recipe)
err = json.Unmarshal(content, &recipe)
if err != nil {
return nil, fmt.Errorf("failed to read recipe: %s", err)
}

// Convert json.Number to int64
for i := 0; i < len(recipe.Setup); i++ {
step := &recipe.Setup[i]
formattedParams := []interface{}{}
for _, param := range step.Params {
dummy := "1"
if reflect.TypeOf(param) == reflect.TypeOf(dummy) {
convertedParam, err := param.(json.Number).Int64()
if err != nil {
return nil, fmt.Errorf("failed to convert recipe parameter: %s", err)
}
formattedParams = append(formattedParams, convertedParam)
} else {
formattedParams = append(formattedParams, param)
}
}
step.Params = formattedParams
}

return &recipe, nil
}

Expand Down Expand Up @@ -134,8 +111,8 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
case "mkpart":
name := args[0].(string)
fsType := PartitionFs(args[1].(string))
start := args[2].(int64)
end := args[3].(int64)
start := int(args[2].(float64))
end := int(args[3].(float64))
if len(args) > 4 && strings.HasPrefix(string(fsType), "luks-") {
luksPassword := args[4].(string)
part, err := disk.NewPartition(name, "", start, end)
Expand Down

0 comments on commit 6037681

Please sign in to comment.