`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)
| 1077 | // return value will be `true` if it succeeded and `false` if it had |
| 1078 | // to fallback to the friendly name. |
| 1079 | func 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. |