github.com/matthewdale/lab@v0.14.0/cmd/project_create.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/spf13/cobra" 9 "github.com/xanzy/go-gitlab" 10 "github.com/zaquestion/lab/internal/git" 11 lab "github.com/zaquestion/lab/internal/gitlab" 12 ) 13 14 // projectCreateCmd represents the create command 15 var projectCreateCmd = &cobra.Command{ 16 Use: "create [path]", 17 Short: "Create a new project on GitLab", 18 Long: `If no path or name is provided the name of the git repo working directory 19 20 path refers the path on gitlab including the full group/namespace/project. If no path or name is provided and the directory is a git repo the name of the current working directory will be used.`, 21 Args: cobra.MaximumNArgs(1), 22 Run: func(cmd *cobra.Command, args []string) { 23 var ( 24 name, _ = cmd.Flags().GetString("name") 25 desc, _ = cmd.Flags().GetString("description") 26 ) 27 path := determinePath(args, name) 28 if path == "" && name == "" { 29 log.Fatal("path or name must be set") 30 } 31 32 visibility := gitlab.InternalVisibility 33 switch { 34 case private: 35 visibility = gitlab.PrivateVisibility 36 case public: 37 visibility = gitlab.PublicVisibility 38 } 39 40 opts := gitlab.CreateProjectOptions{ 41 Path: gitlab.String(path), 42 Name: gitlab.String(name), 43 Description: gitlab.String(desc), 44 Visibility: &visibility, 45 ApprovalsBeforeMerge: gitlab.Int(0), 46 } 47 p, err := lab.ProjectCreate(&opts) 48 if err != nil { 49 log.Fatal(err) 50 } 51 if git.InsideGitRepo() { 52 err = git.RemoteAdd("origin", p.SSHURLToRepo, ".") 53 if err != nil { 54 log.Fatal(err) 55 } 56 } 57 fmt.Println(strings.TrimSuffix(p.HTTPURLToRepo, ".git")) 58 }, 59 } 60 61 func determinePath(args []string, name string) string { 62 var path string 63 if len(args) > 0 { 64 path = args[0] 65 } 66 if path == "" && name == "" && git.InsideGitRepo() { 67 wd, err := git.WorkingDir() 68 if err != nil { 69 log.Fatal(err) 70 } 71 p := strings.Split(wd, "/") 72 path = p[len(p)-1] 73 } 74 return path 75 } 76 77 func init() { 78 projectCreateCmd.Flags().StringP("name", "n", "", "name to use for the new project") 79 projectCreateCmd.Flags().StringP("description", "d", "", "description to use for the new project") 80 projectCreateCmd.Flags().BoolVarP(&private, "private", "p", false, "Make project private; visible only to project members (default: internal)") 81 projectCreateCmd.Flags().BoolVar(&public, "public", false, "Make project public; can be accessed without any authentication (default: internal)") 82 projectCmd.AddCommand(projectCreateCmd) 83 }