Skip to content

Commit c127bf9

Browse files
Merge pull request #74 from ctrlplanedev/trigger-workflow-from-slug
feat: trigger workflow by slug
2 parents aa9175a + f7e07f1 commit c127bf9

4 files changed

Lines changed: 418 additions & 14 deletions

File tree

‎cmd/ctrlc/root/workflow/get.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package workflow
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ctrlplanedev/cli/internal/api"
7+
"github.com/ctrlplanedev/cli/internal/cliutil"
8+
"github.com/google/uuid"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
)
12+
13+
func NewGetCmd() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "get <id-or-slug>",
16+
Short: "Get a workflow by UUID or slug",
17+
Long: `Get a single workflow. The argument is auto-detected as a UUID or slug.`,
18+
Args: cobra.ExactArgs(1),
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
identifier := args[0]
21+
22+
client, err := api.NewAPIKeyClientWithResponses(viper.GetString("url"), viper.GetString("api-key"))
23+
if err != nil {
24+
return fmt.Errorf("failed to create API client: %w", err)
25+
}
26+
27+
workspaceID, err := client.GetWorkspaceID(cmd.Context(), viper.GetString("workspace"))
28+
if err != nil {
29+
return err
30+
}
31+
32+
if _, err := uuid.Parse(identifier); err == nil {
33+
resp, err := client.GetWorkflow(cmd.Context(), workspaceID.String(), identifier)
34+
if err != nil {
35+
return fmt.Errorf("failed to get workflow: %w", err)
36+
}
37+
return cliutil.HandleResponseOutput(cmd, resp)
38+
}
39+
40+
resp, err := client.GetWorkflowBySlug(cmd.Context(), workspaceID.String(), identifier)
41+
if err != nil {
42+
return fmt.Errorf("failed to get workflow by slug: %w", err)
43+
}
44+
return cliutil.HandleResponseOutput(cmd, resp)
45+
},
46+
}
47+
48+
return cmd
49+
}

‎cmd/ctrlc/root/workflow/trigger.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/ctrlplanedev/cli/internal/api"
88
"github.com/ctrlplanedev/cli/internal/cliutil"
9+
"github.com/google/uuid"
910
"github.com/spf13/cobra"
1011
"github.com/spf13/viper"
1112
)
@@ -14,23 +15,19 @@ func NewTriggerCmd() *cobra.Command {
1415
var inputFlags []string
1516

1617
cmd := &cobra.Command{
17-
Use: "trigger <workflow-id>",
18+
Use: "trigger <id-or-slug>",
1819
Short: "Trigger a workflow run",
19-
Long: `Trigger a workflow run with the given inputs.`,
20+
Long: `Trigger a workflow run with the given inputs. The argument is auto-detected as a UUID or slug.`,
2021
Args: cobra.ExactArgs(1),
2122
RunE: func(cmd *cobra.Command, args []string) error {
22-
workflowID := args[0]
23+
identifier := args[0]
2324

24-
apiURL := viper.GetString("url")
25-
apiKey := viper.GetString("api-key")
26-
workspace := viper.GetString("workspace")
27-
28-
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
25+
client, err := api.NewAPIKeyClientWithResponses(viper.GetString("url"), viper.GetString("api-key"))
2926
if err != nil {
3027
return fmt.Errorf("failed to create API client: %w", err)
3128
}
3229

33-
workspaceID, err := client.GetWorkspaceID(cmd.Context(), workspace)
30+
workspaceID, err := client.GetWorkspaceID(cmd.Context(), viper.GetString("workspace"))
3431
if err != nil {
3532
return err
3633
}
@@ -47,15 +44,18 @@ func NewTriggerCmd() *cobra.Command {
4744
inputs[key] = value
4845
}
4946

50-
body := api.CreateWorkflowRunJSONRequestBody{
51-
Inputs: inputs,
47+
if _, err := uuid.Parse(identifier); err == nil {
48+
resp, err := client.CreateWorkflowRun(cmd.Context(), workspaceID.String(), identifier, api.CreateWorkflowRunJSONRequestBody{Inputs: inputs})
49+
if err != nil {
50+
return fmt.Errorf("failed to trigger workflow: %w", err)
51+
}
52+
return cliutil.HandleResponseOutput(cmd, resp)
5253
}
5354

54-
resp, err := client.CreateWorkflowRun(cmd.Context(), workspaceID.String(), workflowID, body)
55+
resp, err := client.CreateWorkflowRunBySlug(cmd.Context(), workspaceID.String(), identifier, api.CreateWorkflowRunBySlugJSONRequestBody{Inputs: inputs})
5556
if err != nil {
56-
return fmt.Errorf("failed to trigger workflow: %w", err)
57+
return fmt.Errorf("failed to trigger workflow by slug: %w", err)
5758
}
58-
5959
return cliutil.HandleResponseOutput(cmd, resp)
6060
},
6161
}

‎cmd/ctrlc/root/workflow/workflow.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func NewWorkflowCmd() *cobra.Command {
1414
},
1515
}
1616

17+
cmd.AddCommand(NewGetCmd())
1718
cmd.AddCommand(NewListCmd())
1819
cmd.AddCommand(NewTriggerCmd())
1920

0 commit comments

Comments
 (0)