diff --git a/app/cli/cmd/workflow_create.go b/app/cli/cmd/workflow_create.go index a73aaed49..d05491154 100644 --- a/app/cli/cmd/workflow_create.go +++ b/app/cli/cmd/workflow_create.go @@ -25,6 +25,7 @@ import ( func newWorkflowCreateCmd() *cobra.Command { var workflowName, project, team, contract string + var skipRACreate bool cmd := &cobra.Command{ Use: "create", @@ -59,12 +60,41 @@ func newWorkflowCreateCmd() *cobra.Command { Name: workflowName, Team: team, Project: project, ContractID: contract, } - res, err := action.NewWorkflowCreate(actionOpts).Run(opts) + workflow, err := action.NewWorkflowCreate(actionOpts).Run(opts) if err != nil { return err } - return encodeOutput([]*action.WorkflowItem{res}, WorkflowListTableOutput) + workflowRA := &action.WorkflowItemWithRobotAccount{ + WorkflowItem: workflow, + } + + // If the robot account creation is not skipped via --skip-robot-account-create , create a new one + if !skipRACreate { + ra, errRA := action.NewWorkflowRobotAccountCreate(actionOpts).Run(workflow.ID, "autogenerated") + if errRA != nil { + return errRA + } + workflowRA.RobotAccountKey = ra.Key + workflowRA.RobotAccountID = ra.ID + } + + // Print the workflow JSON + if flagOutputFormat == formatJSON { + return encodeJSON(workflowRA) + } + + // Print the workflow table + if err := encodeOutput([]*action.WorkflowItem{workflowRA.WorkflowItem}, WorkflowListTableOutput); err != nil { + return err + } + + // Print the Robot Account Token + if workflowRA.RobotAccountID != "" { + fmt.Printf("\nThis is automatically generated Robot Account Token (ID: %s). Save the following token since it will not printed again: \n\n %s\n\n", workflowRA.RobotAccountID, workflowRA.RobotAccountKey) + } + + return nil }, } @@ -79,6 +109,10 @@ func newWorkflowCreateCmd() *cobra.Command { cmd.Flags().StringVar(&team, "team", "", "team name") cmd.Flags().StringVar(&contract, "contract", "", "the ID of an existing contract or the path/URL to a contract file. If not provided an empty one will be created.") + cmd.Flags().BoolVarP(&skipRACreate, "skip-robot-account-create", "s", false, "Skip creating a Robot Account for this workflow.") + + cmd.Flags().SortFlags = false + return cmd } diff --git a/app/cli/internal/action/workflow_list.go b/app/cli/internal/action/workflow_list.go index d7b309c34..d235cae4c 100644 --- a/app/cli/internal/action/workflow_list.go +++ b/app/cli/internal/action/workflow_list.go @@ -38,6 +38,12 @@ type WorkflowItem struct { LastRun *WorkflowRunItem `json:"lastRun,omitempty"` } +type WorkflowItemWithRobotAccount struct { + *WorkflowItem + RobotAccountID string `json:"robotAccountID,omitempty"` + RobotAccountKey string `json:"robotAccountKey,omitempty"` +} + func NewWorkflowList(cfg *ActionsOpts) *WorkflowList { return &WorkflowList{cfg} }