-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Main file is no longer generated automatically - instead, we generate a top-level module package which can be imported into a "unified" CLI tool, if so desired.
- Loading branch information
Showing
20 changed files
with
629 additions
and
526 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package aipcli | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/stoewer/go-strcase" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
func getServiceCommandUse( | ||
servicesByName map[protoreflect.Name][]protoreflect.FullName, | ||
service protoreflect.FullName, | ||
) string { | ||
var serviceName string | ||
if services := servicesByName[service.Name()]; len(services) > 1 { | ||
serviceName = strings.TrimSuffix(string(service), "Service") | ||
serviceName = strings.TrimPrefix(serviceName, string(longestCommonParent(services))) | ||
serviceName = strings.TrimPrefix(serviceName, ".") | ||
serviceName = strings.Join(reverse(strings.Split(serviceName, ".")), "-") | ||
} else { | ||
serviceName = strings.TrimSuffix(string(service.Name()), "Service") | ||
} | ||
return strcase.KebabCase(serviceName) | ||
} | ||
|
||
// longestCommonParent returns the longest common parent of multiple services. | ||
func longestCommonParent(fullNames []protoreflect.FullName) protoreflect.FullName { | ||
if len(fullNames) == 0 { | ||
return "" | ||
} | ||
var i int | ||
var result protoreflect.FullName | ||
ResultLoop: | ||
for { | ||
parts := strings.Split(string(fullNames[0]), ".") | ||
if i > len(parts) { | ||
break | ||
} | ||
candidate := strings.Join(parts[:i], ".") | ||
for _, fullName := range fullNames { | ||
if !strings.HasPrefix(string(fullName), candidate) { | ||
break ResultLoop | ||
} | ||
} | ||
result = protoreflect.FullName(candidate) | ||
i++ | ||
} | ||
return result | ||
} | ||
|
||
func reverse(ss []string) []string { | ||
last := len(ss) - 1 | ||
for i := 0; i < len(ss)/2; i++ { | ||
ss[i], ss[last-i] = ss[last-i], ss[i] | ||
} | ||
return ss | ||
} |
Oops, something went wrong.