getSanitizedMachineName gets the name of the machine and returns it in sanitized format.
()
| 222 | |
| 223 | // getSanitizedMachineName gets the name of the machine and returns it in sanitized format. |
| 224 | func getSanitizedMachineName() (string, error) { |
| 225 | // use the hostname as default id of the instance |
| 226 | hostName, err := os.Hostname() |
| 227 | if err != nil { |
| 228 | return "", err |
| 229 | } |
| 230 | |
| 231 | // Always cast to lower and remove all unwanted chars |
| 232 | // NOTE: this could theoretically lead to overlaps, then it should be passed explicitly |
| 233 | // NOTE: for k8s names/ids below modifications are all noops |
| 234 | // https://kubernetes.io/docs/concepts/overview/working-with-objects/names/ |
| 235 | |
| 236 | // The following code will: |
| 237 | // * remove invalid runes |
| 238 | // * remove diacritical marks (ie "smörgåsbord" to "smorgasbord") |
| 239 | // * lowercase A-Z to a-z |
| 240 | // * leave only a-z, 0-9, '-', '.' and replace everything else with '_' |
| 241 | hostName, _, err = transform.String( |
| 242 | transform.Chain( |
| 243 | norm.NFD, |
| 244 | runes.ReplaceIllFormed(), |
| 245 | runes.Remove(runes.In(unicode.Mn)), |
| 246 | runes.Map( |
| 247 | func(r rune) rune { |
| 248 | switch { |
| 249 | case 'A' <= r && r <= 'Z': |
| 250 | return r + 32 |
| 251 | case 'a' <= r && r <= 'z': |
| 252 | return r |
| 253 | case '0' <= r && r <= '9': |
| 254 | return r |
| 255 | case r == '-', r == '.': |
| 256 | return r |
| 257 | default: |
| 258 | return '_' |
| 259 | } |
| 260 | }, |
| 261 | ), |
| 262 | norm.NFC, |
| 263 | ), |
| 264 | hostName, |
| 265 | ) |
| 266 | if err != nil { |
| 267 | return "", err |
| 268 | } |
| 269 | |
| 270 | return hostName, nil |
| 271 | } |
| 272 | |
| 273 | // ProvideDatabaseConfig loads the database config from the main config. |
| 274 | func ProvideDatabaseConfig(config *types.Config) database.Config { |
no test coverage detected
searching dependent graphs…