github.com/gfleury/gobbs@v0.0.0-20200831213239-44ca2b94c1a1/repos/create.go (about)

     1  package repos
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net"
     8  	"net/http"
     9  
    10  	"github.com/gfleury/gobbs/common"
    11  	"github.com/gfleury/gobbs/common/log"
    12  
    13  	bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var (
    18  	forkable, private *bool
    19  )
    20  
    21  func init() {
    22  	forkable = Create.Flags().BoolP("forkable", "f", true, "Is repository forkable")
    23  	private = Create.Flags().BoolP("private", "j", false, "Is repository private")
    24  }
    25  
    26  // Create is the cmd implementation for Creating Pull Requests
    27  var Create = &cobra.Command{
    28  	Use:     "create repoName",
    29  	Aliases: []string{"cr"},
    30  	Short:   "Create repository",
    31  	Args:    cobra.MinimumNArgs(1),
    32  	RunE: func(cmd *cobra.Command, args []string) error {
    33  		apiClient, cancel, err := common.APIClient(cmd)
    34  		defer cancel()
    35  
    36  		if err != nil {
    37  			cmd.SilenceUsage = true
    38  			return err
    39  		}
    40  
    41  		stashInfo := cmd.Context().Value(common.StashInfoKey).(*common.StashInfo)
    42  		err = mustHaveProject(stashInfo)
    43  		if err != nil {
    44  			return err
    45  		}
    46  
    47  		repository := bitbucketv1.Repository{
    48  			Name:     args[0],
    49  			Forkable: *forkable,
    50  			Public:   !*private,
    51  		}
    52  
    53  		response, err := apiClient.DefaultApi.CreateRepository(*stashInfo.Project(), repository)
    54  
    55  		if netError, ok := err.(net.Error); (!ok || (ok && !netError.Timeout())) &&
    56  			!errors.Is(err, context.Canceled) &&
    57  			!errors.Is(err, context.DeadlineExceeded) &&
    58  			response != nil && response.Response != nil &&
    59  			response.Response.StatusCode >= http.StatusMultipleChoices {
    60  			common.PrintApiError(response.Values)
    61  			cmd.SilenceUsage = true
    62  			log.Debugf(err.Error())
    63  			return fmt.Errorf("Unable to process request, API Error")
    64  		} else if err != nil {
    65  			cmd.SilenceUsage = true
    66  			return err
    67  		}
    68  
    69  		repo, err := bitbucketv1.GetRepositoryResponse(response)
    70  		if err != nil {
    71  			cmd.SilenceUsage = true
    72  			return err
    73  		}
    74  
    75  		header := []string{"ID", "Slug", "Links", "Status"}
    76  		table := common.Table(header)
    77  
    78  		table.Append([]string{
    79  			fmt.Sprintf("%d", repo.ID),
    80  			repo.Slug,
    81  			func() (r string) {
    82  				if repo.Links != nil {
    83  					for _, link := range repo.Links.Clone {
    84  						r = fmt.Sprintf("%s%s: %s\n", r, link.Name, link.Href)
    85  					}
    86  				}
    87  				return
    88  			}(),
    89  			repo.StatusMessage,
    90  		})
    91  		table.Render()
    92  
    93  		return nil
    94  	},
    95  }