2020-02-29 00:49:55 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-04-27 11:42:29 +02:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-16 10:05:01 +02:00
|
|
|
"strings"
|
|
|
|
|
2020-02-29 13:33:03 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
|
|
|
|
"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 12:22:15 +02:00
|
|
|
Short: "Creates a campaign",
|
|
|
|
Long: "Creates a new campaign in the current directory",
|
2020-04-16 10:05:01 +02:00
|
|
|
Example: ` campaigner init \
|
2020-04-27 11:42:29 +02:00
|
|
|
--jira-username johndoe \
|
|
|
|
--jira-token secret \
|
|
|
|
--github-token TOKEN \
|
2020-04-16 10:05:01 +02:00
|
|
|
--url http://my-jira-instance.com \
|
|
|
|
--epic ASD-27 \
|
|
|
|
--issue-type Story \
|
2020-04-27 11:42:29 +02:00
|
|
|
--repository johndoe/awesomeproject \
|
|
|
|
-l 'Area/API' -l 'Tech/Go' \
|
2020-04-16 10:05:01 +02:00
|
|
|
--summary 'Refactor {{.function}} to inject the configuration service' \
|
|
|
|
--template ./refactor-config.tmpl`,
|
2020-04-27 11:42:29 +02:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: initCmdF,
|
2020-02-29 00:49:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 11:42:29 +02: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")
|
|
|
|
cmd.Flags().StringP("template", "t", "", "the template path for the description of the tickets")
|
|
|
|
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-04-27 11:42:29 +02: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:")
|
|
|
|
template := getStringFlagOrAskIfEmpty("template", "Ticket description template path:")
|
2020-03-05 21:31:20 +01:00
|
|
|
issueType, _ := cmd.Flags().GetString("issue-type")
|
2020-04-27 11:42:29 +02:00
|
|
|
labels, _ := cmd.Flags().GetStringSlice("label")
|
2020-03-05 17:38:53 +01:00
|
|
|
|
2020-04-16 10:05:01 +02:00
|
|
|
project := strings.Split(epic, "-")[0]
|
|
|
|
|
2020-03-05 17:38:53 +01:00
|
|
|
cmp := &model.Campaign{
|
2020-04-27 11:42:29 +02: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,
|
|
|
|
},
|
|
|
|
Summary: summary,
|
|
|
|
Template: template,
|
2020-03-05 17:38:53 +01:00
|
|
|
}
|
|
|
|
if err := campaign.Save(cmp); err != nil {
|
2020-02-29 13:33:03 +01:00
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-02-29 00:49:55 +01:00
|
|
|
}
|