-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
106 lines (93 loc) · 2.04 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package issue
import (
"github.com/AlecAivazis/survey/v2"
"github.com/andygrunwald/go-jira"
"github.com/briandowns/spinner"
"github.com/locona/jira/pkg/issue"
"github.com/locona/jira/pkg/prompt"
"github.com/spf13/cobra"
)
func NewCommandUpdate() *cobra.Command {
// updateOption := &issue.Search{}
cmd := &cobra.Command{
Use: "update",
RunE: func(cmd *cobra.Command, args []string) error {
return Update()
},
}
return cmd
}
type UpdateCommand struct {
Value *jira.Issue
Result []jira.Issue
}
func (cmd *UpdateCommand) Request(s *spinner.Spinner) error {
updated, err := issue.Update(cmd.Value)
if err != nil {
return err
}
cmd.Result = []jira.Issue{*updated}
return nil
}
func (cmd *UpdateCommand) Response() error {
issue.ViewTable(cmd.Result)
return nil
}
func Update() error {
selectedIssue, err := selectIssue()
if err != nil {
return err
}
selectField := selectField()
v := &jira.Issue{
Key: selectedIssue.Key,
Fields: &jira.IssueFields{},
}
switch selectField {
case issue.FieldSummary:
summary := inputValue(selectedIssue.Fields.Summary)
v.Fields.Summary = summary
case issue.FieldDescription:
description := inputValue(selectedIssue.Fields.Description)
v.Fields.Description = description
}
return prompt.Progress(&UpdateCommand{
Value: v,
})
}
func selectIssue() (*jira.Issue, error) {
issueList, err := issue.List(&issue.Search{})
if err != nil {
return nil, err
}
options, mapOptionToIssue := issue.Options(issueList)
p := &survey.Select{
Message: "Select Update ISSUE ID",
Options: options,
}
var res string
err = survey.AskOne(p, &res, nil)
if err != nil {
return nil, err
}
selected := mapOptionToIssue[res]
return &selected, nil
}
func selectField() string {
p := &survey.Select{
Message: "Select Update Field",
Options: issue.Fields,
}
var res string
survey.AskOne(p, &res, nil)
return res
}
func inputValue(current string) string {
p := &survey.Multiline{
Message: "Input Value",
Default: current,
}
var res string
survey.AskOne(p, &res, nil)
return res
}