-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at functional impl for source_tag
- Loading branch information
Showing
3 changed files
with
72 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func BuildUserAgent(sourceTag string) string { | ||
return buildUserAgent("go-client", sourceTag) | ||
} | ||
|
||
func BuildUserAgentGRPC(sourceTag string) string { | ||
return buildUserAgent("go-client[grpc]", sourceTag) | ||
} | ||
|
||
func buildUserAgent(appName string, sourceTag string) string { | ||
// need to set to actual current version | ||
appVersion := "0.0.1" | ||
|
||
sourceTagInfo := "" | ||
if sourceTag != "" { | ||
sourceTagInfo = buildSourceTagField(sourceTag) | ||
} | ||
userAgent := fmt.Sprintf("%s/%s%s", appName, appVersion, sourceTagInfo) | ||
return userAgent | ||
} | ||
|
||
func buildSourceTagField(userAgent string) string { | ||
// Lowercase | ||
userAgent = strings.ToLower(userAgent) | ||
|
||
// Limit charset to [a-z0-9_ ] | ||
re := regexp.MustCompile(`[^a-z0-9_ ]`) | ||
userAgent = re.ReplaceAllString(userAgent, "") | ||
|
||
// Trim left/right whitespace | ||
userAgent = strings.TrimSpace(userAgent) | ||
|
||
// Condense multiple spaces to one, and replace with underscore | ||
userAgent = strings.Join(strings.Fields(userAgent), "_") | ||
|
||
return fmt.Sprintf("; source_tag=%s;", userAgent) | ||
} |
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