diff --git a/cmd/owl/main.go b/cmd/owl/main.go index 1ceeda0..e46408a 100644 --- a/cmd/owl/main.go +++ b/cmd/owl/main.go @@ -25,6 +25,7 @@ func Execute() { func init() { rootCmd.PersistentFlags().StringVar(&repoPath, "repo", ".", "Path to the repository to use.") + rootCmd.PersistentFlags().StringVar(&user, "user", "", "Username") } diff --git a/cmd/owl/new_post.go b/cmd/owl/new_post.go new file mode 100644 index 0000000..583bbfc --- /dev/null +++ b/cmd/owl/new_post.go @@ -0,0 +1,50 @@ +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 + } + + _, err = user.CreateNewPost(postTitle) + if err != nil { + println("Error creating post: ", err.Error()) + } else { + println("Post created: ", postTitle) + } + }, +} diff --git a/cmd/owl/new_user.go b/cmd/owl/new_user.go new file mode 100644 index 0000000..d98d8a5 --- /dev/null +++ b/cmd/owl/new_user.go @@ -0,0 +1,38 @@ +package main + +import ( + "h4kor/owl-blogs" + + "github.com/spf13/cobra" +) + +var user string + +func init() { + rootCmd.AddCommand(newUserCmd) +} + +var newUserCmd = &cobra.Command{ + Use: "new-user", + Short: "Creates a new user", + Long: `Creates a new user`, + Run: func(cmd *cobra.Command, args []string) { + if user == "" { + println("Username is required") + return + } + + repo, err := owl.OpenRepository(repoPath) + if err != nil { + println("Error opening repository: ", err.Error()) + return + } + + _, err = repo.CreateUser(user) + if err != nil { + println("Error creating user: ", err.Error()) + } else { + println("User created: ", user) + } + }, +}