aboutsummaryrefslogtreecommitdiffstats
path: root/operations/label_change.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-13 12:20:28 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-13 12:20:28 +0200
commitf569e6aacc0690e7b1bebf33a10a8e0d154937df (patch)
treec17ac2201b7abbc6889137d968c8450dc3099f9a /operations/label_change.go
parent8a25c63d6972d9168dcb3599066076bdb84f8790 (diff)
downloadgit-bug-f569e6aacc0690e7b1bebf33a10a8e0d154937df.tar.gz
operations: return a more convenient array of result for label changes
Diffstat (limited to 'operations/label_change.go')
-rw-r--r--operations/label_change.go40
1 files changed, 26 insertions, 14 deletions
diff --git a/operations/label_change.go b/operations/label_change.go
index 26e7e94c..a3cc9898 100644
--- a/operations/label_change.go
+++ b/operations/label_change.go
@@ -2,8 +2,6 @@ package operations
import (
"fmt"
- "io"
- "io/ioutil"
"sort"
"github.com/MichaelMure/git-bug/bug"
@@ -60,13 +58,9 @@ func NewLabelChangeOperation(author bug.Person, added, removed []bug.Label) Labe
}
// ChangeLabels is a convenience function to apply the operation
-func ChangeLabels(out io.Writer, b bug.Interface, author bug.Person, add, remove []string) error {
- // TODO: return a channel of result (like MergeAll) instead of formatting the result for the upper layers
+func ChangeLabels(b bug.Interface, author bug.Person, add, remove []string) ([]LabelChangeResult, error) {
var added, removed []bug.Label
-
- if out == nil {
- out = ioutil.Discard
- }
+ var results []LabelChangeResult
snap := b.Compile()
@@ -75,17 +69,18 @@ func ChangeLabels(out io.Writer, b bug.Interface, author bug.Person, add, remove
// check for duplicate
if labelExist(added, label) {
- fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
+ results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDuplicateInOp})
continue
}
// check that the label doesn't already exist
if labelExist(snap.Labels, label) {
- fmt.Fprintf(out, "label \"%s\" is already set on this bug\n", str)
+ results = append(results, LabelChangeResult{Label: label, Status: LabelChangeAlreadySet})
continue
}
added = append(added, label)
+ results = append(results, LabelChangeResult{Label: label, Status: LabelChangeAdded})
}
for _, str := range remove {
@@ -93,28 +88,29 @@ func ChangeLabels(out io.Writer, b bug.Interface, author bug.Person, add, remove
// check for duplicate
if labelExist(removed, label) {
- fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
+ results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDuplicateInOp})
continue
}
// check that the label actually exist
if !labelExist(snap.Labels, label) {
- fmt.Fprintf(out, "label \"%s\" doesn't exist on this bug\n", str)
+ results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDoesntExist})
continue
}
removed = append(removed, label)
+ results = append(results, LabelChangeResult{Label: label, Status: LabelChangeRemoved})
}
if len(added) == 0 && len(removed) == 0 {
- return fmt.Errorf("no label added or removed")
+ return results, fmt.Errorf("no label added or removed")
}
labelOp := NewLabelChangeOperation(author, added, removed)
b.Append(labelOp)
- return nil
+ return results, nil
}
func labelExist(labels []bug.Label, label bug.Label) bool {
@@ -126,3 +122,19 @@ func labelExist(labels []bug.Label, label bug.Label) bool {
return false
}
+
+type LabelChangeStatus int
+
+const (
+ _ LabelChangeStatus = iota
+ LabelChangeAdded
+ LabelChangeRemoved
+ LabelChangeDuplicateInOp
+ LabelChangeAlreadySet
+ LabelChangeDoesntExist
+)
+
+type LabelChangeResult struct {
+ Label bug.Label
+ Status LabelChangeStatus
+}