Skip to content

Commit 5c09a4b

Browse files
vuilGitHub Enterprise
authored andcommitted
Add a couple of overridable completion functions (#3)
There are more times that we can tap into providing support for. For now, set up the following types with overridable completion handler functions: - Space, Org, AppName - Binding Name (this more for further experimentation) ai-assisted=no TNZ-87450
1 parent 0b03fb6 commit 5c09a4b

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

command/flag/arguments.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package flag
22

3+
import flags "github.com/jessevdk/go-flags"
4+
5+
// List of overridable completion function for various types
6+
7+
var SpaceCompleteFunc = func(prefix string) []flags.Completion { return nil }
8+
9+
func (Space) Complete(prefix string) []flags.Completion {
10+
return SpaceCompleteFunc(prefix)
11+
}
12+
13+
var OrganizationCompleteFunc = func(prefix string) []flags.Completion { return nil }
14+
15+
func (Organization) Complete(prefix string) []flags.Completion {
16+
return OrganizationCompleteFunc(prefix)
17+
}
18+
19+
var AppNameCompleteFunc = func(prefix string) []flags.Completion { return nil }
20+
21+
func (AppName) Complete(prefix string) []flags.Completion {
22+
return AppNameCompleteFunc(prefix)
23+
}
24+
25+
// --------------------------------------------------------------------------------------------------
26+
327
type AppName struct {
428
AppName string `positional-arg-name:"APP_NAME" required:"true" description:"The application name"`
529
}

command/flag/binding_name.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ type BindingName struct {
66
Value string
77
}
88

9+
var BindingNameCompleteFunc = func(prefix string) []flags.Completion { return nil }
10+
11+
func (BindingName) Complete(prefix string) []flags.Completion {
12+
return BindingNameCompleteFunc(prefix)
13+
}
14+
915
func (b *BindingName) UnmarshalFlag(val string) error {
1016
if val == "" {
1117
return &flags.Error{

command/flag/binding_name_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,47 @@ var _ = Describe("BindingName", func() {
3030
Expect(bindingName.Value).To(Equal("some-name"))
3131
})
3232
})
33+
34+
Describe("Complete", func() {
35+
When("BindingNameCompleteFunc is not set", func() {
36+
It("returns nil", func() {
37+
result := bindingName.Complete("test-")
38+
Expect(result).To(BeNil())
39+
})
40+
})
41+
42+
When("BindingNameCompleteFunc is set", func() {
43+
It("calls BindingNameCompleteFunc with the provided prefix and returns the result", func() {
44+
expectedCompletions := []flags.Completion{
45+
{Item: "binding-1"},
46+
{Item: "binding-2"},
47+
}
48+
BindingNameCompleteFunc = func(prefix string) []flags.Completion {
49+
return expectedCompletions
50+
}
51+
defer func() {
52+
BindingNameCompleteFunc = func(prefix string) []flags.Completion { return nil }
53+
}()
54+
55+
result := bindingName.Complete("binding-")
56+
Expect(result).To(Equal(expectedCompletions))
57+
})
58+
})
59+
60+
When("Complete is called with different prefixes", func() {
61+
It("passes the prefix correctly to BindingNameCompleteFunc", func() {
62+
var capturedPrefix string
63+
BindingNameCompleteFunc = func(prefix string) []flags.Completion {
64+
capturedPrefix = prefix
65+
return nil
66+
}
67+
defer func() {
68+
BindingNameCompleteFunc = func(prefix string) []flags.Completion { return nil }
69+
}()
70+
71+
bindingName.Complete("my-prefix")
72+
Expect(capturedPrefix).To(Equal("my-prefix"))
73+
})
74+
})
75+
})
3376
})

0 commit comments

Comments
 (0)