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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
package create
import (
"fmt"
"net/url"
"os"
"path"
"strings"
"gitlab.com/gitlab-org/cli/pkg/prompt"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/internal/glrepo"
"github.com/MakeNowJust/heredoc/v2"
"github.com/spf13/cobra"
gitlab "gitlab.com/gitlab-org/api/client-go"
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/internal/run"
"gitlab.com/gitlab-org/cli/pkg/git"
)
func NewCmdCreate(f *cmdutils.Factory) *cobra.Command {
projectCreateCmd := &cobra.Command{
Use: "create [path] [flags]",
Short: `Create a new GitLab project/repository.`,
Long: ``,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runCreateProject(cmd, args, f)
},
Example: heredoc.Doc(`
# Create a repository under your account using the current directory name.
$ glab repo create
# Create a repository under a group using the current directory name.
$ glab repo create --group glab-cli
# Create a repository with a specific name.
$ glab repo create my-project
# Create a repository for a group.
$ glab repo create glab-cli/my-project
`),
}
projectCreateCmd.Flags().StringP("name", "n", "", "Name of the new project.")
projectCreateCmd.Flags().StringP("group", "g", "", "Namespace or group for the new project. Defaults to the current user's namespace.")
projectCreateCmd.Flags().StringP("description", "d", "", "Description of the new project.")
projectCreateCmd.Flags().String("defaultBranch", "", "Default branch of the project. Defaults to `master` if not provided.")
projectCreateCmd.Flags().String("remoteName", "origin", "Remote name for the Git repository you're in. Defaults to `origin` if not provided.")
projectCreateCmd.Flags().StringArrayP("tag", "t", []string{}, "The list of tags for the project.")
projectCreateCmd.Flags().Bool("internal", false, "Make project internal: visible to any authenticated user. Default.")
projectCreateCmd.Flags().BoolP("private", "p", false, "Make project private: visible only to project members.")
projectCreateCmd.Flags().BoolP("public", "P", false, "Make project public: visible without any authentication.")
projectCreateCmd.Flags().Bool("readme", false, "Initialize project with `README.md`.")
return projectCreateCmd
}
func runCreateProject(cmd *cobra.Command, args []string, f *cmdutils.Factory) error {
var (
projectPath string
visiblity gitlab.VisibilityValue
err error
isPath bool
namespaceID int
namespace string
)
c := f.IO.Color()
defaultBranch, err := cmd.Flags().GetString("defaultBranch")
if err != nil {
return err
}
remoteName, err := cmd.Flags().GetString("remoteName")
if err != nil {
return err
}
err = initGit(defaultBranch)
if err != nil {
return err
}
apiClient, err := f.HttpClient()
if err != nil {
return err
}
if len(args) == 1 {
var host string
host, namespace, projectPath = projectPathFromArgs(args)
if host != "" {
cfg, _ := f.Config()
client, err := api.NewClientWithCfg(host, cfg, false)
if err != nil {
return err
}
apiClient = client.Lab()
}
user, err := api.CurrentUser(apiClient)
if err != nil {
return err
}
if user.Username == namespace {
namespace = ""
}
} else {
projectPath, err = git.ToplevelDir()
projectPath = path.Base(projectPath)
if err != nil {
return err
}
isPath = true
}
group, err := cmd.Flags().GetString("group")
if err != nil {
return fmt.Errorf("could not parse group flag: %v", err)
}
if group != "" {
namespace = group
}
if namespace != "" {
group, err := api.GetGroup(apiClient, namespace)
if err != nil {
return fmt.Errorf("could not find group or namespace %s: %v", namespace, err)
}
namespaceID = group.ID
}
name, _ := cmd.Flags().GetString("name")
if projectPath == "" && name == "" {
fmt.Println("ERROR: path or name required to create a project.")
return cmd.Usage()
} else if name == "" {
name = projectPath
}
description, _ := cmd.Flags().GetString("description")
if internal, _ := cmd.Flags().GetBool("internal"); internal {
visiblity = gitlab.InternalVisibility
} else if private, _ := cmd.Flags().GetBool("private"); private {
visiblity = gitlab.PrivateVisibility
} else if public, _ := cmd.Flags().GetBool("public"); public {
visiblity = gitlab.PublicVisibility
}
tags, _ := cmd.Flags().GetStringArray("tag")
readme, _ := cmd.Flags().GetBool("readme")
opts := &gitlab.CreateProjectOptions{
Name: gitlab.Ptr(name),
Path: gitlab.Ptr(projectPath),
Description: gitlab.Ptr(description),
DefaultBranch: gitlab.Ptr(defaultBranch),
TagList: &tags,
InitializeWithReadme: gitlab.Ptr(readme),
}
if visiblity != "" {
opts.Visibility = &visiblity
}
if namespaceID != 0 {
opts.NamespaceID = &namespaceID
}
project, err := api.CreateProject(apiClient, opts)
greenCheck := c.Green("✓")
if err == nil {
fmt.Fprintf(f.IO.StdOut, "%s Created repository %s on GitLab: %s\n", greenCheck, project.NameWithNamespace, project.WebURL)
if isPath {
cfg, _ := f.Config()
webURL, _ := url.Parse(project.WebURL)
protocol, _ := cfg.Get(webURL.Host, "git_protocol")
remote := glrepo.RemoteURL(project, protocol)
_, err = git.AddRemote(remoteName, remote)
if err != nil {
return err
}
fmt.Fprintf(f.IO.StdOut, "%s Added remote %s\n", greenCheck, remote)
} else if f.IO.PromptEnabled() {
var doSetup bool
err := prompt.Confirm(&doSetup, fmt.Sprintf("Create a local project directory for %s?", project.NameWithNamespace), true)
if err != nil {
return err
}
if doSetup {
projectPath := project.Path
err = initialiseRepo(projectPath, project.SSHURLToRepo)
if err != nil {
return err
}
fmt.Fprintf(f.IO.StdOut, "%s Initialized repository in './%s/'\n", greenCheck, projectPath)
}
}
} else {
return fmt.Errorf("error creating project: %v", err)
}
return err
}
func initGit(defaultBranch string) error {
if stat, err := os.Stat(".git"); err == nil && stat.IsDir() {
return nil
}
var doInit bool
err := prompt.Confirm(&doInit, "Directory not Git initialized. Run `git init`?", true)
if err != nil || !doInit {
return err
}
gitInit := git.GitCommand("init")
gitInit.Stdout = os.Stdout
gitInit.Stderr = os.Stderr
err = run.PrepareCmd(gitInit).Run()
if err != nil {
return err
}
if defaultBranch == "" {
return nil
}
gitBranch := git.GitCommand("checkout", "-b", defaultBranch)
gitBranch.Stdout = os.Stdout
gitBranch.Stdin = os.Stdin
return run.PrepareCmd(gitBranch).Run()
}
func initialiseRepo(projectPath, remoteURL string) error {
gitInit := git.GitCommand("init", projectPath)
gitInit.Stdout = os.Stdout
gitInit.Stderr = os.Stderr
err := run.PrepareCmd(gitInit).Run()
if err != nil {
return err
}
gitRemoteAdd := git.GitCommand("-C", projectPath, "remote", "add", "origin", remoteURL)
gitRemoteAdd.Stdout = os.Stdout
gitRemoteAdd.Stderr = os.Stderr
err = run.PrepareCmd(gitRemoteAdd).Run()
if err != nil {
return err
}
return nil
}
func projectPathFromArgs(args []string) (host, namespace, project string) {
// sanitize input by removing trailing "/"
project = strings.TrimSuffix(args[0], "/")
if strings.Contains(project, "/") {
pp, _ := glrepo.FromFullName(project)
host = pp.RepoHost()
project = pp.RepoName()
namespace = pp.RepoNamespace()
}
return
}
|