Skip to content

Commit 5282014

Browse files
mariashNick Wei
authored andcommitted
added a custom flag type for the instances flag option in scale
- added the types package - added NullInt type to distinguish between user provided and default values [#148649153] Signed-off-by: Nick Wei <[email protected]>
1 parent 6aa5e04 commit 5282014

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

command/flag/instances.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package flag
2+
3+
import (
4+
"code.cloudfoundry.org/cli/types"
5+
flags "github.com/jessevdk/go-flags"
6+
)
7+
8+
type Instances struct {
9+
types.NullInt
10+
}
11+
12+
func (i *Instances) UnmarshalFlag(val string) error {
13+
err := i.ParseFlagValue(val)
14+
if err != nil || i.Value < 0 {
15+
return &flags.Error{
16+
Type: flags.ErrRequired,
17+
Message: "invalid argument for flag '-i' (expected int > 0)",
18+
}
19+
}
20+
return nil
21+
}

command/flag/instances_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package flag_test
2+
3+
import (
4+
. "code.cloudfoundry.org/cli/command/flag"
5+
"code.cloudfoundry.org/cli/types"
6+
flags "github.com/jessevdk/go-flags"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
)
10+
11+
var _ = Describe("Instances", func() {
12+
var instances Instances
13+
14+
Describe("UnmarshalFlag", func() {
15+
Context("when the empty string is provided", func() {
16+
It("sets IsSet to false", func() {
17+
err := instances.UnmarshalFlag("")
18+
Expect(err).ToNot(HaveOccurred())
19+
Expect(instances).To(Equal(Instances{types.NullInt{Value: 0, IsSet: false}}))
20+
})
21+
})
22+
23+
Context("when an invalid integer is provided", func() {
24+
It("returns an error", func() {
25+
err := instances.UnmarshalFlag("abcdef")
26+
Expect(err).To(MatchError(&flags.Error{
27+
Type: flags.ErrRequired,
28+
Message: "invalid argument for flag '-i' (expected int > 0)",
29+
}))
30+
Expect(instances).To(Equal(Instances{types.NullInt{Value: 0, IsSet: false}}))
31+
})
32+
})
33+
34+
Context("when a negative integer is provided", func() {
35+
It("returns an error", func() {
36+
err := instances.UnmarshalFlag("-10")
37+
Expect(err).To(MatchError(&flags.Error{
38+
Type: flags.ErrRequired,
39+
Message: "invalid argument for flag '-i' (expected int > 0)",
40+
}))
41+
Expect(instances).To(Equal(Instances{types.NullInt{Value: -10, IsSet: true}}))
42+
})
43+
})
44+
45+
Context("when a valid integer is provided", func() {
46+
It("stores the integer and sets IsSet to true", func() {
47+
err := instances.UnmarshalFlag("0")
48+
Expect(err).ToNot(HaveOccurred())
49+
Expect(instances).To(Equal(Instances{types.NullInt{Value: 0, IsSet: true}}))
50+
})
51+
})
52+
})
53+
})

types/null_int.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package types
2+
3+
import "strconv"
4+
5+
type NullInt struct {
6+
Value int
7+
IsSet bool
8+
}
9+
10+
func (n *NullInt) ParseFlagValue(val string) error {
11+
if val == "" {
12+
return nil
13+
}
14+
15+
intVal, err := strconv.Atoi(val)
16+
if err != nil {
17+
return err
18+
}
19+
20+
n.Value = intVal
21+
n.IsSet = true
22+
23+
return nil
24+
}

types/null_int_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package types_test
2+
3+
import (
4+
. "code.cloudfoundry.org/cli/types"
5+
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/gomega"
7+
)
8+
9+
var _ = Describe("NullInt", func() {
10+
var nullInt NullInt
11+
12+
Describe("ParseFlagValue", func() {
13+
Context("when the empty string is provided", func() {
14+
It("sets IsSet to false", func() {
15+
err := nullInt.ParseFlagValue("")
16+
Expect(err).ToNot(HaveOccurred())
17+
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
18+
})
19+
})
20+
21+
Context("when an invalid integer is provided", func() {
22+
It("returns an error", func() {
23+
err := nullInt.ParseFlagValue("abcdef")
24+
Expect(err).To(HaveOccurred())
25+
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
26+
})
27+
})
28+
29+
Context("when a valid integer is provided", func() {
30+
It("stores the integer and sets IsSet to true", func() {
31+
err := nullInt.ParseFlagValue("0")
32+
Expect(err).ToNot(HaveOccurred())
33+
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: true}))
34+
})
35+
})
36+
})
37+
})

types/types_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package types_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"testing"
8+
)
9+
10+
func TestTypes(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Types Suite")
13+
}

0 commit comments

Comments
 (0)