2020-02-29 00:49:55 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-04-27 10:42:29 +01:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-16 09:05:01 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/app"
|
2020-02-29 13:33:03 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
|
|
|
|
2020-02-29 00:49:55 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitCmd() *cobra.Command {
|
2020-02-29 13:33:03 +01:00
|
|
|
cmd := &cobra.Command{
|
2020-02-29 00:49:55 +01:00
|
|
|
Use: "init",
|
2020-04-27 11:22:15 +01:00
|
|
|
Short: "Creates a campaign",
|
|
|
|
Long: "Creates a new campaign in the current directory",
|
2020-04-16 09:05:01 +01:00
|
|
|
Example: ` campaigner init \
|
2020-04-27 10:42:29 +01:00
|
|
|
--jira-username johndoe \
|
|
|
|
--jira-token secret \
|
|
|
|
--github-token TOKEN \
|
2020-04-16 09:05:01 +01:00
|
|
|
--url http://my-jira-instance.com \
|
|
|
|
--epic ASD-27 \
|
|
|
|
--issue-type Story \
|
2020-04-27 10:42:29 +01:00
|
|
|
--repository johndoe/awesomeproject \
|
|
|
|
-l 'Area/API' -l 'Tech/Go' \
|
2020-04-16 09:05:01 +01:00
|
|
|
--summary 'Refactor {{.function}} to inject the configuration service' \
|
2020-04-28 10:21:31 +01:00
|
|
|
--issue-template ./refactor-config.tmpl \
|
|
|
|
--footer-template ./github-footer.tmpl
|
|
|
|
`,
|
2020-04-27 10:42:29 +01:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: initCmdF,
|
2020-02-29 00:49:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 10:42:29 +01:00
|
|
|
cmd.Flags().String("jira-username", "", "the jira username")
|
|
|
|
cmd.Flags().String("jira-token", "", "the jira token or password")
|
|
|
|
cmd.Flags().String("github-token", "", "the github token")
|
|
|
|
cmd.Flags().StringP("url", "u", "", "the jira server URL")
|
|
|
|
cmd.Flags().StringP("epic", "e", "", "the epic id to associate this campaign with")
|
|
|
|
cmd.Flags().StringP("repository", "r", "", "the github repository")
|
|
|
|
cmd.Flags().StringSliceP("label", "l", []string{}, "the labels to add to the Github issues")
|
|
|
|
cmd.Flags().StringP("summary", "s", "", "the summary of the tickets")
|
2020-04-28 10:21:31 +01:00
|
|
|
cmd.Flags().StringP("issue-template", "t", "", "the template path for the description of the tickets")
|
|
|
|
cmd.Flags().StringP("footer-template", "f", "", "the template path to append to the github issues as a footer")
|
2020-04-27 10:42:29 +01:00
|
|
|
cmd.Flags().StringP("issue-type", "i", "Story", "the issue type to create the tickets as")
|
2020-02-29 13:33:03 +01:00
|
|
|
|
|
|
|
return cmd
|
2020-02-29 00:49:55 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 13:33:03 +01:00
|
|
|
func initCmdF(cmd *cobra.Command, _ []string) {
|
2020-10-07 18:31:17 +01:00
|
|
|
campaignPath, _ := cmd.Flags().GetString("campaign")
|
|
|
|
|
|
|
|
_, err := os.Stat(campaignPath)
|
|
|
|
if err == nil {
|
|
|
|
ErrorAndExit(cmd, fmt.Errorf("cannot use %s as campaign file: file already exists", campaignPath))
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
ErrorAndExit(cmd, fmt.Errorf("cannot use %s as campaign file: %w", campaignPath, err))
|
|
|
|
}
|
|
|
|
|
2020-04-27 10:42:29 +01:00
|
|
|
getStringFlagOrAskIfEmpty := func(name string, question string) string {
|
|
|
|
val, _ := cmd.Flags().GetString(name)
|
|
|
|
if val == "" {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Printf("%s ", question)
|
|
|
|
answer, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
val = strings.TrimSpace(answer)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
jiraUsername := getStringFlagOrAskIfEmpty("jira-username", "JIRA username:")
|
|
|
|
jiraToken := getStringFlagOrAskIfEmpty("jira-token", "JIRA password or token:")
|
|
|
|
githubToken := getStringFlagOrAskIfEmpty("github-token", "GitHub token:")
|
|
|
|
url := getStringFlagOrAskIfEmpty("url", "JIRA server URL:")
|
|
|
|
epic := getStringFlagOrAskIfEmpty("epic", "JIRA epic:")
|
|
|
|
repo := getStringFlagOrAskIfEmpty("repository", "GitHub repository:")
|
|
|
|
summary := getStringFlagOrAskIfEmpty("summary", "Ticket summary template:")
|
2020-04-28 10:21:31 +01:00
|
|
|
issueTemplate := getStringFlagOrAskIfEmpty("issue-template", "Ticket description template path:")
|
|
|
|
footerTemplate := getStringFlagOrAskIfEmpty("footer-template", "Github issue footer template path:")
|
2020-03-05 21:31:20 +01:00
|
|
|
issueType, _ := cmd.Flags().GetString("issue-type")
|
2020-04-27 10:42:29 +01:00
|
|
|
labels, _ := cmd.Flags().GetStringSlice("label")
|
2020-03-05 17:38:53 +01:00
|
|
|
|
2020-04-16 09:05:01 +01:00
|
|
|
project := strings.Split(epic, "-")[0]
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
campaign := &model.Campaign{
|
2020-04-27 10:42:29 +01:00
|
|
|
Jira: model.ConfigJira{
|
|
|
|
Url: url,
|
|
|
|
Username: jiraUsername,
|
|
|
|
Token: jiraToken,
|
|
|
|
Project: project,
|
|
|
|
Epic: epic,
|
|
|
|
IssueType: issueType,
|
|
|
|
},
|
|
|
|
Github: model.ConfigGithub{
|
|
|
|
Token: githubToken,
|
|
|
|
Repo: repo,
|
|
|
|
Labels: labels,
|
|
|
|
},
|
2020-04-28 10:21:31 +01:00
|
|
|
Summary: summary,
|
|
|
|
IssueTemplate: issueTemplate,
|
|
|
|
FooterTemplate: footerTemplate,
|
2020-03-05 17:38:53 +01:00
|
|
|
}
|
2020-10-07 18:31:17 +01:00
|
|
|
if err := app.SaveCampaign(campaign, campaignPath); err != nil {
|
2020-02-29 13:33:03 +01:00
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-02-29 00:49:55 +01:00
|
|
|
}
|