mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Move and refactor files and some more changes :3
This commit is contained in:
parent
781e520591
commit
303689ecbb
29 changed files with 1305 additions and 1160 deletions
49
utils/file.go
Normal file
49
utils/file.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FreeFileName(filename string) (string, bool) {
|
||||
ext := filepath.Ext(filename)
|
||||
base := strings.TrimSuffix(filename, ext)
|
||||
// checks if a .tar stands before the "actual" file ending
|
||||
if extraExt := filepath.Ext(base); extraExt == ".tar" {
|
||||
ext = extraExt + ext
|
||||
base = strings.TrimSuffix(base, extraExt)
|
||||
}
|
||||
j := 0
|
||||
for ; ; j++ {
|
||||
if _, stat := os.Stat(filename); stat != nil && !os.IsExist(stat) {
|
||||
break
|
||||
}
|
||||
filename = fmt.Sprintf("%s (%d)%s", base, j+1, ext)
|
||||
}
|
||||
return filename, j != 0
|
||||
}
|
||||
|
||||
func GenerateFilename(name, directory string) string {
|
||||
if runtime.GOOS != "windows" {
|
||||
for _, char := range []string{"/"} {
|
||||
name = strings.ReplaceAll(name, char, "")
|
||||
}
|
||||
Log.Debug("Replaced invalid characters (not windows)")
|
||||
} else {
|
||||
// ahh i love windows :)))
|
||||
for _, char := range []string{"\\", "<", ">", ":", "\"", "/", "|", "?", "*"} {
|
||||
name = strings.ReplaceAll(name, char, "")
|
||||
}
|
||||
Log.Debug("Replaced invalid characters (windows)")
|
||||
}
|
||||
|
||||
filename, changed := FreeFileName(filepath.Join(directory, name))
|
||||
if changed {
|
||||
Log.Debug("File `%s` already exists, changing name to `%s`", filepath.Base(name), filepath.Base(filename))
|
||||
}
|
||||
|
||||
return filename
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue