Skip to content

Commit

Permalink
Break up CLI into multiple files (#105)
Browse files Browse the repository at this point in the history
Currently the CLI is a single, huge file containing all commands and utility functions. This PR breaks it up into multiple files, in which each command gets its own file. This should make the cli more readable and understandable.
  • Loading branch information
erwinvaneyk authored Feb 12, 2018
1 parent 4c58a0f commit 59e4bc5
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 328 deletions.
10 changes: 9 additions & 1 deletion build/env/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ ENV ES_NATS_URL nats://[email protected]:4222
ENV ES_NATS_CLUSTER fissionMQTrigger

# Remove APIs when components stabilize
ENTRYPOINT ["/fission-workflows-bundle", "--nats", "--fission", "--internal", "--controller", "--api-http", "--api-workflow-invocation", "--api-workflow", "--api-admin"]
ENTRYPOINT ["/fission-workflows-bundle", \
"--nats", \
"--fission", \
"--internal", \
"--controller", \
"--api-http", \
"--api-workflow-invocation", \
"--api-workflow", \
"--api-admin"]
2 changes: 1 addition & 1 deletion cmd/wfcli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wfcli status # View whether the Fission Workflows deployment can be reached.

wfcli workflow get # List all workflows in the workflow engine.

wfcli workflow get <id> # Get the definition of a specifc workflow
wfcli workflow get <id> # Get the definition of a specific workflow

wfcli invocation get # List all invocations so-far (both in-progress and finished)

Expand Down
22 changes: 22 additions & 0 deletions cmd/wfcli/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/urfave/cli"
)

var cmdConfig = cli.Command{
Name: "config",
Usage: "Print wfcli config",
Action: func(c *cli.Context) error {
fmt.Println("cli:")
for _, flag := range c.GlobalFlagNames() {
fmt.Printf(" %s: %v\n", flag, c.GlobalGeneric(flag))
}
for _, flag := range c.FlagNames() {
fmt.Printf(" %s: %v\n", flag, c.Generic(flag))
}
return nil
},
}
160 changes: 160 additions & 0 deletions cmd/wfcli/invocation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package main

import (
"fmt"
"os"
"sort"

"github.com/fission/fission-workflows/cmd/wfcli/swagger-client/client/workflow_api"
"github.com/fission/fission-workflows/cmd/wfcli/swagger-client/client/workflow_invocation_api"
"github.com/fission/fission-workflows/cmd/wfcli/swagger-client/models"
"github.com/fission/fission-workflows/pkg/api/workflow/parse/yaml"
"github.com/fission/fission-workflows/pkg/types"
"github.com/go-openapi/strfmt"
"github.com/urfave/cli"
)

var cmdInvocation = cli.Command{
Name: "invocation",
Aliases: []string{"wi", "invocations", "workflow-invocation", "wfi"},
Usage: "Workflow Invocation-related commands",
Subcommands: []cli.Command{
{
Name: "get",
Usage: "get <workflow-invocation-id> <task-invocation-id>",
Action: func(c *cli.Context) error {
u := parseUrl(c.GlobalString("url"))
client := createTransportClient(u)
wfiApi := workflow_invocation_api.New(client, strfmt.Default)
switch c.NArg() {
case 0:
// List workflows invocations
resp, err := wfiApi.List(workflow_invocation_api.NewListParams())
if err != nil {
panic(err)
}
wis := resp.Payload
sort.Strings(wis.Invocations)
var rows [][]string
for _, wfiId := range wis.Invocations {
resp, err := wfiApi.Get(workflow_invocation_api.NewGetParams().WithID(wfiId))
if err != nil {
panic(err)
}
wi := resp.Payload
updated := wi.Status.UpdatedAt.String()
created := wi.Metadata.CreatedAt.String()

rows = append(rows, []string{wfiId, wi.Spec.WorkflowID, string(wi.Status.Status),
created, updated})
}
table(os.Stdout, []string{"ID", "WORKFLOW", "STATUS", "CREATED", "UPDATED"}, rows)
case 1:
// Get Workflow invocation
wfiId := c.Args().Get(0)
resp, err := wfiApi.Get(workflow_invocation_api.NewGetParams().WithID(wfiId))
if err != nil {
panic(err)
}
wfi := resp.Payload
b, err := yaml.Marshal(wfi)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", string(b))
case 2:
fallthrough
default:
wfiId := c.Args().Get(0)
taskId := c.Args().Get(1)
resp, err := wfiApi.Get(workflow_invocation_api.NewGetParams().WithID(wfiId))
if err != nil {
panic(err)
}
wfi := resp.Payload
ti, ok := wfi.Status.Tasks[taskId]
if !ok {
fmt.Println("Task invocation not found.")
return nil
}
b, err := yaml.Marshal(ti)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", string(b))
}

return nil
},
},
{
Name: "status",
Usage: "status <workflow-invocation-id> ",
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
fmt.Println("Need workflow invocation id")
return nil
}
wfiId := c.Args().Get(0)
u := parseUrl(c.GlobalString("url"))
client := createTransportClient(u)
wfApi := workflow_api.New(client, strfmt.Default)
wfiApi := workflow_invocation_api.New(client, strfmt.Default)

wfiResp, err := wfiApi.Get(workflow_invocation_api.NewGetParams().WithID(wfiId))
if err != nil {
panic(err)
}
wfi := wfiResp.Payload

wfResp, err := wfApi.Get0(workflow_api.NewGet0Params().WithID(wfi.Spec.WorkflowID))
if err != nil {
panic(err)
}
wf := wfResp.Payload

wfiUpdated := wfi.Status.UpdatedAt.String()
wfiCreated := wfi.Metadata.CreatedAt.String()
table(os.Stdout, nil, [][]string{
{"ID", wfi.Metadata.ID},
{"WORKFLOW_ID", wfi.Spec.WorkflowID},
{"CREATED", wfiCreated},
{"UPDATED", wfiUpdated},
{"STATUS", string(wfi.Status.Status)},
})
fmt.Println()

var rows [][]string
rows = collectStatus(wf.Spec.Tasks, wfi.Status.Tasks, rows)
rows = collectStatus(wfi.Status.DynamicTasks, wfi.Status.Tasks, rows)

table(os.Stdout, []string{"TASK", "STATUS", "STARTED", "UPDATED"}, rows)
return nil
},
},
},
}

func collectStatus(tasks map[string]models.Task, taskStatus map[string]models.TaskInvocation, rows [][]string) [][]string {
var ids []string
for id := range tasks {
ids = append(ids, id)
}
sort.Strings(ids)

for _, id := range ids {
status := types.TaskInvocationStatus_SCHEDULED.String()
updated := ""
started := ""

taskStatus, ok := taskStatus[id]
if ok {
status = string(taskStatus.Status.Status)
started = taskStatus.Metadata.CreatedAt.String()
updated = taskStatus.Metadata.CreatedAt.String()
}

rows = append(rows, []string{id, status, started, updated})
}
return rows
}
Loading

0 comments on commit 59e4bc5

Please sign in to comment.