|
| 1 | +// Copyright (c) All respective contributors to the Peridot Project. All rights reserved. |
| 2 | +// Copyright (c) 2021-2022 Rocky Enterprise Software Foundation, Inc. All rights reserved. |
| 3 | +// Copyright (c) 2021-2022 Ctrl IQ, Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are met: |
| 7 | +// |
| 8 | +// 1. Redistributions of source code must retain the above copyright notice, |
| 9 | +// this list of conditions and the following disclaimer. |
| 10 | +// |
| 11 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +// this list of conditions and the following disclaimer in the documentation |
| 13 | +// and/or other materials provided with the distribution. |
| 14 | +// |
| 15 | +// 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | +// may be used to endorse or promote products derived from this software without |
| 17 | +// specific prior written permission. |
| 18 | +// |
| 19 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +// POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +package main |
| 32 | + |
| 33 | +import ( |
| 34 | + "errors" |
| 35 | + "fmt" |
| 36 | + "log" |
| 37 | + "os" |
| 38 | + "slices" |
| 39 | + |
| 40 | + "github.com/google/uuid" |
| 41 | + "github.com/olekukonko/tablewriter" |
| 42 | + "github.com/spf13/cobra" |
| 43 | + "openapi.peridot.resf.org/peridotopenapi" |
| 44 | +) |
| 45 | + |
| 46 | +var taskInfo = &cobra.Command{ |
| 47 | + Use: "info [name-or-buildId]", |
| 48 | + Args: cobra.ExactArgs(1), |
| 49 | + Run: taskInfoMn, |
| 50 | +} |
| 51 | + |
| 52 | +var ( |
| 53 | + showLogLink bool |
| 54 | + showSubmitterInfo bool |
| 55 | + showDuration bool |
| 56 | +) |
| 57 | + |
| 58 | +func init() { |
| 59 | + taskInfo.Flags().BoolVar(&succeeded, "succeeded", true, "only query successful tasks") |
| 60 | + taskInfo.Flags().BoolVar(&cancelled, "cancelled", false, "only query cancelled tasks") |
| 61 | + taskInfo.Flags().BoolVar(&failed, "failed", false, "only query failed tasks") |
| 62 | + taskInfo.MarkFlagsMutuallyExclusive("cancelled", "failed", "succeeded") |
| 63 | + |
| 64 | + taskInfo.Flags().BoolVarP(&showLogLink, "logs", "L", false, "include log link in output (table format only)") |
| 65 | + taskInfo.Flags().BoolVar(&showSubmitterInfo, "submitter", false, "include submitter details (table format only)") |
| 66 | + taskInfo.Flags().BoolVar(&showDuration, "duration", true, "include duration from start to stop (table format only)") |
| 67 | +} |
| 68 | + |
| 69 | +func getNextColor(color int) int { |
| 70 | + switch color { |
| 71 | + case 0: |
| 72 | + return tablewriter.FgRedColor |
| 73 | + case tablewriter.FgCyanColor: |
| 74 | + return tablewriter.FgHiRedColor |
| 75 | + case tablewriter.FgHiWhiteColor: |
| 76 | + return tablewriter.FgRedColor |
| 77 | + default: |
| 78 | + color++ |
| 79 | + return color |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func buildHeaderAndAutoMergeCells() ([]string, []int) { |
| 84 | + header := []string{"ptid", "tid", "status", "type", "arch", "created", "finished"} |
| 85 | + mergableNames := []string{"ptid", "type", "arch"} |
| 86 | + var autoMergeCells []int |
| 87 | + |
| 88 | + // Conditional appending to header |
| 89 | + if showDuration { |
| 90 | + header = append(header, "duration") |
| 91 | + mergableNames = append(mergableNames, "duration") |
| 92 | + } |
| 93 | + if showSubmitterInfo { |
| 94 | + header = append(header, "submitter") |
| 95 | + mergableNames = append(mergableNames, "submitter") |
| 96 | + } |
| 97 | + if showLogLink { |
| 98 | + header = append(header, "logs") |
| 99 | + } |
| 100 | + |
| 101 | + // Determine dynamic indices for auto-merge cells |
| 102 | + for _, itemName := range mergableNames { |
| 103 | + index := slices.Index(header, itemName) |
| 104 | + if index != -1 { |
| 105 | + autoMergeCells = append(autoMergeCells, index) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return header, autoMergeCells |
| 110 | +} |
| 111 | + |
| 112 | +func convertSubTaskSliceToCSV(task peridotopenapi.V1AsyncTask) { |
| 113 | + subtasks, ok := task.GetSubtasksOk() |
| 114 | + if !ok { |
| 115 | + errFatal(fmt.Errorf("error getting subtasks: %v", ok)) |
| 116 | + } |
| 117 | + |
| 118 | + var parentTask = (*subtasks)[0] |
| 119 | + |
| 120 | + var table = tablewriter.NewWriter(os.Stdout) |
| 121 | + // var data [][]string |
| 122 | + |
| 123 | + var header, autoMergeCells = buildHeaderAndAutoMergeCells() |
| 124 | + |
| 125 | + var lastColor = 0 // initial color |
| 126 | + var seenTasksColors = make(map[string]int) // track seen task Ids |
| 127 | + |
| 128 | + var parentTaskIds []string // cache parentTaskIds for colorizing |
| 129 | + |
| 130 | + // precache all the subtask's parent tasks so we know if we should color them |
| 131 | + for _, subtask := range *subtasks { |
| 132 | + parentTaskIds = append(parentTaskIds, subtask.GetParentTaskId()) |
| 133 | + } |
| 134 | + |
| 135 | + for _, subtask := range *subtasks { |
| 136 | + json, err := subtask.MarshalJSON() |
| 137 | + if err != nil { |
| 138 | + errFatal(err) |
| 139 | + } |
| 140 | + |
| 141 | + if debug() { |
| 142 | + err = PrettyPrintJSON(json) |
| 143 | + if err != nil { |
| 144 | + errFatal(err) |
| 145 | + } |
| 146 | + // taskResponse, _ := subtask.GetResponse().MarshalJSON() |
| 147 | + // taskMetadata, _ := subtask.GetMetadata().MarshalJSON() |
| 148 | + } |
| 149 | + |
| 150 | + subtaskId := subtask.GetId() |
| 151 | + subtaskParentTaskId := subtask.GetParentTaskId() |
| 152 | + createdAt := subtask.GetCreatedAt() |
| 153 | + finishedAt := subtask.GetFinishedAt() |
| 154 | + |
| 155 | + row := []string{ |
| 156 | + subtaskParentTaskId, |
| 157 | + subtaskId, |
| 158 | + string(subtask.GetStatus()), |
| 159 | + string(subtask.GetType()), |
| 160 | + subtask.GetArch(), |
| 161 | + formatTime(createdAt), |
| 162 | + formatTime(finishedAt), |
| 163 | + } |
| 164 | + |
| 165 | + if showDuration { |
| 166 | + row = append(row, formatDuration(createdAt, finishedAt)) |
| 167 | + } |
| 168 | + |
| 169 | + if showSubmitterInfo { |
| 170 | + effectiveSubmitter := fmt.Sprintf("%s <%s>", parentTask.GetSubmitterId(), parentTask.GetSubmitterEmail()) |
| 171 | + row = append(row, effectiveSubmitter) |
| 172 | + } |
| 173 | + |
| 174 | + if showLogLink { |
| 175 | + row = append(row, getLogLink(subtaskId)) |
| 176 | + } |
| 177 | + |
| 178 | + nextColor := tablewriter.FgWhiteColor |
| 179 | + needsColor := taskIdIsAnyParentTaskId(parentTaskIds, subtaskId) |
| 180 | + if _, seen := seenTasksColors[subtaskId]; !seen && needsColor { |
| 181 | + debugP("before: lastcolor: %d nextcolor %d", lastColor, nextColor) |
| 182 | + nextColor = getNextColor(lastColor) |
| 183 | + debugP("after: lastcolor: %d nextcolor %d", lastColor, nextColor) |
| 184 | + lastColor = nextColor |
| 185 | + seenTasksColors[subtaskId] = nextColor |
| 186 | + } |
| 187 | + |
| 188 | + ptidColor := tablewriter.FgWhiteColor |
| 189 | + if seenColor, seen := seenTasksColors[subtaskParentTaskId]; seen { |
| 190 | + ptidColor = seenColor |
| 191 | + } |
| 192 | + |
| 193 | + var colors = make([]tablewriter.Colors, len(row)) |
| 194 | + |
| 195 | + debugP("color: %d ptidcolor %d", nextColor, ptidColor) |
| 196 | + for i, v := range header { |
| 197 | + switch v { |
| 198 | + case "ptid": |
| 199 | + colors[i] = tablewriter.Colors{ptidColor} |
| 200 | + case "tid": |
| 201 | + if needsColor { |
| 202 | + colors[i] = tablewriter.Colors{nextColor} // if it is a parent |
| 203 | + } else { |
| 204 | + colors[i] = tablewriter.Colors{tablewriter.BgBlackColor, tablewriter.FgWhiteColor} // childless cat ladies |
| 205 | + } |
| 206 | + default: |
| 207 | + colors[i] = tablewriter.Colors{} |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + table.Rich(row, colors) |
| 212 | + } |
| 213 | + |
| 214 | + table.SetHeader(header) |
| 215 | + table.SetAutoMergeCellsByColumnIndex(autoMergeCells) |
| 216 | + table.SetRowLine(true) |
| 217 | + table.Render() |
| 218 | + |
| 219 | +} |
| 220 | + |
| 221 | +func debugP(s string, args ...any) { |
| 222 | + if debug() { |
| 223 | + log.Printf(s, args...) |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +func taskIdIsAnyParentTaskId(parentTaskIds []string, subtaskId string) bool { |
| 228 | + if idx := slices.Index(parentTaskIds, subtaskId); idx > 0 { |
| 229 | + return true |
| 230 | + } |
| 231 | + return false |
| 232 | +} |
| 233 | + |
| 234 | +func taskInfoMn(_ *cobra.Command, args []string) { |
| 235 | + // Ensure project id exists |
| 236 | + projectId := mustGetProjectID() |
| 237 | + |
| 238 | + taskId := args[0] |
| 239 | + |
| 240 | + err := uuid.Validate(taskId) |
| 241 | + if err != nil { |
| 242 | + errFatal(errors.New("invalid task id")) |
| 243 | + } |
| 244 | + |
| 245 | + taskCl := getClient(serviceTask).(peridotopenapi.TaskServiceApi) |
| 246 | + log.Printf("Searching for task %s in project %s\n", taskId, projectId) |
| 247 | + |
| 248 | + res, _, err := taskCl.GetTask(getContext(), projectId, taskId).Execute() |
| 249 | + if err != nil { |
| 250 | + errFatal(fmt.Errorf("error getting task: %s", err.Error())) |
| 251 | + } |
| 252 | + |
| 253 | + switch output() { |
| 254 | + case "table": |
| 255 | + convertSubTaskSliceToCSV(res.GetTask()) |
| 256 | + |
| 257 | + case "json": |
| 258 | + taskJSON, err := res.MarshalJSON() |
| 259 | + if err != nil { |
| 260 | + errFatal(err) |
| 261 | + } |
| 262 | + |
| 263 | + err = PrettyPrintJSON(taskJSON) |
| 264 | + if err != nil { |
| 265 | + errFatal(err) |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments