Make sanitizeFilename private

mine
groselt 2020-05-30 18:17:05 +01:00
parent 7d2eb83f15
commit 6e5afd6d56
4 changed files with 7 additions and 25 deletions

View File

@ -288,8 +288,7 @@ func (l *Log) SaveCmd(evt *tcell.EventKey) *tcell.EventKey {
return nil
}
// SanitizeFilename removes characters not allowed by OS
func SanitizeFilename(name string) string {
func sanitizeFilename(name string) string {
processedString := invalidPathCharsRX.ReplaceAllString(name, "-")
return processedString
@ -300,13 +299,13 @@ func ensureDir(dir string) error {
}
func saveData(cluster, name, data string) (string, error) {
dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster))
dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster))
if err := ensureDir(dir); err != nil {
return "", err
}
now := time.Now().UnixNano()
fName := fmt.Sprintf("%s-%d.log", SanitizeFilename(name), now)
fName := fmt.Sprintf("%s-%d.log", sanitizeFilename(name), now)
path := filepath.Join(dir, fName)
mod := os.O_CREATE | os.O_WRONLY

View File

@ -43,23 +43,6 @@ func TestLogViewSave(t *testing.T) {
assert.Equal(t, len(c2), len(c1)+1)
}
func TestSanitizedFilename(t *testing.T) {
uu := []struct {
name string
expected string
}{
{"alpha", "alpha"},
{"123", "123"},
{"with/slash", "with-slash"},
{"with:colon", "with-colon"},
{":many:invalid\\characters\\", "-many-invalid-characters-"},
}
for _, u := range uu {
assert.Equal(t, u.expected, view.SanitizeFilename(u.name))
}
}
// ----------------------------------------------------------------------------
// Helpers...

View File

@ -18,12 +18,12 @@ import (
func computeFilename(cluster, ns, title, path string) (string, error) {
now := time.Now().UnixNano()
dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster))
dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster))
if err := ensureDir(dir); err != nil {
return "", err
}
name := title + "-" + SanitizeFilename(path)
name := title + "-" + sanitizeFilename(path)
if path == "" {
name = title
}

View File

@ -61,13 +61,13 @@ func enableRegion(str string) string {
}
func saveYAML(cluster, name, data string) (string, error) {
dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster))
dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster))
if err := ensureDir(dir); err != nil {
return "", err
}
now := time.Now().UnixNano()
fName := fmt.Sprintf("%s-%d.yml", SanitizeFilename(name), now)
fName := fmt.Sprintf("%s-%d.yml", sanitizeFilename(name), now)
path := filepath.Join(dir, fName)
mod := os.O_CREATE | os.O_WRONLY