MCPcopy Index your code
hub / github.com/coder/coder / safeAgentName

Function safeAgentName

agent/agentcontainers/api.go:1079–1105  ·  view source on GitHub ↗

`safeAgentName` returns a safe agent name derived from a folder name, falling back to the container’s friendly name if needed. The second return value will be `true` if it succeeded and `false` if it had to fallback to the friendly name.

(name string, friendlyName string)

Source from the content-addressed store, hash-verified

1077// return value will be `true` if it succeeded and `false` if it had
1078// to fallback to the friendly name.
1079func safeAgentName(name string, friendlyName string) (string, bool) {
1080 // Keep only ASCII letters and digits, replacing everything
1081 // else with a hyphen.
1082 var sb strings.Builder
1083 for _, r := range strings.ToLower(name) {
1084 if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
1085 _, _ = sb.WriteRune(r)
1086 } else {
1087 _, _ = sb.WriteRune('-')
1088 }
1089 }
1090
1091 // Remove any consecutive hyphens, and then trim any leading
1092 // and trailing hyphens.
1093 name = consecutiveHyphenRegex.ReplaceAllString(sb.String(), "-")
1094 name = strings.Trim(name, "-")
1095
1096 // Ensure the name of the agent doesn't exceed the maximum agent
1097 // name length.
1098 name = name[:min(len(name), maxAgentNameLength)]
1099
1100 if provisioner.AgentNameRegex.Match([]byte(name)) {
1101 return name, true
1102 }
1103
1104 return safeFriendlyName(friendlyName), false
1105}
1106
1107// safeFriendlyName returns a API safe version of the container's
1108// friendly name.

Callers 2

expandedAgentNameFunction · 0.85
TestSafeAgentNameFunction · 0.85

Calls 3

safeFriendlyNameFunction · 0.85
MatchMethod · 0.80
StringMethod · 0.45

Tested by 1

TestSafeAgentNameFunction · 0.68