diff --git a/.changelog/18836.txt b/.changelog/18836.txt new file mode 100644 index 000000000..b7c906f5f --- /dev/null +++ b/.changelog/18836.txt @@ -0,0 +1,3 @@ +```release-note:improvement +cli: Added support for prefix ID matching and wildcard namespaces to `service info` command +``` diff --git a/command/service_info.go b/command/service_info.go index 3005e43c1..c4880dc80 100644 --- a/command/service_info.go +++ b/command/service_info.go @@ -114,14 +114,44 @@ func (s *ServiceInfoCommand) Run(args []string) int { return 1 } + ns := s.Meta.namespace + serviceID := args[0] + // Set up the options to capture any filter passed. opts := api.QueryOptions{ Filter: filter, - PerPage: int32(perPage), - NextToken: pageToken, + Prefix: serviceID, + Namespace: ns, + } + services, _, err := client.Services().List(&opts) + if err != nil { + s.Ui.Error(fmt.Sprintf("Error listing service registrations: %s", err)) + return 1 + } + switch len(services) { + case 0: + s.Ui.Error(fmt.Sprintf("No service registrations with prefix %q found", serviceID)) + return 1 + case 1: + ns = services[0].Namespace + if len(services[0].Services) > 0 { // should always be valid + serviceID = services[0].Services[0].ServiceName + } + default: + s.Ui.Error(fmt.Sprintf("Prefix matched multiple services\n\n%s", + formatServiceListOutput(s.Meta.namespace, services))) + return 1 } - serviceInfo, qm, err := client.Services().Get(args[0], &opts) + // Set up the options to capture any filter passed. + opts = api.QueryOptions{ + Filter: filter, + PerPage: int32(perPage), + NextToken: pageToken, + Namespace: ns, + } + + serviceInfo, qm, err := client.Services().Get(serviceID, &opts) if err != nil { s.Ui.Error(fmt.Sprintf("Error listing service registrations: %s", err)) return 1 diff --git a/command/service_list.go b/command/service_list.go index b13b67b9b..9c3cb7ed5 100644 --- a/command/service_list.go +++ b/command/service_list.go @@ -113,11 +113,11 @@ func (s *ServiceListCommand) Run(args []string) int { return 0 } - s.formatOutput(list) + s.Ui.Output(formatServiceListOutput(s.Meta.namespace, list)) return 0 } -func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStub) { +func formatServiceListOutput(cmdNS string, regs []*api.ServiceRegistrationListStub) string { // Create objects to hold sorted a sorted namespace array and a mapping, so // we can perform service lookups on a namespace basis. @@ -137,7 +137,7 @@ func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStu // If the request was made using the wildcard namespace, include this in // the output. - if s.Meta.namespace == api.AllNamespacesNamespace { + if cmdNS == api.AllNamespacesNamespace { outputTable[0] += "|Namespace" } @@ -171,7 +171,7 @@ func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStu // Build the output array entry. regOutput := serviceName - if s.Meta.namespace == api.AllNamespacesNamespace { + if cmdNS == api.AllNamespacesNamespace { regOutput += "|" + ns } regOutput += "|" + fmt.Sprintf("[%s]", strings.Join(tags, ",")) @@ -179,5 +179,5 @@ func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStu } } - s.Ui.Output(formatList(outputTable)) + return formatList(outputTable) }