mirror of
https://github.com/kairos-io/kcrypt.git
synced 2025-07-31 23:15:06 +00:00
Merge pull request #4 from kairos-io/create-info-file-if-not-exists
Create the partition file if it doesn't exist
This commit is contained in:
commit
44ccb84dfb
@ -3,6 +3,7 @@ package partition_info
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jaypipes/ghw/pkg/block"
|
"github.com/jaypipes/ghw/pkg/block"
|
||||||
@ -22,16 +23,27 @@ type PartitionInfo struct {
|
|||||||
mapping map[string]string
|
mapping map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPartitionInfoFromFile(file string) (*PartitionInfo, error) {
|
// NewPartitionInfoFromFile reads the given partition info file (if one exists)
|
||||||
|
// and returns a pointer to a PartitionInfo object.
|
||||||
|
// If a file doesn't exist, the function will create one and return an "empty"
|
||||||
|
// PartitionInfo object.
|
||||||
|
// The boolean return value indicates whether a file existed or not (true means,
|
||||||
|
// a file existed already).
|
||||||
|
func NewPartitionInfoFromFile(file string) (*PartitionInfo, bool, error) {
|
||||||
|
existed, err := createInfoFileIfNotExists(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, existed, err
|
||||||
|
}
|
||||||
|
|
||||||
mapping, err := ParsePartitionInfoFile(file)
|
mapping, err := ParsePartitionInfoFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, existed, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &PartitionInfo{
|
return &PartitionInfo{
|
||||||
file: file,
|
file: file,
|
||||||
mapping: mapping,
|
mapping: mapping,
|
||||||
}, nil
|
}, existed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pi PartitionInfo) LookupUUIDForLabel(l string) string {
|
func (pi PartitionInfo) LookupUUIDForLabel(l string) string {
|
||||||
@ -98,3 +110,16 @@ func ParsePartitionInfoFile(file string) (map[string]string, error) {
|
|||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createInfoFileIfNotExists returns true if file already exists or creates the
|
||||||
|
// the file if it doesn't exist and returns false.
|
||||||
|
func createInfoFileIfNotExists(fileName string) (bool, error) {
|
||||||
|
_, err := os.Stat(fileName)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
if _, err := os.Create(fileName); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package partition_info_test
|
package partition_info_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jaypipes/ghw/pkg/block"
|
"github.com/jaypipes/ghw/pkg/block"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
@ -18,11 +21,31 @@ var _ = Describe("Partition Info file parsing", func() {
|
|||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
file = "../../tests/assets/partition_info.yaml"
|
file = "../../tests/assets/partition_info.yaml"
|
||||||
})
|
})
|
||||||
|
When("the files exists already", func() {
|
||||||
It("returns a PartitionInfo", func() {
|
It("returns 'true' and a PartitionInfo object", func() {
|
||||||
result, err := pi.NewPartitionInfoFromFile(file)
|
result, existed, err := pi.NewPartitionInfoFromFile(file)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(result).ToNot(BeNil())
|
Expect(result).ToNot(BeNil())
|
||||||
|
Expect(existed).To(BeTrue())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
When("a file doesn't exist", func() {
|
||||||
|
var fileName string
|
||||||
|
BeforeEach(func() {
|
||||||
|
fileName = path.Join(
|
||||||
|
os.TempDir(),
|
||||||
|
fmt.Sprintf("partition-info-%d.yaml", time.Now().UnixNano()))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("creates the file and returns 'false' and an (empty) mapping", func() {
|
||||||
|
result, existed, err := pi.NewPartitionInfoFromFile(fileName)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(result).ToNot(BeNil())
|
||||||
|
Expect(existed).To(BeFalse())
|
||||||
|
_, err = os.Stat(fileName)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -86,7 +109,7 @@ var _ = Describe("Partition Info file parsing", func() {
|
|||||||
_, err = file.Write([]byte("TO_KEEP: old_uuid_1"))
|
_, err = file.Write([]byte("TO_KEEP: old_uuid_1"))
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
partitionInfo, err = pi.NewPartitionInfoFromFile(file.Name())
|
partitionInfo, _, err = pi.NewPartitionInfoFromFile(file.Name())
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -117,7 +140,7 @@ TO_KEEP: old_uuid_1
|
|||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
file = "../../tests/assets/partition_info.yaml"
|
file = "../../tests/assets/partition_info.yaml"
|
||||||
partitionInfo, err = pi.NewPartitionInfoFromFile(file)
|
partitionInfo, _, err = pi.NewPartitionInfoFromFile(file)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -139,7 +162,7 @@ TO_KEEP: old_uuid_1
|
|||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
file = "../../tests/assets/partition_info.yaml"
|
file = "../../tests/assets/partition_info.yaml"
|
||||||
partitionInfo, err = pi.NewPartitionInfoFromFile(file)
|
partitionInfo, _, err = pi.NewPartitionInfoFromFile(file)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user