11package create
22
33import (
4+ "errors"
45 "fmt"
56 "net/http"
67 "path"
78 "strings"
89
910 "github.com/AlecAivazis/survey/v2"
1011 "github.com/MakeNowJust/heredoc"
12+ "github.com/cli/cli/api"
1113 "github.com/cli/cli/git"
1214 "github.com/cli/cli/internal/config"
15+ "github.com/cli/cli/internal/ghinstance"
1316 "github.com/cli/cli/internal/ghrepo"
1417 "github.com/cli/cli/internal/run"
1518 "github.com/cli/cli/pkg/cmdutil"
@@ -28,6 +31,7 @@ type CreateOptions struct {
2831 Description string
2932 Homepage string
3033 Team string
34+ Template string
3135 EnableIssues bool
3236 EnableWiki bool
3337 Public bool
@@ -73,13 +77,18 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
7377 return runF (opts )
7478 }
7579
80+ if opts .Template != "" && (opts .Homepage != "" || opts .Team != "" || ! opts .EnableIssues || ! opts .EnableWiki ) {
81+ return & cmdutil.FlagError {Err : errors .New (`The '--template' option is not supported with '--homepage, --team, --enable-issues or --enable-wiki'` )}
82+ }
83+
7684 return createRun (opts )
7785 },
7886 }
7987
8088 cmd .Flags ().StringVarP (& opts .Description , "description" , "d" , "" , "Description of repository" )
8189 cmd .Flags ().StringVarP (& opts .Homepage , "homepage" , "h" , "" , "Repository home page URL" )
8290 cmd .Flags ().StringVarP (& opts .Team , "team" , "t" , "" , "The name of the organization team to be granted access" )
91+ cmd .Flags ().StringVarP (& opts .Template , "template" , "p" , "" , "Make the new repository based on a template repository" )
8392 cmd .Flags ().BoolVar (& opts .EnableIssues , "enable-issues" , true , "Enable issues in the new repository" )
8493 cmd .Flags ().BoolVar (& opts .EnableWiki , "enable-wiki" , true , "Enable wiki in the new repository" )
8594 cmd .Flags ().BoolVar (& opts .Public , "public" , false , "Make the new repository public" )
@@ -164,6 +173,37 @@ func createRun(opts *CreateOptions) error {
164173 }
165174 }
166175
176+ // Find template repo ID
177+ if opts .Template != "" {
178+ httpClient , err := opts .HttpClient ()
179+ if err != nil {
180+ return err
181+ }
182+
183+ var toClone ghrepo.Interface
184+ apiClient := api .NewClientFromHTTP (httpClient )
185+
186+ cloneURL := opts .Template
187+ if ! strings .Contains (cloneURL , "/" ) {
188+ currentUser , err := api .CurrentLoginName (apiClient , ghinstance .Default ())
189+ if err != nil {
190+ return err
191+ }
192+ cloneURL = currentUser + "/" + cloneURL
193+ }
194+ toClone , err = ghrepo .FromFullName (cloneURL )
195+ if err != nil {
196+ return fmt .Errorf ("argument error: %w" , err )
197+ }
198+
199+ repo , err := api .GitHubRepo (apiClient , toClone )
200+ if err != nil {
201+ return err
202+ }
203+
204+ opts .Template = repo .ID
205+ }
206+
167207 input := repoCreateInput {
168208 Name : repoToCreate .RepoName (),
169209 Visibility : visibility ,
@@ -189,7 +229,7 @@ func createRun(opts *CreateOptions) error {
189229 }
190230
191231 if opts .ConfirmSubmit {
192- repo , err := repoCreate (httpClient , repoToCreate .RepoHost (), input )
232+ repo , err := repoCreate (httpClient , repoToCreate .RepoHost (), input , opts . Template )
193233 if err != nil {
194234 return err
195235 }
0 commit comments