mirror of
https://github.com/harness/drone.git
synced 2025-05-04 23:40:24 +08:00
Improve sanitize maching name fn (#303)
This commit is contained in:
parent
eaaa825e67
commit
89c135f94b
@ -8,7 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"unicode"
|
||||||
|
|
||||||
"github.com/harness/gitness/events"
|
"github.com/harness/gitness/events"
|
||||||
"github.com/harness/gitness/gitrpc"
|
"github.com/harness/gitness/gitrpc"
|
||||||
@ -17,6 +17,9 @@ import (
|
|||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
|
|
||||||
"github.com/kelseyhightower/envconfig"
|
"github.com/kelseyhightower/envconfig"
|
||||||
|
"golang.org/x/text/runes"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
"golang.org/x/text/unicode/norm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadConfig returns the system configuration from the
|
// LoadConfig returns the system configuration from the
|
||||||
@ -48,19 +51,36 @@ func getSanitizedMachineName() (string, error) {
|
|||||||
// NOTE: this could theoretically lead to overlaps, then it should be passed explicitly
|
// NOTE: this could theoretically lead to overlaps, then it should be passed explicitly
|
||||||
// NOTE: for k8s names/ids below modifications are all noops
|
// NOTE: for k8s names/ids below modifications are all noops
|
||||||
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
|
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
|
||||||
hostName = strings.ToLower(hostName)
|
|
||||||
hostName = strings.Map(func(r rune) rune {
|
// The following code will:
|
||||||
switch {
|
// * remove invalid runes
|
||||||
case 'a' <= r && r <= 'z':
|
// * remove diacritical marks (ie "smörgåsbord" to "smorgasbord")
|
||||||
return r
|
// * lowercase A-Z to a-z
|
||||||
case '0' <= r && r <= '9':
|
// * leave only a-z, 0-9, '-', '.' and replace everything else with '_'
|
||||||
return r
|
hostName, _, err = transform.String(
|
||||||
case r == '-', r == '.':
|
transform.Chain(
|
||||||
return r
|
norm.NFD,
|
||||||
default:
|
runes.ReplaceIllFormed(),
|
||||||
return '_'
|
runes.Remove(runes.In(unicode.Mn)),
|
||||||
}
|
runes.Map(func(r rune) rune {
|
||||||
}, hostName)
|
switch {
|
||||||
|
case 'A' <= r && r <= 'Z':
|
||||||
|
return r + 32
|
||||||
|
case 'a' <= r && r <= 'z':
|
||||||
|
return r
|
||||||
|
case '0' <= r && r <= '9':
|
||||||
|
return r
|
||||||
|
case r == '-', r == '.':
|
||||||
|
return r
|
||||||
|
default:
|
||||||
|
return '_'
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
norm.NFC),
|
||||||
|
hostName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
return hostName, nil
|
return hostName, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user