Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding completion for remaining Eventing resources #1567

Merged
merged 15 commits into from
Jan 18, 2022
Merged
1 change: 1 addition & 0 deletions pkg/kn/commands/channel/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewChannelDeleteCommand(p *commands.KnParams) *cobra.Command {
Example: `
# Delete a channel 'pipe'
kn channel delete pipe`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn channel delete' requires the channel name as single argument")
Expand Down
7 changes: 4 additions & 3 deletions pkg/kn/commands/channel/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func NewChannelDescribeCommand(p *commands.KnParams) *cobra.Command {
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

cmd := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a channel",
Example: describeExample,
Use: "describe NAME",
Short: "Show details of a channel",
Example: describeExample,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn channel describe' requires the channel name given as single argument")
Expand Down
206 changes: 201 additions & 5 deletions pkg/kn/commands/completion_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ type completionConfig struct {

var (
resourceToFuncMap = map[string]func(config *completionConfig) []string{
"broker": completeBroker,
"domain": completeDomain,
"revision": completeRevision,
"route": completeRoute,
"service": completeService,
"apiserver": completeApiserverSource,
"binding": completeBindingSource,
"broker": completeBroker,
"channel": completeChannel,
"container": completeContainerSource,
"domain": completeDomain,
"ping": completePingSource,
"revision": completeRevision,
"route": completeRoute,
"service": completeService,
"subscription": completeSubscription,
"trigger": completeTrigger,
}
)

Expand Down Expand Up @@ -231,3 +238,192 @@ func completeDomain(config *completionConfig) (suggestions []string) {
}
return
}

func completeTrigger(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}
client, err := config.params.NewEventingClient(namespace)
if err != nil {
return
}
triggerList, err := client.ListTriggers(config.command.Context())
if err != nil {
return
}
for _, sug := range triggerList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completeContainerSource(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}
client, err := config.params.NewSourcesClient(namespace)
if err != nil {
return
}
containerSourceList, err := client.ContainerSourcesClient().ListContainerSources(config.command.Context())
if err != nil {
return
}
for _, sug := range containerSourceList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completeApiserverSource(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}
client, err := config.params.NewSourcesClient(namespace)
if err != nil {
return
}
apiServerSourceList, err := client.APIServerSourcesClient().ListAPIServerSource(config.command.Context())
if err != nil {
return
}
for _, sug := range apiServerSourceList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completeBindingSource(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}
client, err := config.params.NewSourcesClient(namespace)
if err != nil {
return
}
bindingList, err := client.SinkBindingClient().ListSinkBindings(config.command.Context())
if err != nil {
return
}
for _, sug := range bindingList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completePingSource(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}

client, err := config.params.NewSourcesV1beta2Client(namespace)
if err != nil {
return
}
pingSourcesClient := client.PingSourcesClient()

pingSourceList, err := pingSourcesClient.ListPingSource(config.command.Context())
if err != nil {
return
}
for _, sug := range pingSourceList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completeChannel(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}

client, err := config.params.NewMessagingClient(namespace)
if err != nil {
return
}

channelList, err := client.ChannelsClient().ListChannel(config.command.Context())
if err != nil {
return
}
for _, sug := range channelList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completeSubscription(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}

client, err := config.params.NewMessagingClient(namespace)
if err != nil {
return
}

subscriptionList, err := client.SubscriptionsClient().ListSubscription(config.command.Context())
if err != nil {
return
}
for _, sug := range subscriptionList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}
Loading