Add functions to produce a string representation of a partition

so that we encapsulate all the logic in the same package

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis 2022-11-11 12:11:43 +02:00
parent c0d6b81b5d
commit 8ca95e953b
No known key found for this signature in database
GPG Key ID: 286DCAFD2C97DDE3
3 changed files with 52 additions and 3 deletions

View File

@ -17,6 +17,8 @@ import (
"github.com/mudler/go-pluggable"
cp "github.com/otiai10/copy"
"github.com/urfave/cli"
pi "github.com/kairos-io/kcrypt/pkg/partition_info"
)
func waitdevice(device string, attempts int) error {
@ -152,7 +154,7 @@ func luksify(label string) (string, error) {
return "", fmt.Errorf("err: %w, out: %s", err, out2)
}
return fmt.Sprintf("%s:%s:%s", b.Label, b.Name, b.UUID), nil
return pi.PartitionToString(b), nil
}
func findPartition(label string) (string, *block.Partition, error) {

View File

@ -1,9 +1,11 @@
package partition_info
import (
"fmt"
"io/ioutil"
"strings"
"github.com/jaypipes/ghw/pkg/block"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
@ -15,6 +17,18 @@ import (
// and make the request.
type PartitionInfo map[string]string
func PartitionToString(p *block.Partition) string {
return fmt.Sprintf("%s:%s:%s", p.Label, p.Name, p.UUID)
}
// Takes a partition info string (as returned by PartitionToString) and return
// the partition label and the UUID
func PartitionDataFromString(partitionStr string) (string, string) {
parts := strings.Split(partitionStr, ":")
return parts[0], parts[2]
}
// UpdatePartitionLabelMapping takes partition information as a string argument
// the the form: `label:name:uuid` (that's what the `kcrypt encrypt` command returns
// on success. This function stores it in the PartitionInfoFile yaml file for
@ -25,8 +39,8 @@ func UpdatePartitionLabelMapping(partitionData, file string) error {
return err
}
parts := strings.Split(partitionData, ":")
partitionInfo[parts[0]] = parts[2]
label, uuid := PartitionDataFromString(partitionData)
partitionInfo[label] = uuid
return UpdatePartitionInfoFile(partitionInfo, file)
}

View File

@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"github.com/jaypipes/ghw/pkg/block"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
@ -99,4 +100,36 @@ TO_KEEP: old_uuid_1
Expect(string(dat)).To(Equal(expectedContent))
})
})
Describe("PartitionToString", func() {
var partition *block.Partition
BeforeEach(func() {
partition = &block.Partition{
Disk: nil,
Name: "sda1",
Label: "COS_PERSISTENT",
MountPoint: "/mnt/sda1",
UUID: "some_uuid_here",
}
})
It("returns a string representation of the partition data", func() {
Expect(pi.PartitionToString(partition)).To(Equal("COS_PERSISTENT:sda1:some_uuid_here"))
})
})
Describe("PartitionDataFromString", func() {
var partitionData string
BeforeEach(func() {
partitionData = "THE_LABEL:the_name:the_uuid"
})
It("returns the label and the uuid", func() {
label, uuid := pi.PartitionDataFromString(partitionData)
Expect(label).To(Equal("THE_LABEL"))
Expect(uuid).To(Equal("the_uuid"))
})
})
})