func NewCmdClone()

in commands/project/clone/repo_clone.go [56:173]


func NewCmdClone(f *cmdutils.Factory, runE func(*CloneOptions, *ContextOpts) error) *cobra.Command {
	opts := &CloneOptions{
		GitFlags: []string{},
		IO:       f.IO,
		Config:   f.Config,
	}

	ctxOpts := &ContextOpts{}

	repoCloneCmd := &cobra.Command{
		Use: `clone <repo> [flags] [<dir>] [-- <gitflags>...]
glab repo clone -g <group> [flags] [<dir>] [-- <gitflags>...]`,
		Short: `Clone a GitLab repository or project.`,
		Example: heredoc.Doc(`
			# Clones repository into current directory
			$ glab repo clone gitlab-org/cli
			$ glab repo clone https://gitlab.com/gitlab-org/cli

			# Clones repository into 'mydirectory'
			$ glab repo clone gitlab-org/cli mydirectory

			# Clones repository 'glab' for current user
			$ glab repo clone glab

			# Finds the project by the ID provided and clones it
			$ glab repo clone 4356677

			# Clones all repos in a group
			$ glab repo clone -g everyonecancontribute --paginate

			# Clones all non-archived repos in a group
			$ glab repo clone -g everyonecancontribute --archived=false --paginate

			# Clones from a GitLab Self-Managed or GitLab Dedicated instance
			$ GITLAB_HOST=salsa.debian.org glab repo clone myrepo
		`),
		Long: heredoc.Doc(`
		Clone supports these shorthand references:

		- repo
		- namespace/repo
		- org/group/repo
		- project ID
		`),
		RunE: func(cmd *cobra.Command, args []string) error {
			// Move arguments after "--" to gitFlags
			if dashPos := cmd.ArgsLenAtDash(); dashPos != -1 {
				opts.GitFlags = args[dashPos:]
				args = args[:dashPos]
			}
			dbg.Debug("Args:", strings.Join(args, " "))
			dbg.Debug("GitFlags:", strings.Join(opts.GitFlags, " "))
			if nArgs := len(args); nArgs > 0 {
				ctxOpts.Repo = args[0]
				if nArgs > 1 && !opts.PreserveNamespace {
					opts.Dir = args[1]
				}
			}
			dbg.Debug("Dir:", opts.Dir)

			if ctxOpts.Repo == "" && opts.GroupName == "" {
				return &cmdutils.FlagError{Err: fmt.Errorf("Specify repository argument, or use the --group flag to specify a group to clone all repos from the group.")}
			}

			if runE != nil {
				return runE(opts, ctxOpts)
			}

			opts.Host = glinstance.OverridableDefault()
			opts.ArchivedSet = cmd.Flags().Changed("archived")

			cfg, err := opts.Config()
			if err != nil {
				return err
			}
			opts.APIClient, err = api.NewClientWithCfg(opts.Host, cfg, false)
			if err != nil {
				return err
			}

			opts.CurrentUser, err = api.CurrentUser(opts.APIClient.Lab())
			if err != nil {
				return err
			}

			opts.Protocol, _ = cfg.Get(opts.Host, "git_protocol")

			if opts.GroupName != "" {
				return groupClone(opts, ctxOpts)
			}

			return cloneRun(opts, ctxOpts)
		},
	}

	repoCloneCmd.Flags().StringVarP(&opts.GroupName, "group", "g", "", "Specify the group to clone repositories from.")
	repoCloneCmd.Flags().BoolVarP(&opts.PreserveNamespace, "preserve-namespace", "p", false, "Clone the repository in a subdirectory based on namespace.")
	repoCloneCmd.Flags().BoolVarP(&opts.Archived, "archived", "a", false, "Limit by archived status. Use with '-a=false' to exclude archived repositories. Used with the --group flag.")
	repoCloneCmd.Flags().BoolVarP(&opts.IncludeSubgroups, "include-subgroups", "G", true, "Include projects in subgroups of this group. Default is true. Used with the --group flag.")
	repoCloneCmd.Flags().BoolVarP(&opts.Owned, "mine", "m", false, "Limit by projects in the group owned by the current authenticated user. Used with the --group flag.")
	repoCloneCmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "", "Limit by visibility: public, internal, private. Used with the --group flag.")
	repoCloneCmd.Flags().BoolVarP(&opts.WithIssuesEnabled, "with-issues-enabled", "I", false, "Limit by projects with the issues feature enabled. Default is false. Used with the --group flag.")
	repoCloneCmd.Flags().BoolVarP(&opts.WithMREnabled, "with-mr-enabled", "M", false, "Limit by projects with the merge request feature enabled. Default is false. Used with the --group flag.")
	repoCloneCmd.Flags().BoolVarP(&opts.WithShared, "with-shared", "S", true, "Include projects shared to this group. Default is true. Used with the --group flag.")
	repoCloneCmd.Flags().BoolVarP(&opts.Paginate, "paginate", "", false, "Make additional HTTP requests to fetch all pages of projects before cloning. Respects --per-page.")
	repoCloneCmd.Flags().IntVarP(&opts.Page, "page", "", 1, "Page number.")
	repoCloneCmd.Flags().IntVarP(&opts.PerPage, "per-page", "", 30, "Number of items to list per page.")

	repoCloneCmd.Flags().SortFlags = false
	repoCloneCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
		if errors.Is(err, pflag.ErrHelp) {
			return err
		}
		return &cmdutils.FlagError{Err: fmt.Errorf("%w\nSeparate Git clone flags with '--'.", err)}
	})

	return repoCloneCmd
}