owl-blogs/cmd/owl-cli/main.go

72 lines
1.6 KiB
Go
Raw Normal View History

2022-07-20 17:17:42 +00:00
package main
import (
2022-08-03 14:55:48 +00:00
"h4kor/owl-blogs"
2022-07-20 17:17:42 +00:00
"os"
)
func main() {
2022-08-03 14:55:48 +00:00
println("owl blogs")
2022-07-20 17:17:42 +00:00
println("Commands")
2022-07-23 07:00:10 +00:00
println("init <repo> - Creates a new repository")
println("<repo> new-user <name> - Creates a new user")
2022-07-23 15:19:47 +00:00
println("<repo> new-post <user> <title> - Creates a new post")
2022-07-20 17:17:42 +00:00
2022-07-23 07:00:10 +00:00
if len(os.Args) < 3 {
println("Please specify a repository and command")
os.Exit(1)
2022-07-20 17:17:42 +00:00
}
2022-07-23 07:00:10 +00:00
if os.Args[1] == "init" {
repoName := os.Args[2]
2022-08-03 14:55:48 +00:00
_, err := owl.CreateRepository(repoName)
2022-07-23 07:00:10 +00:00
if err != nil {
println("Error creating repository: ", err.Error())
2022-07-20 17:17:42 +00:00
}
2022-07-23 07:00:10 +00:00
println("Repository created: ", repoName)
os.Exit(0)
}
repoName := os.Args[1]
2022-08-03 14:55:48 +00:00
repo, err := owl.OpenRepository(repoName)
2022-07-23 07:00:10 +00:00
if err != nil {
println("Error opening repository: ", err.Error())
os.Exit(1)
}
switch os.Args[2] {
case "new-user":
if len(os.Args) < 4 {
println("Please specify a user name")
os.Exit(1)
}
userName := os.Args[3]
user, err := repo.CreateUser(userName)
if err != nil {
println("Error creating user: ", err.Error())
os.Exit(1)
}
println("User created: ", user.Name())
2022-07-23 15:19:47 +00:00
case "new-post":
if len(os.Args) < 5 {
println("Please specify a user name and a title")
os.Exit(1)
}
userName := os.Args[3]
user, err := repo.GetUser(userName)
if err != nil {
println("Error finding user: ", err.Error())
os.Exit(1)
}
title := os.Args[4]
post, err := user.CreateNewPost(title)
if err != nil {
println("Error creating post: ", err.Error())
os.Exit(1)
}
println("Post created: ", post.Title())
2022-07-20 17:17:42 +00:00
default:
2022-07-23 07:00:10 +00:00
println("Unknown command: ", os.Args[2])
os.Exit(1)
2022-07-20 17:17:42 +00:00
}
}