Skip to content

Commit

Permalink
Merge pull request #94 from trickest/json-output
Browse files Browse the repository at this point in the history
Add support for JSON output
  • Loading branch information
mhmdiaa authored Nov 3, 2023
2 parents 478985a + 6a2fe0b commit 2798838
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 210 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ trickest list --space <space_name>
| Flag | Type | Default | Description |
|---------|--------|---------|-------------------------------------|
| --space | string | / | The name of the space to be listed |
| --json | boolean| / | Display output in JSON format |



Expand All @@ -87,6 +88,8 @@ trickest list --project <project_name> --space <space_name>
|-----------|--------|---------|----------------------------------------------------|
| --project | string | / | The name of the project to be listed. |
| --space | string | / | The name of the space to which the project belongs |
| --json | boolean | / | Display output in JSON format |


##### Note: When passing values that have spaces in their names (e.g. "Alpine Testing"), they need to be double-quoted.

Expand All @@ -105,6 +108,7 @@ trickest get --workflow <workflow_name> --space <space_name> [--watch]
| --workflow | string | / | The name of the workflow |
| --run | string | / | Get the status of a specific run |
| --watch | boolean | / | Option to track execution status in case workflow is in running state |
| --json | boolean | / | Display output in JSON format |

##### If the supplied workflow has a running execution, you can jump in and watch it running with the `--watch` flag!

Expand Down
4 changes: 2 additions & 2 deletions cmd/execute/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func copyWorkflow(destinationSpaceID, destinationProjectID, workflowID uuid.UUID
}

