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

cli: add prefix ID and wildcard namespace support for service info #18836

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/18836.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added support for prefix ID matching and wildcard namespaces to `service info` command
```
32 changes: 31 additions & 1 deletion command/service_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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
}

// 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(args[0], &opts)
serviceInfo, qm, err := client.Services().Get(serviceID, &opts)
if err != nil {
s.Ui.Error(fmt.Sprintf("Error listing service registrations: %s", err))
return 1
Expand Down
10 changes: 5 additions & 5 deletions command/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
}

Expand Down Expand Up @@ -171,13 +171,13 @@ 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, ","))
outputTable = append(outputTable, regOutput)
}
}

s.Ui.Output(formatList(outputTable))
return formatList(outputTable)
}