MCPcopy Create free account
hub / github.com/cli/cli / sanitizeFileName

Function sanitizeFileName

pkg/cmd/release/upload/upload.go:135–157  ·  view source on GitHub ↗

this method attempts to mimic the same functionality on the client that the platform does on uploaded assets in order to allow the --clobber logic work correctly, since that feature is one that only exists in the client

(name string)

Source from the content-addressed store, hash-verified

133// uploaded assets in order to allow the --clobber logic work correctly, since that feature is
134// one that only exists in the client
135func sanitizeFileName(name string) string {
136 value := text.RemoveDiacritics(name)
137 // Stripped all non-ascii characters, provide default name.
138 if strings.HasPrefix(value, ".") {
139 value = "default" + value
140 }
141
142 // Replace special characters with the separator
143 value = regexp.MustCompile(`(?i)[^a-z0-9\-_\+@]+`).ReplaceAllLiteralString(value, ".")
144
145 // No more than one of the separator in a row.
146 value = regexp.MustCompile(`\.{2,}`).ReplaceAllLiteralString(value, ".")
147
148 // Remove leading/trailing separator.
149 value = strings.Trim(value, ".")
150
151 // Just file extension left, add default name.
152 if name != value && !strings.Contains(value, ".") {
153 value = "default." + value
154 }
155
156 return value
157}

Callers 2

uploadRunFunction · 0.85
Test_SanitizeFileNameFunction · 0.85

Calls 2

RemoveDiacriticsFunction · 0.92
ContainsMethod · 0.80

Tested by 1

Test_SanitizeFileNameFunction · 0.68