diff --git a/cmd/otk/osbuild-gen-partition-table/main_test.go b/cmd/otk/osbuild-gen-partition-table/main_test.go index 4a0c8c4433..5788551510 100644 --- a/cmd/otk/osbuild-gen-partition-table/main_test.go +++ b/cmd/otk/osbuild-gen-partition-table/main_test.go @@ -147,7 +147,7 @@ func TestUnmarshalOutput(t *testing.T) { "partition-table": { "Size": 911, "UUID": "", - "Type": 0, + "Type": "", "Partitions": [ { "Start": 0, @@ -227,7 +227,7 @@ var expectedSimplePartOutput = `{ "partition-table": { "Size": 11821645824, "UUID": "dbd21911-1c4e-4107-8a9f-14fe6e751358", - "Type": 2, + "Type": "gpt", "Partitions": [ { "Start": 9048576, diff --git a/pkg/disk/disk.go b/pkg/disk/disk.go index 2484295ba2..a0b585feeb 100644 --- a/pkg/disk/disk.go +++ b/pkg/disk/disk.go @@ -119,39 +119,33 @@ func NewFSType(s string) (FSType, error) { } } -// PartitionTableType is the partition table type enum. -type PartitionTableType uint64 +// PartitionTableType is the partition table type +type PartitionTableType string const ( - PT_NONE PartitionTableType = iota - PT_DOS - PT_GPT + PT_NONE PartitionTableType = "" + PT_DOS PartitionTableType = "dos" + PT_GPT PartitionTableType = "gpt" ) -func (t PartitionTableType) String() string { - switch t { +func NewPartitionTableType(s string) (PartitionTableType, error) { + switch PartitionTableType(s) { case PT_NONE: - return "" + return PT_NONE, nil case PT_DOS: - return "dos" + return PT_DOS, nil case PT_GPT: - return "gpt" + return PT_GPT, nil default: - panic(fmt.Sprintf("unknown or unsupported partition table type with enum value %d", t)) + return PT_NONE, fmt.Errorf("unknown or unsupported partition table type name: %s", s) } } -func NewPartitionTableType(s string) (PartitionTableType, error) { - switch s { - case "": - return PT_NONE, nil - case "dos": - return PT_DOS, nil - case "gpt": - return PT_GPT, nil - default: - return PT_NONE, fmt.Errorf("unknown or unsupported partition table type name: %s", s) +func (t PartitionTableType) String() string { + if _, err := NewPartitionTableType(string(t)); err != nil { + panic(fmt.Sprintf("unknown or unsupported partition table type with value: %v", string(t))) } + return string(t) } // Entity is the base interface for all disk-related entities. diff --git a/pkg/disk/enums_test.go b/pkg/disk/enums_test.go index 0bb0f3ac08..3174a55d37 100644 --- a/pkg/disk/enums_test.go +++ b/pkg/disk/enums_test.go @@ -26,8 +26,8 @@ func TestEnumPartitionTableType(t *testing.T) { } // error test: bad value - badPtt := disk.PartitionTableType(3) - assert.PanicsWithValue("unknown or unsupported partition table type with enum value 3", func() { _ = badPtt.String() }) + badPtt := disk.PartitionTableType("bad") + assert.PanicsWithValue("unknown or unsupported partition table type with value: bad", func() { _ = badPtt.String() }) // error test: bad name _, err := disk.NewPartitionTableType("not-a-type") diff --git a/pkg/disk/partition_table.go b/pkg/disk/partition_table.go index f898f172db..cccfa93dbe 100644 --- a/pkg/disk/partition_table.go +++ b/pkg/disk/partition_table.go @@ -1109,7 +1109,7 @@ func mkBIOSBoot(ptType PartitionTableType) (Partition, error) { case PT_GPT: partType = BIOSBootPartitionGUID default: - return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table enum: %d", ptType) + return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table type: %q", string(ptType)) } return Partition{ Size: 1 * datasizes.MiB, @@ -1127,7 +1127,7 @@ func mkESP(size uint64, ptType PartitionTableType) (Partition, error) { case PT_GPT: partType = EFISystemPartitionGUID default: - return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table enum: %d", ptType) + return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table type: %q", string(ptType)) } return Partition{ Size: size, diff --git a/pkg/disk/partition_table_test.go b/pkg/disk/partition_table_test.go index 1f3fcb75bf..f67f0cd7de 100644 --- a/pkg/disk/partition_table_test.go +++ b/pkg/disk/partition_table_test.go @@ -1185,19 +1185,19 @@ func TestAddPartitionsForBootMode(t *testing.T) { }, }, "bad-pttype-bios": { - pt: disk.PartitionTable{Type: disk.PartitionTableType(911)}, + pt: disk.PartitionTable{Type: disk.PartitionTableType("bad")}, bootMode: platform.BOOT_LEGACY, - errmsg: "error creating BIOS boot partition: unknown or unsupported partition table enum: 911", + errmsg: `error creating BIOS boot partition: unknown or unsupported partition table type: "bad"`, }, "bad-pttype-uefi": { - pt: disk.PartitionTable{Type: disk.PartitionTableType(911)}, + pt: disk.PartitionTable{Type: disk.PartitionTableType("bad")}, bootMode: platform.BOOT_UEFI, - errmsg: "error creating EFI system partition: unknown or unsupported partition table enum: 911", + errmsg: `error creating EFI system partition: unknown or unsupported partition table type: "bad"`, }, "bad-pttype-hybrid": { - pt: disk.PartitionTable{Type: disk.PartitionTableType(911)}, + pt: disk.PartitionTable{Type: disk.PartitionTableType("bad")}, bootMode: platform.BOOT_HYBRID, - errmsg: "error creating BIOS boot partition: unknown or unsupported partition table enum: 911", + errmsg: `error creating BIOS boot partition: unknown or unsupported partition table type: "bad"`, }, "bad-bootmode": { pt: disk.PartitionTable{Type: disk.PT_GPT},