init import

This commit is contained in:
Олег Бородин
2026-05-24 11:02:51 +02:00
commit 25365aef77
154 changed files with 21501 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package auxtool
import (
"os"
"path/filepath"
"strings"
)
// Clean only overbase elements of dir path if possible
func CleanDirs(basedir, datadir string) {
separator := string(os.PathSeparator)
basedir = filepath.Clean(separator + basedir)
datadir = filepath.Clean(separator + datadir)
items := strings.Split(datadir, separator)
for i := len(items); i > 0; i-- {
p := filepath.Join(items[0:i]...)
p = filepath.Clean(separator + p)
if p == basedir {
break
}
fileInfo, err := os.Stat(p)
if err != nil {
return
}
if fileInfo.IsDir() {
err = os.Remove(p)
if err != nil {
return
}
}
}
}
+31
View File
@@ -0,0 +1,31 @@
package auxtool
import (
"os"
)
func FileExists(name string) bool {
fileStat, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
if fileStat.IsDir() {
return false
}
return true
}
func DirExists(name string) bool {
fileStat, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
if fileStat == nil {
return false
}
return fileStat.IsDir()
}
+22
View File
@@ -0,0 +1,22 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
*
*/
/*
*/
package auxtool
import (
"math/rand"
)
func RandomString(n int) string {
const letters = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
arr := make([]byte, n)
for i := range arr {
arr[i] = letters[rand.Intn(len(letters))]
}
return string(arr)
}
+22
View File
@@ -0,0 +1,22 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package auxtool
import (
"encoding/hex"
"fmt"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func MakeTmpFilename(prefix string) string {
randBytes := make([]byte, 6)
rand.Read(randBytes)
suffix := hex.EncodeToString(randBytes)
return fmt.Sprintf("%s.tmp.%s", prefix, suffix)
}
+13
View File
@@ -0,0 +1,13 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package auxtool
import (
"time"
)
func TimeNow() string {
return time.Now().Format(time.RFC3339)
}