Skip to content

Commit

Permalink
✨ RawError, RawActivity simplified. (#458)
Browse files Browse the repository at this point in the history
This PR simplifies the API for use cases for which the _new_ RawError()
and RawActivity() were meant to support.

Especially the RawActivity(). The main case here is to report multi-line
activity. In this case the activity is reporting something followed by
multiple lines. The first line is the-activity followed by
multiple-lines of detail. This PR simplifies this more than the
RawActivity() because it can be achieved in (1) addon.Activity() call.

For example:
```
addon.Activity("Doing something with a list of things:\n%s", "One\nTwo\nThree")
```
Results  in:
```
- Doing something with a list of things:
- > One
- > Two
- > Three
```

The Error and RawError is achieved by reversing which is the edge case.
Errorf() is added (instead of RawError) which makes for a more intuitive
API.

Signed-off-by: Jeff Ortel <jortel@redhat.com>
  • Loading branch information
jortel committed Jul 25, 2023
1 parent 89241be commit 0d7f07a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 55 deletions.
83 changes: 29 additions & 54 deletions addon/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/task"
"gopkg.in/yaml.v2"
"strings"
)

Expand Down Expand Up @@ -91,14 +90,10 @@ func (h *Task) Succeeded() {
// The reason can be a printf style format.
func (h *Task) Failed(reason string, v ...interface{}) {
reason = fmt.Sprintf(reason, v...)
h.report.Status = task.Failed
h.report.Errors = append(
h.report.Errors,
api.TaskError{
Severity: "Error",
Description: reason,
})
h.pushReport()
h.Error(api.TaskError{
Severity: "Error",
Description: reason,
})
Log.Info(
"Addon reported: failed.",
"reason",
Expand All @@ -107,30 +102,17 @@ func (h *Task) Failed(reason string, v ...interface{}) {
}

//
// Error report addon error.
// The description can be a printf style format.
func (h *Task) Error(severity, description string, v ...interface{}) {
description = fmt.Sprintf(description, v...)
h.RawError(
api.TaskError{
Severity: severity,
Description: description,
})
return
}

//
// Activity report addon activity.
// The description can be a printf style format.
func (h *Task) Activity(entry string, v ...interface{}) {
entry = fmt.Sprintf(entry, v...)
h.RawActivity(entry)
return
// Errorf report addon error.
func (h *Task) Errorf(severity, description string, v ...interface{}) {
h.Error(api.TaskError{
Severity: severity,
Description: fmt.Sprintf(description, v...),
})
}

//
// RawError report addon error.
func (h *Task) RawError(error ...api.TaskError) {
// Error report addon error.
func (h *Task) Error(error ...api.TaskError) {
h.report.Status = task.Failed
for i := range error {
h.report.Errors = append(
Expand All @@ -146,31 +128,24 @@ func (h *Task) RawError(error ...api.TaskError) {
}

//
// RawActivity report addon activity.
func (h *Task) RawActivity(object interface{}) {
switch object.(type) {
case string:
s := object.(string)
h.RawActivity(strings.Split(s, "\n"))
case []string:
prefix := ""
list := object.([]string)
if len(list) > 1 {
prefix = "> "
}
for i := range list {
entry := prefix + list[i]
h.report.Activity = append(
h.report.Activity,
entry)
Log.Info(
"Addon reported: activity.",
"object",
entry)
// Activity report addon activity.
// The description can be a printf style format.
func (h *Task) Activity(entry string, v ...interface{}) {
entry = fmt.Sprintf(entry, v...)
lines := strings.Split(entry, "\n")
for i := range lines {
if i > 0 {
entry = "> " + lines[i]
} else {
entry = lines[i]
}
default:
b, _ := yaml.Marshal(object)
h.RawActivity(string(b))
h.report.Activity = append(
h.report.Activity,
entry)
Log.Info(
"Addon reported: activity.",
"entry",
entry)
}
h.pushReport()
return
Expand Down
2 changes: 1 addition & 1 deletion hack/cmd/addon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func main() {
return
})

addon.Error("Warning", "Test warning.")
addon.Errorf("Warning", "Test warning.")
}

//
Expand Down

0 comments on commit 0d7f07a

Please sign in to comment.