campaigner/cmd/publish.go

105 lines
2.6 KiB
Go
Raw Normal View History

2020-03-04 22:22:16 +01:00
package cmd
import (
2020-03-05 22:37:01 +01:00
"fmt"
2020-04-29 18:52:15 +01:00
"git.ctrlz.es/mgdelacroix/campaigner/app"
2020-03-05 22:37:01 +01:00
2020-03-04 22:22:16 +01:00
"github.com/spf13/cobra"
)
func JiraPublishCmd() *cobra.Command {
cmd := &cobra.Command{
2020-03-04 22:22:16 +01:00
Use: "jira",
Short: "Publishes the campaign tickets in jira",
2020-03-04 22:22:16 +01:00
Args: cobra.NoArgs,
2020-04-29 18:52:15 +01:00
RunE: withAppE(jiraPublishCmdF),
2020-03-04 22:22:16 +01:00
}
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
cmd.Flags().IntP("batch", "b", 0, "Number of tickets to publish")
cmd.Flags().Bool("dry-run", false, "Print the tickets information instead of publishing them")
return cmd
2020-03-04 22:22:16 +01:00
}
func GithubPublishCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "github",
Short: "Publishes the campaign tickets in github",
Args: cobra.NoArgs,
2020-04-29 18:52:15 +01:00
RunE: withAppE(githubPublishCmdF),
}
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
cmd.Flags().IntP("batch", "b", 0, "Number of tickets to publish")
cmd.Flags().Bool("dry-run", false, "Print the tickets information instead of publishing them")
return cmd
}
2020-03-04 22:22:16 +01:00
func PublishCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "publish",
2020-04-27 11:22:15 +01:00
Short: "Publishes campaign tickets",
Long: "Publishes the campaign tickets in both Jira and Github",
2020-03-04 22:22:16 +01:00
}
cmd.AddCommand(
GithubPublishCmd(),
2020-03-04 22:22:16 +01:00
JiraPublishCmd(),
)
return cmd
}
2020-04-29 18:52:15 +01:00
func jiraPublishCmdF(a *app.App, cmd *cobra.Command, _ []string) error {
2020-03-05 22:37:01 +01:00
all, _ := cmd.Flags().GetBool("all")
batch, _ := cmd.Flags().GetInt("batch")
dryRun, _ := cmd.Flags().GetBool("dry-run")
2020-03-05 22:37:01 +01:00
if !all && batch == 0 {
return fmt.Errorf("One of --all or --batch flags is required")
}
if all {
2020-04-29 18:52:15 +01:00
count, err := a.PublishAllInJira(dryRun)
2020-03-05 22:37:01 +01:00
if err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("All %d tickets successfully published in jira\n", count)
} else {
2020-04-29 18:52:15 +01:00
if err := a.PublishBatchInJira(batch, dryRun); err != nil {
2020-03-05 22:37:01 +01:00
ErrorAndExit(cmd, err)
}
2020-03-05 22:37:01 +01:00
cmd.Printf("Batch of %d tickets successfully published in jira\n", batch)
}
return nil
}
2020-04-29 18:52:15 +01:00
func githubPublishCmdF(a *app.App, cmd *cobra.Command, _ []string) error {
all, _ := cmd.Flags().GetBool("all")
batch, _ := cmd.Flags().GetInt("batch")
dryRun, _ := cmd.Flags().GetBool("dry-run")
if !all && batch == 0 {
return fmt.Errorf("One of --all or --batch flags is required")
}
if all {
2020-04-29 18:52:15 +01:00
count, err := a.PublishAllInGithub(dryRun)
if err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("All %d tickets successfully published in github\n", count)
} else {
2020-04-29 18:52:15 +01:00
if err := a.PublishBatchInGithub(batch, dryRun); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("Batch of %d tickets successfully published in github\n", batch)
}
return nil
}