| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995 |
- package cmd
- import (
- "context"
- "encoding/json"
- "fmt"
- "os"
- "strings"
- "github.com/AlecAivazis/survey/v2"
- "github.com/olekukonko/tablewriter"
- "github.com/urfave/cli/v3"
- )
- // WorkflowCommand returns the workflow command
- func WorkflowCommand() *cli.Command {
- return &cli.Command{
- Name: "workflow",
- Usage: "Manage workflows",
- Description: `Manage ARP workflows. Workflows are automated processes defined by templates.
- Workflow templates define the structure of automated workflows as DAGs (Directed Acyclic Graphs).
- Workflow instances are running executions of templates.
- Workflow nodes represent individual steps in a running workflow.`,
- Commands: []*cli.Command{
- // WorkflowTemplate commands
- {
- Name: "template",
- Usage: "Manage workflow templates",
- Commands: []*cli.Command{
- {
- Name: "list",
- Aliases: []string{"ls"},
- Usage: "List all workflow templates",
- Flags: []cli.Flag{
- &cli.BoolFlag{
- Name: "json",
- Aliases: []string{"j"},
- Usage: "Output as JSON",
- },
- },
- Action: workflowTemplateList,
- },
- {
- Name: "get",
- Usage: "Get a workflow template by ID",
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Template ID",
- Required: true,
- },
- &cli.BoolFlag{
- Name: "json",
- Aliases: []string{"j"},
- Usage: "Output as JSON",
- },
- },
- Action: workflowTemplateGet,
- },
- {
- Name: "create",
- Usage: "Create a new workflow template",
- Action: workflowTemplateCreate,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "name",
- Aliases: []string{"n"},
- Usage: "Template name",
- },
- &cli.StringFlag{
- Name: "description",
- Aliases: []string{"d"},
- Usage: "Template description",
- },
- &cli.StringFlag{
- Name: "definition",
- Aliases: []string{"f"},
- Usage: "Workflow definition (JSON string or @filename)",
- },
- &cli.BoolFlag{
- Name: "active",
- Usage: "Set template as active",
- Value: true,
- },
- },
- },
- {
- Name: "update",
- Usage: "Update a workflow template",
- Action: workflowTemplateUpdate,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Template ID",
- Required: true,
- },
- &cli.StringFlag{
- Name: "name",
- Aliases: []string{"n"},
- Usage: "Template name",
- },
- &cli.StringFlag{
- Name: "description",
- Aliases: []string{"d"},
- Usage: "Template description",
- },
- &cli.StringFlag{
- Name: "definition",
- Aliases: []string{"f"},
- Usage: "Workflow definition (JSON string or @filename)",
- },
- &cli.BoolFlag{
- Name: "active",
- Usage: "Set template as active",
- },
- },
- },
- {
- Name: "delete",
- Usage: "Delete a workflow template",
- Action: workflowTemplateDelete,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Template ID",
- Required: true,
- },
- &cli.BoolFlag{
- Name: "yes",
- Aliases: []string{"y"},
- Usage: "Skip confirmation",
- },
- },
- },
- },
- },
- // WorkflowInstance commands
- {
- Name: "instance",
- Usage: "Manage workflow instances",
- Commands: []*cli.Command{
- {
- Name: "list",
- Aliases: []string{"ls"},
- Usage: "List all workflow instances",
- Flags: []cli.Flag{
- &cli.BoolFlag{
- Name: "json",
- Aliases: []string{"j"},
- Usage: "Output as JSON",
- },
- },
- Action: workflowInstanceList,
- },
- {
- Name: "get",
- Usage: "Get a workflow instance by ID",
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Instance ID",
- Required: true,
- },
- &cli.BoolFlag{
- Name: "json",
- Aliases: []string{"j"},
- Usage: "Output as JSON",
- },
- },
- Action: workflowInstanceGet,
- },
- {
- Name: "start",
- Usage: "Start a new workflow instance from a template",
- Action: workflowInstanceStart,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "template",
- Aliases: []string{"t"},
- Usage: "Template ID",
- Required: true,
- },
- &cli.StringFlag{
- Name: "service",
- Aliases: []string{"s"},
- Usage: "Service ID to associate with the workflow",
- },
- &cli.StringFlag{
- Name: "context",
- Aliases: []string{"c"},
- Usage: "Initial workflow context (JSON string)",
- },
- },
- },
- {
- Name: "cancel",
- Usage: "Cancel a running workflow instance",
- Action: workflowInstanceCancel,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Instance ID",
- Required: true,
- },
- },
- },
- },
- },
- // WorkflowNode commands
- {
- Name: "node",
- Usage: "Manage workflow nodes",
- Commands: []*cli.Command{
- {
- Name: "list",
- Aliases: []string{"ls"},
- Usage: "List all nodes for a workflow instance",
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "instance",
- Aliases: []string{"i"},
- Usage: "Instance ID",
- Required: true,
- },
- &cli.BoolFlag{
- Name: "json",
- Aliases: []string{"j"},
- Usage: "Output as JSON",
- },
- },
- Action: workflowNodeList,
- },
- {
- Name: "get",
- Usage: "Get a workflow node by ID",
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Node ID",
- Required: true,
- },
- &cli.BoolFlag{
- Name: "json",
- Aliases: []string{"j"},
- Usage: "Output as JSON",
- },
- },
- Action: workflowNodeGet,
- },
- {
- Name: "retry",
- Usage: "Retry a failed workflow node",
- Action: workflowNodeRetry,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "id",
- Aliases: []string{"i"},
- Usage: "Node ID",
- Required: true,
- },
- },
- },
- },
- },
- },
- }
- }
- // WorkflowTemplate represents a workflow template
- type WorkflowTemplate struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Description string `json:"description"`
- Definition string `json:"definition"`
- IsActive bool `json:"isActive"`
- CreatedBy *User `json:"createdBy"`
- CreatedAt string `json:"createdAt"`
- UpdatedAt string `json:"updatedAt"`
- }
- // WorkflowInstance represents a running workflow instance
- type WorkflowInstance struct {
- ID string `json:"id"`
- Template *WorkflowTemplate `json:"template"`
- Status string `json:"status"`
- Context string `json:"context"`
- Service *Service `json:"service"`
- CreatedAt string `json:"createdAt"`
- UpdatedAt string `json:"updatedAt"`
- CompletedAt *string `json:"completedAt"`
- }
- // WorkflowNode represents a node in a workflow instance
- type WorkflowNode struct {
- ID string `json:"id"`
- NodeKey string `json:"nodeKey"`
- NodeType string `json:"nodeType"`
- Status string `json:"status"`
- Task *Task `json:"task"`
- InputData string `json:"inputData"`
- OutputData string `json:"outputData"`
- RetryCount int `json:"retryCount"`
- CreatedAt string `json:"createdAt"`
- UpdatedAt string `json:"updatedAt"`
- StartedAt *string `json:"startedAt"`
- CompletedAt *string `json:"completedAt"`
- }
- // WorkflowTemplate CRUD operations
- func workflowTemplateList(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- query := `query WorkflowTemplates { workflowTemplates { id name description definition isActive createdBy { id email } createdAt updatedAt } }`
- resp, err := c.Query(query, nil)
- if err != nil {
- return err
- }
- var result struct {
- WorkflowTemplates []WorkflowTemplate `json:"workflowTemplates"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if cmd.Bool("json") {
- enc := json.NewEncoder(os.Stdout)
- enc.SetIndent("", " ")
- return enc.Encode(result.WorkflowTemplates)
- }
- if len(result.WorkflowTemplates) == 0 {
- fmt.Println("No workflow templates found.")
- return nil
- }
- table := tablewriter.NewWriter(os.Stdout)
- table.Header([]string{"ID", "Name", "Description", "Active", "Created By", "Created At"})
- for _, t := range result.WorkflowTemplates {
- desc := t.Description
- if len(desc) > 30 {
- desc = desc[:27] + "..."
- }
- createdBy := ""
- if t.CreatedBy != nil {
- createdBy = t.CreatedBy.Email
- }
- active := "yes"
- if !t.IsActive {
- active = "no"
- }
- table.Append([]string{t.ID, t.Name, desc, active, createdBy, t.CreatedAt})
- }
- table.Render()
- return nil
- }
- func workflowTemplateGet(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- query := `query WorkflowTemplate($id: ID!) { workflowTemplate(id: $id) { id name description definition isActive createdBy { id email } createdAt updatedAt } }`
- resp, err := c.Query(query, map[string]interface{}{"id": id})
- if err != nil {
- return err
- }
- var result struct {
- WorkflowTemplate *WorkflowTemplate `json:"workflowTemplate"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.WorkflowTemplate == nil {
- return fmt.Errorf("workflow template not found")
- }
- if cmd.Bool("json") {
- enc := json.NewEncoder(os.Stdout)
- enc.SetIndent("", " ")
- return enc.Encode(result.WorkflowTemplate)
- }
- t := result.WorkflowTemplate
- fmt.Printf("ID: %s\n", t.ID)
- fmt.Printf("Name: %s\n", t.Name)
- fmt.Printf("Description: %s\n", t.Description)
- fmt.Printf("Active: %v\n", t.IsActive)
- if t.CreatedBy != nil {
- fmt.Printf("Created By: %s\n", t.CreatedBy.Email)
- }
- fmt.Printf("Created At: %s\n", t.CreatedAt)
- fmt.Printf("Updated At: %s\n", t.UpdatedAt)
- fmt.Printf("\nDefinition:\n%s\n", t.Definition)
- return nil
- }
- func workflowTemplateCreate(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- name := cmd.String("name")
- description := cmd.String("description")
- definition := cmd.String("definition")
- isActive := cmd.Bool("active")
- if name == "" {
- prompt := &survey.Input{Message: "Template name:"}
- if err := survey.AskOne(prompt, &name, survey.WithValidator(survey.Required)); err != nil {
- return err
- }
- }
- if description == "" {
- prompt := &survey.Input{Message: "Description (optional):"}
- survey.AskOne(prompt, &description)
- }
- if definition == "" {
- prompt := &survey.Multiline{Message: "Workflow definition (JSON):"}
- if err := survey.AskOne(prompt, &definition, survey.WithValidator(survey.Required)); err != nil {
- return err
- }
- }
- // Handle file input for definition
- definition = ReadFileOrString(definition)
- mutation := `mutation CreateWorkflowTemplate($input: NewWorkflowTemplate!) { createWorkflowTemplate(input: $input) { id name description definition isActive createdBy { id email } createdAt updatedAt } }`
- input := map[string]interface{}{
- "name": name,
- "description": description,
- "definition": definition,
- "isActive": isActive,
- }
- resp, err := c.Mutation(mutation, map[string]interface{}{"input": input})
- if err != nil {
- return err
- }
- var result struct {
- CreateWorkflowTemplate *WorkflowTemplate `json:"createWorkflowTemplate"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.CreateWorkflowTemplate == nil {
- return fmt.Errorf("failed to create workflow template")
- }
- fmt.Printf("Workflow template created successfully!\n")
- fmt.Printf("ID: %s\n", result.CreateWorkflowTemplate.ID)
- fmt.Printf("Name: %s\n", result.CreateWorkflowTemplate.Name)
- return nil
- }
- func workflowTemplateUpdate(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- name := cmd.String("name")
- description := cmd.String("description")
- definition := cmd.String("definition")
- // Check if active flag was explicitly set
- var isActive *bool
- if cmd.IsSet("active") {
- val := cmd.Bool("active")
- isActive = &val
- }
- if name == "" && description == "" && definition == "" && isActive == nil {
- fmt.Println("No updates provided. Use flags to specify what to update.")
- return nil
- }
- input := make(map[string]interface{})
- if name != "" {
- input["name"] = name
- }
- if description != "" {
- input["description"] = description
- }
- if definition != "" {
- input["definition"] = ReadFileOrString(definition)
- }
- if isActive != nil {
- input["isActive"] = *isActive
- }
- mutation := `mutation UpdateWorkflowTemplate($id: ID!, $input: UpdateWorkflowTemplateInput!) { updateWorkflowTemplate(id: $id, input: $input) { id name description definition isActive createdAt updatedAt } }`
- resp, err := c.Mutation(mutation, map[string]interface{}{"id": id, "input": input})
- if err != nil {
- return err
- }
- var result struct {
- UpdateWorkflowTemplate *WorkflowTemplate `json:"updateWorkflowTemplate"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.UpdateWorkflowTemplate == nil {
- return fmt.Errorf("workflow template not found")
- }
- fmt.Printf("Workflow template updated successfully!\n")
- fmt.Printf("ID: %s\n", result.UpdateWorkflowTemplate.ID)
- fmt.Printf("Name: %s\n", result.UpdateWorkflowTemplate.Name)
- return nil
- }
- func workflowTemplateDelete(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- skipConfirm := cmd.Bool("yes")
- if !skipConfirm {
- confirm := false
- prompt := &survey.Confirm{
- Message: fmt.Sprintf("Are you sure you want to delete workflow template %s?", id),
- Default: false,
- }
- if err := survey.AskOne(prompt, &confirm); err != nil {
- return err
- }
- if !confirm {
- fmt.Println("Deletion cancelled.")
- return nil
- }
- }
- mutation := `mutation DeleteWorkflowTemplate($id: ID!) { deleteWorkflowTemplate(id: $id) }`
- resp, err := c.Mutation(mutation, map[string]interface{}{"id": id})
- if err != nil {
- return err
- }
- var result struct {
- DeleteWorkflowTemplate bool `json:"deleteWorkflowTemplate"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.DeleteWorkflowTemplate {
- fmt.Printf("Workflow template %s deleted successfully.\n", id)
- } else {
- fmt.Printf("Failed to delete workflow template %s.\n", id)
- }
- return nil
- }
- // WorkflowInstance operations
- func workflowInstanceList(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- query := `query WorkflowInstances { workflowInstances { id template { id name } status context service { id name } createdAt updatedAt completedAt } }`
- resp, err := c.Query(query, nil)
- if err != nil {
- return err
- }
- var result struct {
- WorkflowInstances []WorkflowInstance `json:"workflowInstances"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if cmd.Bool("json") {
- enc := json.NewEncoder(os.Stdout)
- enc.SetIndent("", " ")
- return enc.Encode(result.WorkflowInstances)
- }
- if len(result.WorkflowInstances) == 0 {
- fmt.Println("No workflow instances found.")
- return nil
- }
- table := tablewriter.NewWriter(os.Stdout)
- table.Header([]string{"ID", "Template", "Status", "Service", "Created At"})
- for _, i := range result.WorkflowInstances {
- templateName := ""
- if i.Template != nil {
- templateName = i.Template.Name
- }
- serviceName := ""
- if i.Service != nil {
- serviceName = i.Service.Name
- }
- table.Append([]string{i.ID, templateName, i.Status, serviceName, i.CreatedAt})
- }
- table.Render()
- return nil
- }
- func workflowInstanceGet(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- query := `query WorkflowInstance($id: ID!) { workflowInstance(id: $id) { id template { id name description } status context service { id name } createdAt updatedAt completedAt } }`
- resp, err := c.Query(query, map[string]interface{}{"id": id})
- if err != nil {
- return err
- }
- var result struct {
- WorkflowInstance *WorkflowInstance `json:"workflowInstance"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.WorkflowInstance == nil {
- return fmt.Errorf("workflow instance not found")
- }
- if cmd.Bool("json") {
- enc := json.NewEncoder(os.Stdout)
- enc.SetIndent("", " ")
- return enc.Encode(result.WorkflowInstance)
- }
- i := result.WorkflowInstance
- fmt.Printf("ID: %s\n", i.ID)
- if i.Template != nil {
- fmt.Printf("Template: %s (%s)\n", i.Template.Name, i.Template.ID)
- }
- fmt.Printf("Status: %s\n", i.Status)
- if i.Service != nil {
- fmt.Printf("Service: %s (%s)\n", i.Service.Name, i.Service.ID)
- }
- fmt.Printf("Created At: %s\n", i.CreatedAt)
- fmt.Printf("Updated At: %s\n", i.UpdatedAt)
- if i.CompletedAt != nil {
- fmt.Printf("Completed At: %s\n", *i.CompletedAt)
- }
- if i.Context != "" {
- fmt.Printf("\nContext:\n%s\n", i.Context)
- }
- return nil
- }
- func workflowInstanceStart(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- templateID := cmd.String("template")
- serviceID := cmd.String("service")
- contextJSON := cmd.String("context")
- mutation := `mutation StartWorkflow($templateId: ID!, $input: StartWorkflowInput!) { startWorkflow(templateId: $templateId, input: $input) { id template { id name } status context service { id name } createdAt } }`
- input := make(map[string]interface{})
- if serviceID != "" {
- input["serviceId"] = serviceID
- }
- if contextJSON != "" {
- input["context"] = contextJSON
- }
- resp, err := c.Mutation(mutation, map[string]interface{}{"templateId": templateID, "input": input})
- if err != nil {
- return err
- }
- var result struct {
- StartWorkflow *WorkflowInstance `json:"startWorkflow"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.StartWorkflow == nil {
- return fmt.Errorf("failed to start workflow")
- }
- fmt.Printf("Workflow started successfully!\n")
- fmt.Printf("Instance ID: %s\n", result.StartWorkflow.ID)
- if result.StartWorkflow.Template != nil {
- fmt.Printf("Template: %s\n", result.StartWorkflow.Template.Name)
- }
- fmt.Printf("Status: %s\n", result.StartWorkflow.Status)
- return nil
- }
- func workflowInstanceCancel(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- mutation := `mutation CancelWorkflow($id: ID!) { cancelWorkflow(id: $id) { id status completedAt } }`
- resp, err := c.Mutation(mutation, map[string]interface{}{"id": id})
- if err != nil {
- return err
- }
- var result struct {
- CancelWorkflow *WorkflowInstance `json:"cancelWorkflow"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.CancelWorkflow == nil {
- return fmt.Errorf("workflow instance not found")
- }
- fmt.Printf("Workflow cancelled successfully!\n")
- fmt.Printf("Instance ID: %s\n", result.CancelWorkflow.ID)
- fmt.Printf("Status: %s\n", result.CancelWorkflow.Status)
- return nil
- }
- // WorkflowNode operations
- func workflowNodeList(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- instanceID := cmd.String("instance")
- query := `query WorkflowInstance($id: ID!) { workflowInstance(id: $id) { id status nodes: workflowNodes { id nodeKey nodeType status task { id title } retryCount createdAt startedAt completedAt } } }`
- resp, err := c.Query(query, map[string]interface{}{"id": instanceID})
- if err != nil {
- return err
- }
- var result struct {
- WorkflowInstance *struct {
- ID string `json:"id"`
- Status string `json:"status"`
- Nodes []WorkflowNode `json:"nodes"`
- } `json:"workflowInstance"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.WorkflowInstance == nil {
- return fmt.Errorf("workflow instance not found")
- }
- if cmd.Bool("json") {
- enc := json.NewEncoder(os.Stdout)
- enc.SetIndent("", " ")
- return enc.Encode(result.WorkflowInstance.Nodes)
- }
- if len(result.WorkflowInstance.Nodes) == 0 {
- fmt.Println("No nodes found for this workflow instance.")
- return nil
- }
- table := tablewriter.NewWriter(os.Stdout)
- table.Header([]string{"ID", "Key", "Type", "Status", "Task", "Retries"})
- for _, n := range result.WorkflowInstance.Nodes {
- taskTitle := ""
- if n.Task != nil {
- taskTitle = n.Task.Title
- if len(taskTitle) > 30 {
- taskTitle = taskTitle[:27] + "..."
- }
- }
- table.Append([]string{n.ID, n.NodeKey, n.NodeType, n.Status, taskTitle, fmt.Sprintf("%d", n.RetryCount)})
- }
- table.Render()
- return nil
- }
- func workflowNodeGet(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- query := `query WorkflowNode($id: ID!) { workflowNode(id: $id) { id nodeKey nodeType status task { id title } inputData outputData retryCount createdAt updatedAt startedAt completedAt } }`
- // Note: This assumes a workflowNode query exists. If not, we need to fetch via instance
- // For now, let's use a workaround by fetching the instance and finding the node
- query = `query WorkflowInstances { workflowInstances { id nodes: workflowNodes { id nodeKey nodeType status task { id title content } inputData outputData retryCount createdAt updatedAt startedAt completedAt } } }`
- resp, err := c.Query(query, nil)
- if err != nil {
- return err
- }
- var result struct {
- WorkflowInstances []struct {
- ID string `json:"id"`
- Nodes []WorkflowNode `json:"nodes"`
- } `json:"workflowInstances"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- // Find the node
- var node *WorkflowNode
- for _, instance := range result.WorkflowInstances {
- for _, n := range instance.Nodes {
- if n.ID == id {
- node = &n
- break
- }
- }
- if node != nil {
- break
- }
- }
- if node == nil {
- return fmt.Errorf("workflow node not found")
- }
- if cmd.Bool("json") {
- enc := json.NewEncoder(os.Stdout)
- enc.SetIndent("", " ")
- return enc.Encode(node)
- }
- fmt.Printf("ID: %s\n", node.ID)
- fmt.Printf("Key: %s\n", node.NodeKey)
- fmt.Printf("Type: %s\n", node.NodeType)
- fmt.Printf("Status: %s\n", node.Status)
- fmt.Printf("Retry Count: %d\n", node.RetryCount)
- if node.Task != nil {
- fmt.Printf("Task: %s (%s)\n", node.Task.Title, node.Task.ID)
- }
- fmt.Printf("Created At: %s\n", node.CreatedAt)
- fmt.Printf("Updated At: %s\n", node.UpdatedAt)
- if node.StartedAt != nil {
- fmt.Printf("Started At: %s\n", *node.StartedAt)
- }
- if node.CompletedAt != nil {
- fmt.Printf("Completed At: %s\n", *node.CompletedAt)
- }
- if node.InputData != "" {
- fmt.Printf("\nInput Data:\n%s\n", node.InputData)
- }
- if node.OutputData != "" {
- fmt.Printf("\nOutput Data:\n%s\n", node.OutputData)
- }
- return nil
- }
- func workflowNodeRetry(ctx context.Context, cmd *cli.Command) error {
- c, cfg, err := GetClient(ctx, cmd)
- if err != nil {
- return err
- }
- if err := RequireAuth(cfg); err != nil {
- return err
- }
- id := cmd.String("id")
- mutation := `mutation RetryWorkflowNode($nodeId: ID!) { retryWorkflowNode(nodeId: $nodeId) { id nodeKey status retryCount } }`
- resp, err := c.Mutation(mutation, map[string]interface{}{"nodeId": id})
- if err != nil {
- return err
- }
- var result struct {
- RetryWorkflowNode *WorkflowNode `json:"retryWorkflowNode"`
- }
- if err := json.Unmarshal(resp.Data, &result); err != nil {
- return err
- }
- if result.RetryWorkflowNode == nil {
- return fmt.Errorf("workflow node not found")
- }
- fmt.Printf("Workflow node retry initiated!\n")
- fmt.Printf("Node ID: %s\n", result.RetryWorkflowNode.ID)
- fmt.Printf("Key: %s\n", result.RetryWorkflowNode.NodeKey)
- fmt.Printf("Status: %s\n", result.RetryWorkflowNode.Status)
- fmt.Printf("Retry Count: %d\n", result.RetryWorkflowNode.RetryCount)
- return nil
- }
- // ReadFileOrString reads file content if input starts with @, otherwise returns as-is
- func ReadFileOrString(input string) string {
- if strings.HasPrefix(input, "@") {
- filename := input[1:]
- content, err := os.ReadFile(filename)
- if err != nil {
- return input // Return original if file can't be read
- }
- return string(content)
- }
- return input
- }
|