directories utils

This commit is contained in:
Niko Abeler 2022-07-20 19:33:22 +02:00
parent 142facbfe1
commit 2faa95f753
2 changed files with 13 additions and 4 deletions

8
directories.go Normal file
View File

@ -0,0 +1,8 @@
package kiss
import "os"
func dirExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

View File

@ -22,13 +22,14 @@ type Post struct {
} }
func CreateRepository(name string) (Repository, error) { func CreateRepository(name string) (Repository, error) {
newRepo := Repository{name: name}
// check if repository already exists // check if repository already exists
if _, err := os.Stat(name); err == nil { if dirExists(newRepo.Dir()) {
return Repository{}, fmt.Errorf("Repository already exists") return Repository{}, fmt.Errorf("Repository already exists")
} }
os.Mkdir(name, 0755) os.Mkdir(name, 0755)
return Repository{name: name}, nil return newRepo, nil
} }
func (repo Repository) Dir() string { func (repo Repository) Dir() string {
@ -46,7 +47,7 @@ func PostDir(post Post) string {
func CreateNewUser(repo Repository, name string) (User, error) { func CreateNewUser(repo Repository, name string) (User, error) {
new_user := User{repo: repo, name: name} new_user := User{repo: repo, name: name}
// check if user already exists // check if user already exists
if _, err := os.Stat(new_user.Dir()); err == nil { if dirExists(new_user.Dir()) {
return User{}, fmt.Errorf("User already exists") return User{}, fmt.Errorf("User already exists")
} }
@ -72,7 +73,7 @@ func CreateNewPost(user User, title string) {
// if post already exists, add -n to the end of the name // if post already exists, add -n to the end of the name
i := 0 i := 0
for { for {
if _, err := os.Stat(post_dir); err == nil { if dirExists(post_dir) {
i++ i++
folder_name = fmt.Sprintf("%d-%s-%d", timestamp, title, i) folder_name = fmt.Sprintf("%d-%s-%d", timestamp, title, i)
post_dir = path.Join(user.Dir(), "public", folder_name) post_dir = path.Join(user.Dir(), "public", folder_name)