Add the app struct

This commit is contained in:
Miguel de la Cruz 2020-04-29 19:52:15 +02:00
parent 8732aee990
commit 114f73f9f5
11 changed files with 208 additions and 198 deletions

View file

@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/app"
"git.ctrlz.es/mgdelacroix/campaigner/model"
"git.ctrlz.es/mgdelacroix/campaigner/parsers"
)
@ -25,7 +25,7 @@ func GrepAddCmd() *cobra.Command {
`,
Example: ` grep -nriIF --include \*.go cobra.Command | campaigner add grep`,
Args: cobra.NoArgs,
Run: grepAddCmdF,
Run: withApp(grepAddCmdF),
}
cmd.Flags().BoolP("file-only", "f", false, "Generates one ticket per file instead of per match")
@ -60,7 +60,7 @@ func GovetAddCmd() *cobra.Command {
`,
Example: ` govet ./... 2>&1 | campaigner add govet`,
Args: cobra.NoArgs,
Run: govetAddCmdF,
Run: withApp(govetAddCmdF),
}
cmd.Flags().BoolP("file-only", "f", false, "Generates one ticket per file instead of per match")
@ -74,7 +74,7 @@ func CsvAddCmd() *cobra.Command {
Short: "Generates the tickets reading a csv file",
Example: ` campaigner add csv tickets.csv`,
Args: cobra.ExactArgs(1),
Run: csvAddCmdF,
Run: withApp(csvAddCmdF),
}
}
@ -95,20 +95,13 @@ func AddCmd() *cobra.Command {
return cmd
}
func grepAddCmdF(cmd *cobra.Command, _ []string) {
func grepAddCmdF(a *app.App, cmd *cobra.Command, _ []string) {
fileOnly, _ := cmd.Flags().GetBool("file-only")
tickets := parsers.ParseWith(parsers.GREP)
a.Campaign.AddTickets(tickets, fileOnly)
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
cmp.Tickets = append(cmp.Tickets, tickets...)
cmp.Tickets = model.RemoveDuplicateTickets(cmp.Tickets, fileOnly)
if err := campaign.Save(cmp); err != nil {
if err := a.Save(); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("%d tickets have been added\n", len(tickets))
@ -118,36 +111,24 @@ func agAddCmdF(_ *cobra.Command, _ []string) error {
return fmt.Errorf("not implemented yet")
}
func govetAddCmdF(cmd *cobra.Command, _ []string) {
func govetAddCmdF(a *app.App, cmd *cobra.Command, _ []string) {
fileOnly, _ := cmd.Flags().GetBool("file-only")
tickets := parsers.ParseWith(parsers.GOVET)
a.Campaign.AddTickets(tickets, fileOnly)
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
cmp.Tickets = append(cmp.Tickets, tickets...)
cmp.Tickets = model.RemoveDuplicateTickets(cmp.Tickets, fileOnly)
if err := campaign.Save(cmp); err != nil {
if err := a.Save(); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("%d tickets have been added\n", len(tickets))
}
func csvAddCmdF(cmd *cobra.Command, args []string) {
func csvAddCmdF(a *app.App, cmd *cobra.Command, args []string) {
file, err := os.Open(args[0])
if err != nil {
ErrorAndExit(cmd, err)
}
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
csvReader := csv.NewReader(bufio.NewReader(file))
records, err := csvReader.ReadAll()
if err != nil {
@ -160,10 +141,10 @@ func csvAddCmdF(cmd *cobra.Command, args []string) {
for i, header := range headers {
data[header] = line[i]
}
cmp.Tickets = append(cmp.Tickets, &model.Ticket{Data: data})
a.Campaign.Tickets = append(a.Campaign.Tickets, &model.Ticket{Data: data})
}
if err := campaign.Save(cmp); err != nil {
if err := a.Save(); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("%d tickets have been added\n", len(records[1:]))

View file

@ -6,7 +6,7 @@ import (
"os"
"strings"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/app"
"git.ctrlz.es/mgdelacroix/campaigner/model"
"github.com/spf13/cobra"
@ -78,7 +78,7 @@ func initCmdF(cmd *cobra.Command, _ []string) {
project := strings.Split(epic, "-")[0]
cmp := &model.Campaign{
campaign := &model.Campaign{
Jira: model.ConfigJira{
Url: url,
Username: jiraUsername,
@ -96,7 +96,7 @@ func initCmdF(cmd *cobra.Command, _ []string) {
IssueTemplate: issueTemplate,
FooterTemplate: footerTemplate,
}
if err := campaign.Save(cmp); err != nil {
if err := app.SaveCampaign(campaign, "./campaign.json"); err != nil {
ErrorAndExit(cmd, err)
}
}

View file

@ -3,9 +3,7 @@ package cmd
import (
"fmt"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/github"
"git.ctrlz.es/mgdelacroix/campaigner/jira"
"git.ctrlz.es/mgdelacroix/campaigner/app"
"github.com/spf13/cobra"
)
@ -15,7 +13,7 @@ func JiraPublishCmd() *cobra.Command {
Use: "jira",
Short: "Publishes the campaign tickets in jira",
Args: cobra.NoArgs,
RunE: jiraPublishCmdF,
RunE: withAppE(jiraPublishCmdF),
}
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
@ -30,7 +28,7 @@ func GithubPublishCmd() *cobra.Command {
Use: "github",
Short: "Publishes the campaign tickets in github",
Args: cobra.NoArgs,
RunE: githubPublishCmdF,
RunE: withAppE(githubPublishCmdF),
}
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
@ -55,7 +53,7 @@ func PublishCmd() *cobra.Command {
return cmd
}
func jiraPublishCmdF(cmd *cobra.Command, _ []string) error {
func jiraPublishCmdF(a *app.App, cmd *cobra.Command, _ []string) error {
all, _ := cmd.Flags().GetBool("all")
batch, _ := cmd.Flags().GetInt("batch")
dryRun, _ := cmd.Flags().GetBool("dry-run")
@ -64,24 +62,14 @@ func jiraPublishCmdF(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("One of --all or --batch flags is required")
}
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
jiraClient, err := jira.NewClient(cmp.Jira.Url, cmp.Jira.Username, cmp.Jira.Token)
if err != nil {
ErrorAndExit(cmd, err)
}
if all {
count, err := jiraClient.PublishAll(cmp, dryRun)
count, err := a.PublishAllInJira(dryRun)
if err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("All %d tickets successfully published in jira\n", count)
} else {
if err := jiraClient.PublishBatch(cmp, batch, dryRun); err != nil {
if err := a.PublishBatchInJira(batch, dryRun); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("Batch of %d tickets successfully published in jira\n", batch)
@ -90,7 +78,7 @@ func jiraPublishCmdF(cmd *cobra.Command, _ []string) error {
return nil
}
func githubPublishCmdF(cmd *cobra.Command, _ []string) error {
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")
@ -99,21 +87,14 @@ func githubPublishCmdF(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("One of --all or --batch flags is required")
}
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
githubClient := github.NewClient(cmp.Github.Repo, cmp.Github.Token)
if all {
count, err := githubClient.PublishAll(cmp, dryRun)
count, err := a.PublishAllInGithub(dryRun)
if err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("All %d tickets successfully published in github\n", count)
} else {
if err := githubClient.PublishBatch(cmp, batch, dryRun); err != nil {
if err := a.PublishBatchInGithub(batch, dryRun); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("Batch of %d tickets successfully published in github\n", batch)

View file

@ -5,8 +5,34 @@ import (
"os"
"github.com/spf13/cobra"
"git.ctrlz.es/mgdelacroix/campaigner/app"
)
func withApp(f func(*app.App, *cobra.Command, []string)) func(*cobra.Command, []string) {
a, err := app.NewApp("./campaign.json")
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
os.Exit(1)
}
return func(cmd *cobra.Command, args []string) {
f(a, cmd, args)
}
}
func withAppE(f func(*app.App, *cobra.Command, []string) error) func(*cobra.Command, []string) error {
a, err := app.NewApp("./campaign.json")
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
os.Exit(1)
}
return func(cmd *cobra.Command, args []string) error {
return f(a, cmd, args)
}
}
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "campaigner",

View file

@ -1,9 +1,9 @@
package cmd
import (
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"github.com/spf13/cobra"
"git.ctrlz.es/mgdelacroix/campaigner/app"
)
func StatusCmd() *cobra.Command {
@ -12,15 +12,10 @@ func StatusCmd() *cobra.Command {
Short: "Prints the campaign status",
Long: "Prints the current status of the campaign and its tickets",
Args: cobra.NoArgs,
Run: statusCmdF,
Run: withApp(statusCmdF),
}
}
func statusCmdF(cmd *cobra.Command, _ []string) {
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
cmp.PrintStatus()
func statusCmdF(a *app.App, cmd *cobra.Command, _ []string) {
a.Campaign.PrintStatus()
}