Skip to content

Commit

Permalink
feat: added configuration and removal options to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
JaleelB committed May 22, 2024
1 parent 353342e commit 3434083
Showing 1 changed file with 160 additions and 115 deletions.
275 changes: 160 additions & 115 deletions internal/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"os"
"strings"

"github.com/logrusorgru/aurora"
Expand All @@ -14,6 +15,131 @@ type options struct {
Context string
}

func removeJiraFlow(jiraManager *JiraManager) {
fmt.Println(aurora.BrightMagenta("\nRemoving Jira-Flow..."))

if err := RemoveGitHooks(jiraManager.Config.HookPath); err != nil {
fmt.Printf("Error removing Git hooks: %v\n", err)
return
}

fmt.Println(aurora.BrightMagenta("Jira-Flow has been successfully removed from this repository."))
}

func configureJiraFlow(jiraManager *JiraManager) {

options := []options{
{Name: "Automatically", Context: "Automatically link commits to Jira issues based on branch name"},
{Name: "Manually", Context: "Manually link commits to Jira issues by entering the Jira issue key"},
}

templates := &promptui.SelectTemplates{
Label: "\u003F {{ . | white }}",
Active: "\u27A4 {{ .Name | cyan }}",
Inactive: " {{ .Name }}",
Selected: "\u2713 How would you like to link your commits to a Jira issue: {{ .Context }}",
Details: `
+ {{ .Context | faint }}
`,
}

searcher := func(input string, index int) bool {
option := options[index]
name := strings.Replace(strings.ToLower(option.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(name, input)
}

selectPrompt := promptui.Select{
Label: "How would you like to link your commits to a Jira issue?",
Items: options,
Templates: templates,
Size: 4,
Searcher: searcher,
}

idx, _, err := selectPrompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

selectedOption := options[idx]

switch selectedOption.Name {
case "Automatically":
jiraManager.Config.AutoLink = true

branchName, _ := GetCurrentBranchName()
issueKey, err := jiraManager.ExtractIssueKeyFromBranchName(branchName)

if err != nil {
fmt.Printf("\nFailed to extract JIRA issue key from branch name: %v\n", err)
return
}

jiraManager.Config.JiraKey = issueKey

checkDirErr := CheckGitAndHooksDir()
if checkDirErr != nil {
fmt.Printf("\nFailed to check git and hooks directory: %v\n", checkDirErr)
return
}

setHookErr := SetGitHooks(jiraManager.Config)
if setHookErr != nil {
fmt.Printf("\nFailed to set git hooks: %v\n", setHookErr)
return
}

msg := fmt.Sprintf("\nSuccess! The issue key %q will now be prepended to your commits linking them to your JIRA issue.", issueKey)
fmt.Printf("%s", aurora.BrightMagenta(msg))

case "Manually":

prompt := promptui.Prompt{
Label: "JIRA Issue Key: ",
Validate: func(input string) error {
_, err := jiraManager.ValidateJiraKey(input)
return err
},
Templates: &promptui.PromptTemplates{
Prompt: "\u003F {{ . }}",
Valid: "\u003F {{ . | white }}",
Invalid: "\u003F {{ . | white }}",
Success: "\u2713 {{ . | white }}",
},
}

result, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

jiraManager.Config.JiraKey = result
jiraManager.Config.AutoLink = false

checkDirErr := CheckGitAndHooksDir()
if checkDirErr != nil {
fmt.Printf("\nFailed to check git and hooks directory: %v\n", checkDirErr)
return
}

setHookErr := SetGitHooks(jiraManager.Config)
if setHookErr != nil {
fmt.Printf("\nFailed to set git hooks: %v\n", setHookErr)
return
}

msg := fmt.Sprintf("\nYou have entered JIRA issue key: %q. The issue key will now prepeded to your commmits linking them to your JIRA issue.", result)
fmt.Printf("%s", aurora.BrightMagenta(msg))
}
}

func CLIMenu(
config *Config,
) *cobra.Command {
Expand All @@ -38,126 +164,45 @@ func CLIMenu(
description := aurora.BrightMagenta("\nWelcome to JiraFlow! JiraFlow will help you link your commits with JIRA tickets directly from your command line.\n")
fmt.Println(description)

options := []options{
{Name: "Automatically", Context: "Automatically link commits to Jira issues based on branch name"},
{Name: "Manually", Context: "Manually link commits to Jira issues by entering the Jira issue key"},
mainOptions := []options{
{Name: "Configure", Context: "Configure JiraFlow for this repository"},
{Name: "Remove", Context: "Remove JiraFlow from this repository"},
{Name: "Exit", Context: "Exit JiraFlow"},
}

templates := &promptui.SelectTemplates{
Label: "\u003F {{ . }}",
Active: "\u27A4 {{ .Name | cyan }}",
Inactive: " {{ .Name | white }}",
Selected: "\u2713 How would you like to link your commits to a Jira issue: {{ .Context }}",
mainTemplates := &promptui.SelectTemplates{
Label: "\u003F {{ . | white }}",
Active: "\u27A4 {{ .Context | cyan }}",
Inactive: " {{ .Context }}",
Selected: "\u2713 {{ .Context }}",
Details: `
+ {{ .Context | faint }}
`,
}

searcher := func(input string, index int) bool {
option := options[index]
name := strings.Replace(strings.ToLower(option.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(name, input)
}

selectPrompt := promptui.Select{
Label: "How would you like to link your commits to a Jira issue?",
Items: options,
Templates: templates,
Size: 4,
Searcher: searcher,
}

idx, _, err := selectPrompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

selectedOption := options[idx]

switch selectedOption.Name {
case "Automatically":
jiraManager.Config.AutoLink = true

branchName, _ := GetCurrentBranchName()
issueKey, err := jiraManager.ExtractIssueKeyFromBranchName(branchName)

if err != nil {
fmt.Printf("\nFailed to extract JIRA issue key from branch name: %v\n", err)
return
}

jiraManager.Config.JiraKey = issueKey

checkDirErr := CheckGitAndHooksDir()
if checkDirErr != nil {
fmt.Printf("\nFailed to check git and hooks directory: %v\n", checkDirErr)
return
}

// setHookErr := SetGitHookScript(jiraManager.Config)
// if setHookErr != nil {
// fmt.Printf("\nFailed to set git hook script: %v\n", err)
// return
// }
setHookErr := SetGitHooks(jiraManager.Config)
if setHookErr != nil {
fmt.Printf("\nFailed to set git hooks: %v\n", setHookErr)
return
}

msg := fmt.Sprintf("\nSuccess! The issue key %q will now be prepended to your commits linking them to your JIRA issue.", issueKey)
fmt.Printf("%s", aurora.BrightMagenta(msg))

case "Manually":

prompt := promptui.Prompt{
Label: "JIRA Issue Key: ",
Validate: func(input string) error {
_, err := jiraManager.ValidateJiraKey(input)
return err
},
Templates: &promptui.PromptTemplates{
Prompt: "\u003F {{ . }}",
Valid: "\u003F {{ . | white }}",
Invalid: "\u003F {{ . | white }}",
Success: "\u2713 {{ . | white }}",
},
}

result, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

jiraManager.Config.JiraKey = result
jiraManager.Config.AutoLink = false

checkDirErr := CheckGitAndHooksDir()
if checkDirErr != nil {
fmt.Printf("\nFailed to check git and hooks directory: %v\n", checkDirErr)
return
}

// setHookErr := SetGitHookScript(jiraManager.Config)
// if setHookErr != nil {
// fmt.Printf("\nFailed to set git hook script: %v\n", err)
// return
// }
setHookErr := SetGitHooks(jiraManager.Config)
if setHookErr != nil {
fmt.Printf("\nFailed to set git hooks: %v\n", setHookErr)
return
}

msg := fmt.Sprintf("\nYou have entered JIRA issue key: %q. The issue key will now prepeded to your commmits linking them to your JIRA issue.", result)
fmt.Printf("%s", aurora.BrightMagenta(msg))
}
}

mainSelectPrompt := promptui.Select{
Label: "What would you like to do?",
Items: mainOptions,
Templates: mainTemplates,
Size: 3,
}

idx, _, err := mainSelectPrompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

switch mainOptions[idx].Name {
case "Configure":
configureJiraFlow(jiraManager)
case "Remove":
removeJiraFlow(jiraManager)
case "Exit":
fmt.Print(aurora.BrightMagenta("\nExiting JiraFlow..."))
os.Exit(0)
}

},
}

Expand Down

0 comments on commit 3434083

Please sign in to comment.