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 }
0 commit comments