/
firmware.go
41 lines (36 loc) · 1.22 KB
/
firmware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package completion
import (
"context"
"fmt"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
// WithFirmware firmware (managedObject) completion
func WithFirmware(flagName string, clientFunc func() (*c8y.Client, error)) Option {
return func(cmd *cobra.Command) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
client, err := clientFunc()
if err != nil {
return []string{err.Error()}, cobra.ShellCompDirectiveDefault
}
pattern := "*" + toComplete + "*"
items, _, err := client.Firmware.GetFirmwareByName(
c8y.WithDisabledDryRunContext(context.Background()),
pattern,
c8y.NewPaginationOptions(100),
)
if err != nil {
values := []string{fmt.Sprintf("error. %s", err)}
return values, cobra.ShellCompDirectiveError
}
values := []string{}
for _, item := range items.ManagedObjects {
if toComplete == "" || MatchString(pattern, item.Name) || MatchString(pattern, item.ID) {
values = append(values, fmt.Sprintf("%s\t%s | id: %s", item.Name, item.Type, item.ID))
}
}
return values, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
}