package cmd import ( "bytes" "context" "flag" "fmt" "os" "strings" "go.jolheiser.com/git-ea/blog" "github.com/AlecAivazis/survey/v2" "github.com/peterbourgon/ff/v3/ffcli" ) func (h *Handler) Post() *ffcli.Command { fs := flag.NewFlagSet("post", flag.ContinueOnError) authorFlag := fs.String("author", "", "Post author") fs.StringVar(authorFlag, "a", *authorFlag, "--author") milestoneFlag := fs.String("milestone", "", "Post milestone") fs.StringVar(milestoneFlag, "m", *milestoneFlag, "--milestone") changelogFlag := fs.String("changelog", "", "Post changelog (no header)") fs.StringVar(changelogFlag, "c", *changelogFlag, "--changelog") outputFlag := fs.String("output", "content/post/release-of-${milestone}.md", "Output file") fs.StringVar(outputFlag, "o", *outputFlag, "--output") return &ffcli.Command{ Name: "post", FlagSet: fs, ShortUsage: "post", ShortHelp: "post creates a new blog release post", Exec: func(ctx context.Context, args []string) error { author := *authorFlag if author == "" { if err := survey.AskOne(&survey.Input{ Message: "Blog post author", Default: currentUser(), }, &author, survey.WithValidator(survey.Required)); err != nil { return err } } milestone := *milestoneFlag if milestone == "" { var defMilestone blog.Release r, err := blog.LatestRelease() if err == nil { defMilestone = r } if err := survey.AskOne(&survey.Input{ Message: "Release milestone", Default: strings.TrimPrefix(defMilestone.Name, "v"), Help: fmt.Sprintf("Default was published on %s", defMilestone.PublishedAt.Format("01/02/2006")), }, &milestone, survey.WithValidator(survey.Required)); err != nil { return err } } changelog := *changelogFlag if changelog == "" { if err := blog.OpenChangelogPullRequest(milestone); err != nil { fmt.Println(err) } if err := survey.AskOne(&survey.Editor{ Message: "Blog post changelog", FileName: "*.md", }, &changelog, survey.WithValidator(survey.Required)); err != nil { return err } } post, err := blog.FormatRelease(author, milestone, changelog) if err != nil { return err } complete, err := blog.FormatPR(bytes.NewReader(post)) if err != nil { return err } output := os.Expand(*outputFlag, func(s string) string { switch s { case "milestone": return milestone default: return s } }) fi, err := os.Create(output) if err != nil { return err } defer fi.Close() if _, err := fi.Write(complete); err != nil { fmt.Println(err) } fmt.Printf("Blog post created at %q\n", output) return nil }, } }