Skip to content

Commit 6f88da5

Browse files
kzysxibz
authored andcommitted
Define functional options for DrivesBuilder
This would make firecracker-microvm/firecracker-containerd#283 cleaner. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 678f313 commit 6f88da5

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

drives.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,30 @@ func (b DrivesBuilder) AddDrive(path string, readOnly bool, opts ...DriveOpt) Dr
6363
func (b DrivesBuilder) Build() []models.Drive {
6464
return append(b.drives, b.rootDrive)
6565
}
66+
67+
// WithDriveID sets the ID of the drive
68+
func WithDriveID(id string) DriveOpt {
69+
return func(d *models.Drive) {
70+
d.DriveID = String(id)
71+
}
72+
}
73+
74+
// WithReadOnly sets the drive read-only
75+
func WithReadOnly(flag bool) DriveOpt {
76+
return func(d *models.Drive) {
77+
d.IsReadOnly = Bool(flag)
78+
}
79+
}
80+
81+
// WithPartuuid sets the unique ID of the boot partition
82+
func WithPartuuid(uuid string) DriveOpt {
83+
return func(d *models.Drive) {
84+
d.Partuuid = uuid
85+
}
86+
}
87+
// WithRateLimiter sets the rate limitter of the drive
88+
func WithRateLimiter(limiter models.RateLimiter) DriveOpt {
89+
return func(d *models.Drive) {
90+
d.RateLimiter = &limiter
91+
}
92+
}

drives_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ func TestDrivesBuilderWithRootDrive(t *testing.T) {
3636
}
3737

3838
b := NewDrivesBuilder(expectedPath)
39-
drives := b.WithRootDrive(expectedPath, func(drive *models.Drive) {
40-
drive.DriveID = String("foo")
41-
}).Build()
39+
drives := b.WithRootDrive(expectedPath, WithDriveID("foo")).Build()
4240

4341
if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
4442
t.Errorf("expected drives %v, but received %v", e, a)

0 commit comments

Comments
 (0)