Rename backups => snapshot

This commit is contained in:
Tom Wiesing 2022-09-07 11:02:50 +02:00
parent 611cbeebb9
commit d818cb93a5
No known key found for this signature in database
8 changed files with 178 additions and 166 deletions

View file

@ -2,6 +2,11 @@ package fsx
import "os"
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func IsDirectory(path string) bool {
info, err := os.Stat(path)
return err == nil && info.Mode().IsDir()

View file

@ -10,28 +10,33 @@ import (
// NOTE(twiesing): A bunch of scripts cannot properly handle the extra characters in the password.
// For now it is disabled, but it should be re-enabled later.
const PasswordCharSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // + "!@#$%&*"
const PasswordCharCount = len(PasswordCharSet)
var passwordCharCount = big.NewInt(int64(len(PasswordCharSet)))
// Password returns a randomly generated password with the provided length.
// Password returns a randomly generated string with the provided length.
// It consists of alphanumeric characters only.
//
// When an error occurs, it is guaranteed to return "", err.
// [rand.Reader] is used as the source of randomness.
func Password(length int) (string, error) {
if length < 0 {
panic("length < 0")
}
// create a buffer to write the string to!
var password strings.Builder
password.Grow(length)
for i := 0; i < length; i++ {
// grab a random index!
index, err := rand.Int(rand.Reader, big.NewInt(int64(PasswordCharCount)))
// grab a random bIndex!
bIndex, err := rand.Int(rand.Reader, passwordCharCount)
if err != nil {
return "", err
}
// and use that index!
if err := password.WriteByte(PasswordCharSet[int(index.Int64())]); err != nil {
index := int(bIndex.Int64())
if err := password.WriteByte(PasswordCharSet[index]); err != nil {
return "", err
}
}