Skip to content

Commit

Permalink
accepts 0B, -1B, 0T, etc for flags for non push commands
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Kocher <mkocher@vmware.com>
  • Loading branch information
Ben Fuller authored and mkocher committed Aug 19, 2022
1 parent d4c3e4b commit 7739f18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion command/flag/bytes_with_unlimited.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flag

import (
"regexp"
"strings"

"code.cloudfoundry.org/bytefmt"
Expand All @@ -10,17 +11,26 @@ import (

type BytesWithUnlimited types.NullInt

var zeroBytes *regexp.Regexp = regexp.MustCompile(`^0[KMGT]?B?$`)
var negativeOneBytes *regexp.Regexp = regexp.MustCompile(`^-1[KMGT]?B?$`)

func (m *BytesWithUnlimited) UnmarshalFlag(val string) error {
if val == "" {
return nil
}

if val == "-1" {
if negativeOneBytes.MatchString(val) {
m.Value = -1
m.IsSet = true
return nil
}

if zeroBytes.MatchString(val) {
m.Value = 0
m.IsSet = true
return nil
}

size, err := ConvertToBytes(val)
if err != nil {
return err
Expand Down
20 changes: 20 additions & 0 deletions command/flag/bytes_with_unlimited_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ var _ = Describe("BytesWithUnlimited", func() {
Expect(err).ToNot(HaveOccurred())
Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(-1))
})

It("accepts units", func() {
err := bytesWithUnlimited.UnmarshalFlag("-1T")
Expect(err).ToNot(HaveOccurred())
Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(-1))
})
})

When("the value is 0", func() {
It("set's the value to 0", func() {
err := bytesWithUnlimited.UnmarshalFlag("0")
Expect(err).ToNot(HaveOccurred())
Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(0))
})

It("accepts units", func() {
err := bytesWithUnlimited.UnmarshalFlag("0TB")
Expect(err).ToNot(HaveOccurred())
Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(0))
})
})

When("the suffix is lowercase", func() {
Expand Down

0 comments on commit 7739f18

Please sign in to comment.