package cmd import ( "fmt" "strings" "code.gitea.io/sdk/gitea" "github.com/spf13/cobra" ) var repoCreateFromTemplateCmd = &cobra.Command{ Use: "create-from-template ", Aliases: []string{"ct"}, Short: "Create a repository from a template", Long: `Scaffold a new repository based on an existing template repository. By default this copies only the default branch content. Pass --with- flags to include topics, labels, webhooks, git hooks, and other template metadata.`, Example: ` # Create a new repo under your account from a template fgj repo create-from-template org/template-name my-new-repo # Target a specific owner and make it private fgj repo create-from-template org/template-name new-repo --owner myorg --private # Copy everything: content, topics, labels, webhooks, hooks, avatar fgj repo create-from-template org/template-name new-repo \ --with-content --with-topics --with-labels \ --with-webhooks --with-git-hooks --with-avatar`, Args: cobra.ExactArgs(2), RunE: runRepoCreateFromTemplate, } func init() { repoCmd.AddCommand(repoCreateFromTemplateCmd) repoCreateFromTemplateCmd.Flags().String("owner", "", "Owner (user or org) for the new repository (defaults to you)") repoCreateFromTemplateCmd.Flags().StringP("description", "d", "", "Description for the new repository") repoCreateFromTemplateCmd.Flags().Bool("private", false, "Make the new repository private") repoCreateFromTemplateCmd.Flags().Bool("with-content", true, "Include default branch content from the template") repoCreateFromTemplateCmd.Flags().Bool("with-topics", false, "Include topics from the template") repoCreateFromTemplateCmd.Flags().Bool("with-labels", false, "Include labels from the template") repoCreateFromTemplateCmd.Flags().Bool("with-webhooks", false, "Include webhooks from the template") repoCreateFromTemplateCmd.Flags().Bool("with-git-hooks", false, "Include git hooks from the template") repoCreateFromTemplateCmd.Flags().Bool("with-avatar", false, "Include the template repo's avatar") addJSONFlags(repoCreateFromTemplateCmd, "Output created repository as JSON") } func runRepoCreateFromTemplate(cmd *cobra.Command, args []string) error { templateSlug := args[0] newName := args[1] parts := strings.SplitN(templateSlug, "/", 2) if len(parts) != 2 || parts[0] == "" || parts[1] == "" { return fmt.Errorf("template must be in owner/name format (got %q)", templateSlug) } templateOwner, templateName := parts[0], parts[1] client, err := loadClient() if err != nil { return err } owner, _ := cmd.Flags().GetString("owner") if owner == "" { user, _, err := client.GetMyUserInfo() if err != nil { return fmt.Errorf("failed to resolve current user (pass --owner to override): %w", err) } owner = user.UserName } description, _ := cmd.Flags().GetString("description") private, _ := cmd.Flags().GetBool("private") withContent, _ := cmd.Flags().GetBool("with-content") withTopics, _ := cmd.Flags().GetBool("with-topics") withLabels, _ := cmd.Flags().GetBool("with-labels") withWebhooks, _ := cmd.Flags().GetBool("with-webhooks") withGitHooks, _ := cmd.Flags().GetBool("with-git-hooks") withAvatar, _ := cmd.Flags().GetBool("with-avatar") opt := gitea.CreateRepoFromTemplateOption{ Owner: owner, Name: newName, Description: description, Private: private, GitContent: withContent, Topics: withTopics, Labels: withLabels, Webhooks: withWebhooks, GitHooks: withGitHooks, Avatar: withAvatar, } ios.StartSpinner("Creating from template...") repo, _, err := client.CreateRepoFromTemplate(templateOwner, templateName, opt) ios.StopSpinner() if err != nil { return fmt.Errorf("template instantiation failed: %w", err) } if wantJSON(cmd) { return outputJSON(cmd, repo) } cs := ios.ColorScheme() fmt.Fprintf(ios.Out, "%s Created %s from template %s\n", cs.SuccessIcon(), repo.FullName, templateSlug) if repo.HTMLURL != "" { fmt.Fprintf(ios.Out, " %s\n", repo.HTMLURL) } return nil }