Skip to content

Commit 8fc7697

Browse files
authored
Merge pull request #81 from trickest/fix/output
Output command improvements
2 parents a474bb9 + e1063c8 commit 8fc7697

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ trickest output --workflow <workflow_name> --space <space_name> [--nodes <comma_
192192
| --runs | integer | 1 | The number of executions to be downloaded sorted by newest |
193193
| --output-dir | string | / | Path to directory which should be used to store outputs |
194194
| --nodes | string | / | A comma separated list of nodes whose outputs should be downloaded |
195-
195+
| --files | string | / | A comma-separated list of file names that should be downloaded from the selected node |
196196

197197
## Output Structure
198198

cmd/execute/helpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"github.com/google/uuid"
87
"io"
98
"math"
109
"mime/multipart"
@@ -21,6 +20,8 @@ import (
2120
"trickest-cli/types"
2221
"trickest-cli/util"
2322

23+
"github.com/google/uuid"
24+
2425
"github.com/schollz/progressbar/v3"
2526
)
2627

@@ -133,7 +134,7 @@ func createRun(versionID uuid.UUID, watch bool, machines *types.Bees, outputNode
133134
watch = true
134135
}
135136
if watch {
136-
WatchRun(createRunResp.ID, outputsDir, nodesToDownload, false, &executionMachines, showParams)
137+
WatchRun(createRunResp.ID, outputsDir, nodesToDownload, nil, false, &executionMachines, showParams)
137138
} else {
138139
availableBees := GetAvailableMachines()
139140
fmt.Println("Run successfully created! ID: " + createRunResp.ID.String())

cmd/execute/watch.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package execute
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/google/uuid"
7-
"github.com/gosuri/uilive"
8-
"github.com/xlab/treeprint"
96
"os"
107
"os/signal"
118
"regexp"
@@ -18,9 +15,13 @@ import (
1815
"trickest-cli/cmd/output"
1916
"trickest-cli/types"
2017
"trickest-cli/util"
18+
19+
"github.com/google/uuid"
20+
"github.com/gosuri/uilive"
21+
"github.com/xlab/treeprint"
2122
)
2223

23-
func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]output.NodeInfo, timestampOnly bool, machines *types.Bees, showParameters bool) {
24+
func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]output.NodeInfo, filesToDownload []string, timestampOnly bool, machines *types.Bees, showParameters bool) {
2425
const fmtStr = "%-12s %v\n"
2526
writer := uilive.New()
2627
writer.Start()
@@ -121,9 +122,9 @@ func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]o
121122
}
122123
if downloadAllNodes {
123124
// DownloadRunOutputs downloads all outputs if no nodes were specified
124-
output.DownloadRunOutput(run, nil, nil, downloadPath)
125+
output.DownloadRunOutput(run, nil, nil, nil, downloadPath)
125126
} else if len(nodesToDownload) > 0 {
126-
output.DownloadRunOutput(run, nodesToDownload, nil, downloadPath)
127+
output.DownloadRunOutput(run, nodesToDownload, filesToDownload, nil, downloadPath)
127128
}
128129
mutex.Unlock()
129130
return

cmd/get/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var GetCmd = &cobra.Command{
6464
if runs[0].Status == "COMPLETED" && runs[0].CompletedDate.IsZero() {
6565
runs[0].Status = "RUNNING"
6666
}
67-
execute.WatchRun(runs[0].ID, "", map[string]output.NodeInfo{}, !watch, &runs[0].Bees, showNodeParams)
67+
execute.WatchRun(runs[0].ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Bees, showNodeParams)
6868
return
6969
} else {
7070
const fmtStr = "%-15s %v\n"

cmd/output/output.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var (
4040
runID string
4141
outputDir string
4242
nodesFlag string
43+
filesFlag string
4344
)
4445

4546
// OutputCmd represents the download command
@@ -67,6 +68,13 @@ The YAML config file should be formatted like:
6768
}
6869
}
6970

71+
var files []string
72+
if filesFlag != "" {
73+
for _, file := range strings.Split(filesFlag, ",") {
74+
files = append(files, file)
75+
}
76+
}
77+
7078
path := util.FormatPath()
7179
if path == "" {
7280
if len(args) == 0 {
@@ -144,7 +152,7 @@ The YAML config file should be formatted like:
144152
runs = []types.Run{*run}
145153
}
146154

147-
if numberOfRuns == 1 && (runs[0].Status == "SCHEDULED" || runs[0].CreationType == types.RunCreationScheduled) {
155+
if numberOfRuns == 1 && runs[0].Status == "SCHEDULED" {
148156
runs = GetRuns(workflow.ID, numberOfRuns+1)
149157
runs = append(runs, runs...)
150158
}
@@ -161,7 +169,7 @@ The YAML config file should be formatted like:
161169
if run.Status == "SCHEDULED" {
162170
continue
163171
}
164-
DownloadRunOutput(&run, nodes, version, path)
172+
DownloadRunOutput(&run, nodes, files, version, path)
165173
}
166174
},
167175
}
@@ -173,9 +181,10 @@ func init() {
173181
OutputCmd.Flags().StringVar(&runID, "run", "", "Download output data of a specific run")
174182
OutputCmd.Flags().StringVar(&outputDir, "output-dir", "", "Path to directory which should be used to store outputs")
175183
OutputCmd.Flags().StringVar(&nodesFlag, "nodes", "", "A comma-separated list of nodes whose outputs should be downloaded")
184+
OutputCmd.Flags().StringVar(&filesFlag, "files", "", "A comma-separated list of file names that should be downloaded from the selected node")
176185
}
177186

