1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
package enable
import (
"errors"
"fmt"
"net/http"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/workflow/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
)
type EnableOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Prompter iprompter
Selector string
Prompt bool
}
type iprompter interface {
Select(string, string, []string) (int, error)
}
func NewCmdEnable(f *cmdutil.Factory, runF func(*EnableOptions) error) *cobra.Command {
opts := &EnableOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Prompter: f.Prompter,
}
cmd := &cobra.Command{
Use: "enable [<workflow-id> | <workflow-name>]",
Short: "Enable a workflow",
Long: "Enable a workflow, allowing it to be run and show up when listing workflows.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
if len(args) > 0 {
opts.Selector = args[0]
} else if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("workflow ID or name required when not running interactively")
} else {
opts.Prompt = true
}
if runF != nil {
return runF(opts)
}
return runEnable(opts)
},
}
return cmd
}
func runEnable(opts *EnableOptions) error {
c, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("could not build http client: %w", err)
}
client := api.NewClientFromHTTP(c)
repo, err := opts.BaseRepo()
if err != nil {
return err
}
states := []shared.WorkflowState{shared.DisabledManually, shared.DisabledInactivity}
workflow, err := shared.ResolveWorkflow(opts.Prompter,
opts.IO, client, repo, opts.Prompt, opts.Selector, states)
if err != nil {
var fae shared.FilteredAllError
if errors.As(err, &fae) {
return errors.New("there are no disabled workflows to enable")
}
return err
}
path := fmt.Sprintf("repos/%s/actions/workflows/%d/enable", ghrepo.FullName(repo), workflow.ID)
err = client.REST(repo.RepoHost(), "PUT", path, nil, nil)
if err != nil {
return fmt.Errorf("failed to enable workflow: %w", err)
}
if opts.IO.CanPrompt() {
cs := opts.IO.ColorScheme()
fmt.Fprintf(opts.IO.Out, "%s Enabled %s\n", cs.SuccessIcon(), cs.Bold(workflow.Name))
}
return nil
}
|