owl-blogs/cmd/owl/new_post.go

52 lines
992 B
Go
Raw Permalink Normal View History

2022-09-05 19:12:54 +00:00
package main
import (
"h4kor/owl-blogs"
"github.com/spf13/cobra"
)
var postTitle string
func init() {
rootCmd.AddCommand(newPostCmd)
newPostCmd.PersistentFlags().StringVar(&postTitle, "title", "", "Post title")
}
var newPostCmd = &cobra.Command{
Use: "new-post",
Short: "Creates a new post",
Long: `Creates a new post`,
Run: func(cmd *cobra.Command, args []string) {
if user == "" {
println("Username is required")
return
}
if postTitle == "" {
println("Post title is required")
return
}
repo, err := owl.OpenRepository(repoPath)
if err != nil {
println("Error opening repository: ", err.Error())
return
}
user, err := repo.GetUser(user)
if err != nil {
println("Error getting user: ", err.Error())
return
}
2022-10-13 18:33:00 +00:00
post, err := user.CreateNewPost(postTitle, true)
2022-09-05 19:12:54 +00:00
if err != nil {
println("Error creating post: ", err.Error())
} else {
println("Post created: ", postTitle)
2022-10-13 18:33:00 +00:00
println("Edit: ", post.ContentFile())
2022-09-05 19:12:54 +00:00
}
},
}