owl-blogs/repository.go

58 lines
1.4 KiB
Go
Raw Normal View History

2022-07-20 17:17:42 +00:00
package kiss
import (
"fmt"
"os"
"path"
)
type Repository struct {
name string
}
func CreateRepository(name string) (Repository, error) {
2022-07-20 17:33:22 +00:00
newRepo := Repository{name: name}
2022-07-20 17:17:42 +00:00
// check if repository already exists
2022-07-20 17:33:22 +00:00
if dirExists(newRepo.Dir()) {
2022-07-20 17:17:42 +00:00
return Repository{}, fmt.Errorf("Repository already exists")
}
os.Mkdir(name, 0755)
2022-07-20 17:35:31 +00:00
os.Mkdir(path.Join(name, "users"), 0755)
2022-07-20 17:33:22 +00:00
return newRepo, nil
2022-07-20 17:17:42 +00:00
}
func (repo Repository) Dir() string {
return repo.name
}
2022-07-20 17:35:31 +00:00
func (repo Repository) Users() ([]User, error) {
userNames := listDir(path.Join(repo.Dir(), "users"))
users := make([]User, len(userNames))
for i, name := range userNames {
users[i] = User{repo: repo, name: name}
}
return users, nil
2022-07-20 17:17:42 +00:00
}
2022-07-20 17:35:31 +00:00
func (repo Repository) CreateUser(name string) (User, error) {
2022-07-20 17:17:42 +00:00
new_user := User{repo: repo, name: name}
// check if user already exists
2022-07-20 17:33:22 +00:00
if dirExists(new_user.Dir()) {
2022-07-20 17:17:42 +00:00
return User{}, fmt.Errorf("User already exists")
}
// creates repo/name folder if it doesn't exist
user_dir := new_user.Dir()
os.Mkdir(user_dir, 0755)
os.Mkdir(path.Join(user_dir, "meta"), 0755)
// create public folder
os.Mkdir(path.Join(user_dir, "public"), 0755)
// create Meta files
os.WriteFile(path.Join(user_dir, "meta", "VERSION"), []byte("0.0.1"), 0644)
os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte("<html><body><{{content}}/body></html>"), 0644)
return new_user, nil
}