Merge pull request #746 from groselt/sanitizeFilename

Sanitize filename before saving output
mine
Fernand Galiana 2020-06-20 08:12:37 -06:00 committed by GitHub
commit e3725817f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
@ -29,6 +30,9 @@ const (
flushTimeout = 50 * time.Millisecond flushTimeout = 50 * time.Millisecond
) )
// InvalidCharsRX contains invalid filename characters.
var invalidPathCharsRX = regexp.MustCompile(`[:/\\]+`)
// Log represents a generic log viewer. // Log represents a generic log viewer.
type Log struct { type Log struct {
*tview.Flex *tview.Flex
@ -284,18 +288,24 @@ func (l *Log) SaveCmd(evt *tcell.EventKey) *tcell.EventKey {
return nil return nil
} }
func sanitizeFilename(name string) string {
processedString := invalidPathCharsRX.ReplaceAllString(name, "-")
return processedString
}
func ensureDir(dir string) error { func ensureDir(dir string) error {
return os.MkdirAll(dir, 0744) return os.MkdirAll(dir, 0744)
} }
func saveData(cluster, name, data string) (string, error) { func saveData(cluster, name, data string) (string, error) {
dir := filepath.Join(config.K9sDumpDir, cluster) dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster))
if err := ensureDir(dir); err != nil { if err := ensureDir(dir); err != nil {
return "", err return "", err
} }
now := time.Now().UnixNano() now := time.Now().UnixNano()
fName := fmt.Sprintf("%s-%d.log", strings.Replace(name, "/", "-", -1), now) fName := fmt.Sprintf("%s-%d.log", sanitizeFilename(name), now)
path := filepath.Join(dir, fName) path := filepath.Join(dir, fName)
mod := os.O_CREATE | os.O_WRONLY mod := os.O_CREATE | os.O_WRONLY

View File

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

View File

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