func()

in pkg/cmd/serviceaccount/phases/workflow/runner.go [71:121]


func (r *runner) BindToCommand(cmd *cobra.Command, data RunData) {
	// Alter the command's help text
	if cmd.Short == "" {
		cmd.Short = fmt.Sprintf("%s a workload identity", cmd.Use)
	}
	if cmd.Long == "" {
		long := fmt.Sprintf("The \"%s\" command executes the following phases in order:", cmd.Use)

		// Add extra padding to align the phase names
		longest := 0
		for _, phase := range r.phases {
			if longest < len(phase.Name) {
				longest = len(phase.Name)
			}
		}
		for _, phase := range r.phases {
			paddingCount := longest - len(phase.Name)
			long += fmt.Sprintf("\n%s%s  %s", phase.Name, strings.Repeat(" ", paddingCount), phase.Description)
		}
		cmd.Long = long
	}

	// common flags between commands
	cmd.Flags().StringSliceVar(&r.skipPhases, "skip-phases", []string{}, "List of phases to skip")

	// add the phase command, enabling the user to specify the phase to run
	phaseCmd := &cobra.Command{
		Use:   "phase",
		Short: fmt.Sprintf("The \"phase\" command invokes a single phase of the %s workflow", cmd.Use),
	}
	for _, phase := range r.phases {
		// workaround: create a copy of the variable 'phase' so that each subcommand
		// gets its own 'phase' variable instead of sharing the iterator variable
		p := phase

		subcommand := &cobra.Command{
			Use:     p.Name,
			Aliases: p.Aliases,
			Short:   p.Description,
			RunE: func(c *cobra.Command, args []string) error {
				// only run this particular phase
				r.phases = []Phase{p}
				return r.Run(data)
			},
		}
		inheritsFlags(cmd.Flags(), subcommand.Flags(), p.Flags)
		phaseCmd.AddCommand(subcommand)
	}

	cmd.AddCommand(phaseCmd)
}