mirror of
https://github.com/kairos-io/kcrypt.git
synced 2025-07-17 08:51:30 +00:00
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:
parent
c0d6b81b5d
commit
8ca95e953b
4
main.go
4
main.go
@ -17,6 +17,8 @@ import (
|
|||||||
"github.com/mudler/go-pluggable"
|
"github.com/mudler/go-pluggable"
|
||||||
cp "github.com/otiai10/copy"
|
cp "github.com/otiai10/copy"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
|
pi "github.com/kairos-io/kcrypt/pkg/partition_info"
|
||||||
)
|
)
|
||||||
|
|
||||||
func waitdevice(device string, attempts int) error {
|
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.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) {
|
func findPartition(label string) (string, *block.Partition, error) {
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package partition_info
|
package partition_info
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jaypipes/ghw/pkg/block"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@ -15,6 +17,18 @@ import (
|
|||||||
// and make the request.
|
// and make the request.
|
||||||
type PartitionInfo map[string]string
|
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
|
// UpdatePartitionLabelMapping takes partition information as a string argument
|
||||||
// the the form: `label:name:uuid` (that's what the `kcrypt encrypt` command returns
|
// 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
|
// on success. This function stores it in the PartitionInfoFile yaml file for
|
||||||
@ -25,8 +39,8 @@ func UpdatePartitionLabelMapping(partitionData, file string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(partitionData, ":")
|
label, uuid := PartitionDataFromString(partitionData)
|
||||||
partitionInfo[parts[0]] = parts[2]
|
partitionInfo[label] = uuid
|
||||||
|
|
||||||
return UpdatePartitionInfoFile(partitionInfo, file)
|
return UpdatePartitionInfoFile(partitionInfo, file)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/jaypipes/ghw/pkg/block"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
@ -99,4 +100,36 @@ TO_KEEP: old_uuid_1
|
|||||||
Expect(string(dat)).To(Equal(expectedContent))
|
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"))
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user