Skip to content

Commit

Permalink
govc: fix tasks to activate option dump/json/xml
Browse files Browse the repository at this point in the history
Closes: #2582
Signed-off-by: Syuparn <s.hello.spagetti@gmail.com>
  • Loading branch information
Syuparn committed Sep 12, 2021
1 parent 5196d83 commit 85956c7
Showing 1 changed file with 68 additions and 41 deletions.
109 changes: 68 additions & 41 deletions govc/task/recent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"io"
"strings"

"github.com/vmware/govmomi/govc/cli"
Expand Down Expand Up @@ -152,13 +153,15 @@ func (cmd *recent) Run(ctx context.Context, f *flag.FlagSet) error {
_ = v.Destroy(context.Background())
}()

v.Follow = cmd.follow
// writes dump/json/xml once even if follow is specified, otherwise syntax error occurs
writesPlain := !(cmd.Dump || cmd.JSON || cmd.XML)
v.Follow = cmd.follow && writesPlain

stamp := "15:04:05"
tmpl := "%-40s %-30s %-30s %9s %9s %9s %s\n"
fmt.Fprintf(cmd.Out, tmpl, "Task", "Target", "Initiator", "Queued", "Started", "Completed", "Result")
res := &taskResult{name: tn}
if writesPlain {
res.WriteHeader(cmd.Out)
}

var last string
updated := false

return v.Collect(ctx, func(tasks []types.TaskInfo) {
Expand All @@ -167,55 +170,79 @@ func (cmd *recent) Run(ctx context.Context, f *flag.FlagSet) error {
}
updated = true

for _, info := range tasks {
var user string
res.Tasks = tasks
cmd.WriteResult(res)
})
}

switch x := info.Reason.(type) {
case *types.TaskReasonUser:
user = x.UserName
}
type taskResult struct {
Tasks []types.TaskInfo
last string
name func(info *types.TaskInfo) string
}

if info.EntityName == "" || user == "" {
continue
}
func (t *taskResult) WriteHeader(w io.Writer) {
fmt.Fprintf(w, t.format("Task", "Target", "Initiator", "Queued", "Started", "Completed", "Result"))
}

ruser := strings.SplitN(user, "\\", 2)
if len(ruser) == 2 {
user = ruser[1] // discard domain
} else {
user = strings.TrimPrefix(user, "com.vmware.") // e.g. com.vmware.vsan.health
}
func (t *taskResult) Write(w io.Writer) error {
stamp := "15:04:05"

queued := info.QueueTime.Format(stamp)
start := "-"
end := start
for _, info := range t.Tasks {
var user string

if info.StartTime != nil {
start = info.StartTime.Format(stamp)
}
switch x := info.Reason.(type) {
case *types.TaskReasonUser:
user = x.UserName
}

msg := fmt.Sprintf("%2d%% %s", info.Progress, info.Task)
if info.EntityName == "" || user == "" {
continue
}

if info.CompleteTime != nil {
msg = info.CompleteTime.Sub(*info.StartTime).String()
ruser := strings.SplitN(user, "\\", 2)
if len(ruser) == 2 {
user = ruser[1] // discard domain
} else {
user = strings.TrimPrefix(user, "com.vmware.") // e.g. com.vmware.vsan.health
}

if info.State == types.TaskInfoStateError {
msg = strings.TrimSuffix(info.Error.LocalizedMessage, ".")
}
queued := info.QueueTime.Format(stamp)
start := "-"
end := start

end = info.CompleteTime.Format(stamp)
}
if info.StartTime != nil {
start = info.StartTime.Format(stamp)
}

result := fmt.Sprintf("%-7s [%s]", info.State, msg)
msg := fmt.Sprintf("%2d%% %s", info.Progress, info.Task)

item := fmt.Sprintf(tmpl, chop(tn(&info), 40), chop(info.EntityName, 30), chop(user, 30), queued, start, end, result)
if info.CompleteTime != nil {
msg = info.CompleteTime.Sub(*info.StartTime).String()

if item == last {
continue // task info was updated, but the fields we display were not
if info.State == types.TaskInfoStateError {
msg = strings.TrimSuffix(info.Error.LocalizedMessage, ".")
}
last = item

fmt.Fprint(cmd.Out, item)
end = info.CompleteTime.Format(stamp)
}
})

result := fmt.Sprintf("%-7s [%s]", info.State, msg)

item := t.format(chop(t.name(&info), 40), chop(info.EntityName, 30), chop(user, 30), queued, start, end, result)

if item == t.last {
continue // task info was updated, but the fields we display were not
}
t.last = item

fmt.Fprint(w, item)
}

return nil
}

func (t *taskResult) format(task, target, initiator, queued, started, completed, result string) string {
return fmt.Sprintf("%-40s %-30s %-30s %9s %9s %9s %s\n",
task, target, initiator, queued, started, completed, result)
}

0 comments on commit 85956c7

Please sign in to comment.