func updateWorkflow(workflow *types.Workflow, deleteProjectOnError bool) {
workflow.WorkflowCategory = nil
workflow.WorkflowCategory = ""
data, err := json.Marshal(workflow)
if err != nil {
fmt.Println("Error marshaling update workflow request!")
Expand All @@ -317,7 +317,7 @@ func updateWorkflow(workflow *types.Workflow, deleteProjectOnError bool) {

if resp.Status() != http.StatusOK {
if deleteProjectOnError {
delete.DeleteProject(workflow.ProjectInfo)
delete.DeleteProject(*workflow.ProjectInfo)
}
request.ProcessUnexpectedResponse(resp)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func createYAML(workflow *types.Workflow, destinationPath string) {
w := workflowExport{
Steps: make([]nodeExport, 0),
}
if workflow.WorkflowCategory != nil {
w.Category = &workflow.WorkflowCategory.Name
if workflow.WorkflowCategory != "" {
w.Category = &workflow.WorkflowCategory
}
w.Name = workflow.Name
version := execute.GetLatestWorkflowVersion(workflow.ID)
Expand Down
61 changes: 42 additions & 19 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package get

import (
"encoding/json"
"fmt"
"strings"
"time"
Expand All @@ -19,6 +20,7 @@ var (
watch bool
showNodeParams bool
runID string
jsonOutput bool
)

// GetCmd represents the get command
Expand Down Expand Up @@ -46,6 +48,11 @@ var GetCmd = &cobra.Command{
return
}

if workflow == nil {
fmt.Println("Error: You need to specify the full location of the workflow with the --space and --project flags")
return
}

version := execute.GetLatestWorkflowVersion(workflow.ID)
allNodes, roots := execute.CreateTrees(version, false)

Expand All @@ -61,32 +68,47 @@ var GetCmd = &cobra.Command{
run := execute.GetRunByID(runUUID)
runs = []types.Run{*run}
}
if runs != nil && len(runs) > 0 && (runs[0].Status == "RUNNING" || runs[0].Status == "COMPLETED") {
if len(runs) > 0 && (runs[0].Status == "RUNNING" || runs[0].Status == "COMPLETED") {
if runs[0].Status == "COMPLETED" && runs[0].CompletedDate.IsZero() {
runs[0].Status = "RUNNING"
}
execute.WatchRun(runs[0].ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Machines, showNodeParams)
if jsonOutput {
data, err := json.Marshal(runs[0])
if err != nil {
fmt.Println("Error marshalling project data")
return
}
output := string(data)
fmt.Println(output)
} else {
execute.WatchRun(*runs[0].ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Machines, showNodeParams)
}
return
} else {
const fmtStr = "%-15s %v\n"
out := ""
out += fmt.Sprintf(fmtStr, "Name:", workflow.Name)
status := "no runs"
if runs != nil && len(runs) > 0 {
status = strings.ToLower(runs[0].Status)
}
out += fmt.Sprintf(fmtStr, "Status:", status)
availableBees := execute.GetAvailableMachines()
out += fmt.Sprintf(fmtStr, "Max machines:", execute.FormatMachines(version.MaxMachines, true)+
" (currently available: "+execute.FormatMachines(availableBees, true)+")")
out += fmt.Sprintf(fmtStr, "Created:", workflow.CreatedDate.In(time.Local).Format(time.RFC1123)+
" ("+util.FormatDuration(time.Since(workflow.CreatedDate))+" ago)")
if jsonOutput {
// No runs
fmt.Println("{}")
} else {
const fmtStr = "%-15s %v\n"
out := ""
out += fmt.Sprintf(fmtStr, "Name:", workflow.Name)
status := "no runs"
if len(runs) > 0 {
status = strings.ToLower(runs[0].Status)
}
out += fmt.Sprintf(fmtStr, "Status:", status)
availableBees := execute.GetAvailableMachines()
out += fmt.Sprintf(fmtStr, "Max machines:", execute.FormatMachines(version.MaxMachines, true)+
" (currently available: "+execute.FormatMachines(availableBees, true)+")")
out += fmt.Sprintf(fmtStr, "Created:", workflow.CreatedDate.In(time.Local).Format(time.RFC1123)+
" ("+util.FormatDuration(time.Since(workflow.CreatedDate))+" ago)")

for _, node := range allNodes {
node.Status = ""
for _, node := range allNodes {
node.Status = ""
}
out += "\n" + execute.PrintTrees(roots, &allNodes, showNodeParams, false)
fmt.Print(out)
}
out += "\n" + execute.PrintTrees(roots, &allNodes, showNodeParams, false)
fmt.Print(out)
}

},
Expand All @@ -96,4 +118,5 @@ func init() {
GetCmd.Flags().BoolVar(&watch, "watch", false, "Watch the workflow execution if it's still running")
GetCmd.Flags().BoolVar(&showNodeParams, "show-params", false, "Show parameters in the workflow tree")
GetCmd.Flags().StringVar(&runID, "run", "", "Get the status of a specific run")
GetCmd.Flags().BoolVar(&jsonOutput, "json", false, "Display output in JSON format")
}
4 changes: 4 additions & 0 deletions cmd/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"github.com/spf13/cobra"
)

var (
jsonOutput bool
)

// LibraryCmd represents the library command
var LibraryCmd = &cobra.Command{
Use: "library",
Expand Down
30 changes: 22 additions & 8 deletions cmd/library/libraryListTools.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package library

import (
"encoding/json"
"fmt"
"math"
"strings"
Expand All @@ -19,7 +20,7 @@ var libraryListToolsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
tools := list.GetTools(math.MaxInt, "", "")
if len(tools) > 0 {
printTools(tools)
printTools(tools, jsonOutput)
} else {
fmt.Println("Couldn't find any tool in the library!")
}
Expand All @@ -28,15 +29,28 @@ var libraryListToolsCmd = &cobra.Command{

func init() {
libraryListCmd.AddCommand(libraryListToolsCmd)
libraryListToolsCmd.Flags().BoolVar(&jsonOutput, "json", false, "Display output in JSON format")
}

func printTools(tools []types.Tool) {
tree := treeprint.New()
tree.SetValue("Tools")
for _, tool := range tools {
branch := tree.AddBranch(tool.Name + " [" + strings.TrimPrefix(tool.SourceURL, "https://") + "]")
branch.AddNode("\U0001f4cb \033[3m" + tool.Description + "\033[0m") //📋
func printTools(tools []types.Tool, jsonOutput bool) {
var output string
if jsonOutput {
data, err := json.Marshal(tools)
if err != nil {
fmt.Println("Error marshalling project data")
return
}
output = string(data)
} else {
tree := treeprint.New()
tree.SetValue("Tools")
for _, tool := range tools {
branch := tree.AddBranch(tool.Name + " [" + strings.TrimPrefix(tool.SourceURL, "https://") + "]")
branch.AddNode("\U0001f4cb \033[3m" + tool.Description + "\033[0m") //📋
}

output = tree.String()
}

fmt.Println(tree.String())
fmt.Println(output)
}
34 changes: 24 additions & 10 deletions cmd/library/libraryListWorkflows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package library

import (
"encoding/json"
"fmt"

"github.com/google/uuid"
Expand All @@ -18,7 +19,7 @@ var libraryListWorkflowsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
workflows := list.GetWorkflows(uuid.Nil, uuid.Nil, "", true)
if len(workflows) > 0 {
printWorkflows(workflows)
printWorkflows(workflows, jsonOutput)
} else {
fmt.Println("Couldn't find any workflow in the library!")
}
Expand All @@ -27,17 +28,30 @@ var libraryListWorkflowsCmd = &cobra.Command{

func init() {
libraryListCmd.AddCommand(libraryListWorkflowsCmd)
libraryListWorkflowsCmd.Flags().BoolVar(&jsonOutput, "json", false, "Display output in JSON format")
}

func printWorkflows(workflows []types.WorkflowListResponse) {
tree := treeprint.New()
tree.SetValue("Workflows")
for _, workflow := range workflows {
wfSubBranch := tree.AddBranch("\U0001f9be " + workflow.Name) //🦾
if workflow.Description != "" {
wfSubBranch.AddNode("\U0001f4cb \033[3m" + workflow.Description + "\033[0m") //📋
func printWorkflows(workflows []types.Workflow, jsonOutput bool) {
var output string

if jsonOutput {
data, err := json.Marshal(workflows)
if err != nil {
fmt.Println("Error marshalling project data")
return
}
output = string(data)
} else {
tree := treeprint.New()
tree.SetValue("Workflows")
for _, workflow := range workflows {
wfSubBranch := tree.AddBranch("\U0001f9be " + workflow.Name) //🦾
if workflow.Description != "" {
wfSubBranch.AddNode("\U0001f4cb \033[3m" + workflow.Description + "\033[0m") //📋
}
}
}

fmt.Println(tree.String())
output = tree.String()
}
fmt.Println(output)
}
32 changes: 24 additions & 8 deletions cmd/library/librarySearch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package library

import (
"encoding/json"
"fmt"
"math"

Expand All @@ -21,19 +22,34 @@ var librarySearchCmd = &cobra.Command{
}
tools := list.GetTools(math.MaxInt, search, "")
workflows := list.GetWorkflows(uuid.Nil, uuid.Nil, search, true)
if len(tools) > 0 {
printTools(tools)
if jsonOutput {
results := map[string]interface{}{
"tools": tools,
"workflows": workflows,
}
data, err := json.Marshal(results)
if err != nil {
fmt.Println("Error marshalling project data")
return
}
output := string(data)
fmt.Println(output)
} else {
fmt.Println("Couldn't find any tool in the library that matches the search!")
}
if len(workflows) > 0 {
printWorkflows(workflows)
} else {
fmt.Println("Couldn't find any workflow in the library that matches the search!")
if len(tools) > 0 {
printTools(tools, jsonOutput)
} else {
fmt.Println("Couldn't find any tool in the library that matches the search!")
}
if len(workflows) > 0 {
printWorkflows(workflows, jsonOutput)
} else {
fmt.Println("Couldn't find any workflow in the library that matches the search!")
}
}
},
}

func init() {
LibraryCmd.AddCommand(librarySearchCmd)
librarySearchCmd.Flags().BoolVar(&jsonOutput, "json", false, "Display output in JSON format")
}
Loading

0 comments on commit 2798838

Please sign in to comment.