|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "os" |
6 | | - |
7 | | - "github.com/deiwin/interact" |
| 4 | + "github.com/fatih/color" |
8 | 5 | "github.com/hellofresh/github-cli/pkg/pullapprove" |
9 | 6 | "github.com/hellofresh/github-cli/pkg/repo" |
10 | | - log "github.com/sirupsen/logrus" |
| 7 | + "github.com/pkg/errors" |
11 | 8 | "github.com/spf13/cobra" |
12 | 9 | ) |
13 | 10 |
|
14 | 11 | type ( |
15 | 12 | // CreateRepoFlags are the flags for the create repository command |
16 | 13 | CreateRepoFlags struct { |
| 14 | + Name string |
| 15 | + Description string |
| 16 | + Org string |
17 | 17 | Private bool |
18 | 18 | HasPullApprove bool |
19 | 19 | HasTeams bool |
| 20 | + HasCollaborators bool |
20 | 21 | HasLabels bool |
21 | 22 | HasDefaultLabels bool |
22 | 23 | HasWebhooks bool |
23 | 24 | HasBranchProtections bool |
24 | 25 | } |
25 | 26 | ) |
26 | 27 |
|
27 | | -var ( |
28 | | - createRepoFlags CreateRepoFlags |
29 | | - checkNotEmpty = func(input string) error { |
30 | | - // note that the inputs provided to these checks are already trimmed |
31 | | - if input == "" { |
32 | | - return errors.New("input should not be empty") |
33 | | - } |
34 | | - return nil |
35 | | - } |
36 | | -) |
| 28 | +var createRepoFlags CreateRepoFlags |
| 29 | + |
| 30 | +func init() { |
| 31 | + repoCmd.AddCommand(createRepoCmd) |
| 32 | + |
| 33 | + createRepoCmd.Flags().StringVarP(&createRepoFlags.Name, "name", "n", "", "The name of the repository") |
| 34 | + createRepoCmd.Flags().StringVarP(&createRepoFlags.Description, "description", "d", "", "The repository's description") |
| 35 | + createRepoCmd.Flags().StringVarP(&createRepoFlags.Org, "organization", "o", "", "Github's organization") |
| 36 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.Private, "private", true, "Is the repository private?") |
| 37 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.HasPullApprove, "add-pullapprove", true, "Enables pull approve") |
| 38 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.HasTeams, "add-teams", true, "Enable teams") |
| 39 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.HasLabels, "add-labels", true, "Enable labels") |
| 40 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.HasDefaultLabels, "add-default-labels", true, "Removes the default github labels") |
| 41 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.HasWebhooks, "add-webhooks", true, "Enables webhooks configurations") |
| 42 | + createRepoCmd.Flags().BoolVar(&createRepoFlags.HasBranchProtections, "add-branch-protections", true, "Enables branch protections") |
| 43 | +} |
37 | 44 |
|
38 | 45 | // RunCreateRepo runs the command to create a new repository |
39 | 46 | func RunCreateRepo(cmd *cobra.Command, args []string) { |
40 | 47 | var err error |
41 | 48 |
|
42 | | - actor := interact.NewActor(os.Stdin, os.Stdout) |
43 | | - repoName, err := actor.Prompt("Please enter the repository name", checkNotEmpty) |
44 | | - if err != nil { |
45 | | - log.Fatal(err) |
46 | | - } |
| 49 | + name := createRepoFlags.Name |
| 50 | + checkEmpty(name, "Please provide a repository name") |
| 51 | + |
| 52 | + description := createRepoFlags.Description |
47 | 53 |
|
48 | | - org, err := actor.PromptOptional("Please enter the org name", globalConfig.Github.Organization, checkNotEmpty) |
49 | | - if err != nil { |
50 | | - log.Fatal(err) |
| 54 | + org := createRepoFlags.Org |
| 55 | + if org == "" { |
| 56 | + org = globalConfig.Github.Organization |
51 | 57 | } |
| 58 | + checkEmpty(org, "Please provide an organization") |
52 | 59 |
|
53 | | - opts := &repo.HelloFreshRepoOpt{ |
54 | | - Name: repoName, |
55 | | - Org: org, |
56 | | - Private: createRepoFlags.Private, |
57 | | - PullApprove: &repo.PullApproveRule{ |
58 | | - Enabled: createRepoFlags.HasPullApprove, |
| 60 | + opts := &repo.GithubRepoOpts{ |
| 61 | + PullApprove: &repo.PullApproveOpts{ |
59 | 62 | Filename: globalConfig.PullApprove.Filename, |
60 | 63 | ProtectedBranchName: globalConfig.PullApprove.ProtectedBranchName, |
61 | 64 | Client: pullapprove.New(globalConfig.PullApprove.Token), |
62 | 65 | }, |
63 | | - Labels: &repo.LabelsRule{ |
64 | | - Enabled: createRepoFlags.HasLabels, |
| 66 | + Labels: &repo.LabelsOpts{ |
65 | 67 | RemoveDefaultLabels: globalConfig.Github.RemoveDefaultLabels, |
66 | 68 | Labels: globalConfig.Github.Labels, |
67 | 69 | }, |
68 | | - Teams: &repo.TeamsRule{ |
69 | | - Enabled: createRepoFlags.HasTeams, |
70 | | - Teams: globalConfig.Github.Teams, |
| 70 | + Teams: &repo.TeamsOpts{ |
| 71 | + Teams: globalConfig.Github.Teams, |
71 | 72 | }, |
72 | | - Webhooks: &repo.WebhooksRule{ |
73 | | - Enabled: createRepoFlags.HasWebhooks, |
| 73 | + Webhooks: &repo.WebhooksOpts{ |
74 | 74 | Webhooks: globalConfig.Github.Webhooks, |
75 | 75 | }, |
76 | | - BranchProtections: &repo.BranchProtectionsRule{ |
77 | | - Enabled: createRepoFlags.HasBranchProtections, |
| 76 | + BranchProtections: &repo.BranchProtectionsOpts{ |
78 | 77 | Protections: globalConfig.Github.Protections, |
79 | 78 | }, |
80 | 79 | } |
81 | 80 |
|
82 | 81 | creator := repo.NewGithub(githubClient) |
83 | | - err = creator.Create(opts) |
84 | | - if err != nil { |
85 | | - log.WithError(err).Fatal("Could not create github repo") |
| 82 | + |
| 83 | + color.White("Creating repository...") |
| 84 | + err = creator.CreateRepo(name, description, org, createRepoFlags.Private) |
| 85 | + checkEmpty(errors.Wrap(err, "could not create repository"), "") |
| 86 | + |
| 87 | + if createRepoFlags.HasPullApprove { |
| 88 | + color.White("Adding pull approve...") |
| 89 | + err = creator.AddPullApprove(name, org, opts.PullApprove) |
| 90 | + checkEmpty(errors.Wrap(err, "could not add pull approve"), "") |
86 | 91 | } |
| 92 | + |
| 93 | + if createRepoFlags.HasTeams { |
| 94 | + color.White("Adding teams to repository...") |
| 95 | + err = creator.AddTeamsToRepo(name, org, opts.Teams) |
| 96 | + checkEmpty(errors.Wrap(err, "could add teams to repository"), "") |
| 97 | + } |
| 98 | + |
| 99 | + if createRepoFlags.HasCollaborators { |
| 100 | + color.White("Adding collaborators to repository...") |
| 101 | + err = creator.AddCollaborators(name, org, opts.Collaborators) |
| 102 | + checkEmpty(errors.Wrap(err, "could not add collaborators to repository"), "") |
| 103 | + } |
| 104 | + |
| 105 | + if createRepoFlags.HasLabels { |
| 106 | + color.White("Adding labels to repository...") |
| 107 | + err = creator.AddLabelsToRepo(name, org, opts.Labels) |
| 108 | + checkEmpty(errors.Wrap(err, "could add labels to repository"), "") |
| 109 | + } |
| 110 | + |
| 111 | + if createRepoFlags.HasWebhooks { |
| 112 | + color.White("Adding webhooks to repository...") |
| 113 | + err = creator.AddWebhooksToRepo(name, org, opts.Webhooks) |
| 114 | + checkEmpty(errors.Wrap(err, "could add webhooks to repository"), "") |
| 115 | + } |
| 116 | + |
| 117 | + if createRepoFlags.HasBranchProtections { |
| 118 | + color.White("Adding branch protections to repository...") |
| 119 | + err = creator.AddBranchProtections(name, org, opts.BranchProtections) |
| 120 | + checkEmpty(errors.Wrap(err, "could add branch protections to repository"), "") |
| 121 | + } |
| 122 | + |
| 123 | + color.Green("Repository created!") |
| 124 | + checkEmpty(err, "Could not create github repo") |
87 | 125 | } |
0 commit comments