178-
func DownloadRunOutput(run *types.Run, nodes map[string]NodeInfo, version *types.WorkflowVersionDetailed, destinationPath string) {
187+
func DownloadRunOutput(run *types.Run, nodes map[string]NodeInfo, files []string, version *types.WorkflowVersionDetailed, destinationPath string) {
179188
if run.Status != "COMPLETED" && run.Status != "STOPPED" && run.Status != "FAILED" {
180189
fmt.Println("The workflow run hasn't been completed yet!")
181190
fmt.Println("Run ID: " + run.ID.String() + " Status: " + run.Status)
@@ -246,7 +255,7 @@ func DownloadRunOutput(run *types.Run, nodes map[string]NodeInfo, version *types
246255
}
247256
subJob.OutputsStatus = updatedSubJob.OutputsStatus
248257
}
249-
getSubJobOutput(runDir, &subJob, true)
258+
getSubJobOutput(runDir, &subJob, files, true)
250259
}
251260
} else {
252261
noneFound := true
@@ -272,7 +281,7 @@ func DownloadRunOutput(run *types.Run, nodes map[string]NodeInfo, version *types
272281
}
273282
subJob.OutputsStatus = updatedSubJob.OutputsStatus
274283
}
275-
getSubJobOutput(runDir, &subJob, true)
284+
getSubJobOutput(runDir, &subJob, files, true)
276285
}
277286
}
278287
if noneFound {
@@ -287,7 +296,7 @@ func DownloadRunOutput(run *types.Run, nodes map[string]NodeInfo, version *types
287296
}
288297
}
289298

290-
func getSubJobOutput(savePath string, subJob *types.SubJob, fetchData bool) []types.SubJobOutput {
299+
func getSubJobOutput(savePath string, subJob *types.SubJob, files []string, fetchData bool) []types.SubJobOutput {
291300
if subJob.OutputsStatus != "SAVED" && !subJob.TaskGroup {
292301
return nil
293302
}
@@ -339,7 +348,7 @@ func getSubJobOutput(savePath string, subJob *types.SubJob, fetchData bool) []ty
339348
results := make([]types.SubJobOutput, 0)
340349
if subJob.Children != nil {
341350
for _, child := range subJob.Children {
342-
childRes := getSubJobOutput(savePath, &child, true)
351+
childRes := getSubJobOutput(savePath, &child, files, true)
343352
if childRes != nil {
344353
results = append(results, childRes...)
345354
}
@@ -361,7 +370,8 @@ func getSubJobOutput(savePath string, subJob *types.SubJob, fetchData bool) []ty
361370
}
362371
}
363372

364-
for i, output := range subJobOutputs.Results {
373+
subJobOutputResults := filterSubJobOutputsByFileNames(subJobOutputs.Results, files)
374+
for i, output := range subJobOutputResults {
365375
resp := request.Trickest.Post().DoF("subjob-output/%s/signed_url/", output.ID)
366376
if resp == nil {
367377
fmt.Println("Error: Couldn't get sub-job outputs signed URL.")
@@ -380,22 +390,22 @@ func getSubJobOutput(savePath string, subJob *types.SubJob, fetchData bool) []ty
380390
}
381391

382392
if resp.Status() == http.StatusNotFound {
383-
subJobOutputs.Results[i].SignedURL = "expired"
393+
subJobOutputResults[i].SignedURL = "expired"
384394
} else {
385-
subJobOutputs.Results[i].SignedURL = signedURL.Url
395+
subJobOutputResults[i].SignedURL = signedURL.Url
386396

387397
if fetchData {
388-
fileName := subJobOutputs.Results[i].FileName
398+
fileName := subJobOutputResults[i].FileName
389399

390-
if fileName != subJobOutputs.Results[i].Path {
391-
subDirsPath := strings.TrimSuffix(subJobOutputs.Results[i].Path, fileName)
400+
if fileName != subJobOutputResults[i].Path {
401+
subDirsPath := strings.TrimSuffix(subJobOutputResults[i].Path, fileName)
392402
err := os.MkdirAll(subDirsPath, 0755)
393403
if err != nil {
394404
fmt.Println(err)
395405
fmt.Println("Couldn't create a directory to store run output!")
396406
os.Exit(0)
397407
}
398-
fileName = subJobOutputs.Results[i].Path
408+
fileName = subJobOutputResults[i].Path
399409
}
400410

401411
fileName = path.Join(savePath, fileName)
@@ -446,7 +456,25 @@ func getSubJobOutput(savePath string, subJob *types.SubJob, fetchData bool) []ty
446456
}
447457
}
448458

449-
return subJobOutputs.Results
459+
return subJobOutputResults
460+
}
461+
462+
func filterSubJobOutputsByFileNames(outputs []types.SubJobOutput, fileNames []string) []types.SubJobOutput {
463+
if fileNames == nil {
464+
return outputs
465+
}
466+
467+
var matchingOutputs []types.SubJobOutput
468+
for _, output := range outputs {
469+
for _, fileName := range fileNames {
470+
if output.FileName == fileName {
471+
matchingOutputs = append(matchingOutputs, output)
472+
break
473+
}
474+
}
475+
}
476+
477+
return matchingOutputs
450478
}
451479

452480
func GetRunByID(id uuid.UUID) *types.Run {

0 commit comments

Comments
 (0)