"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "pkg/cmd/repo/create/create.go" between
gh-cli-1.11.0.tar.gz and gh-cli-1.12.0.tar.gz

About: GitHub CLI is GitHub’s official command line tool.

create.go  (gh-cli-1.11.0):create.go  (gh-cli-1.12.0)
skipping to change at line 29 skipping to change at line 29
"github.com/cli/cli/pkg/iostreams" "github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/prompt" "github.com/cli/cli/pkg/prompt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type CreateOptions struct { type CreateOptions struct {
HttpClient func() (*http.Client, error) HttpClient func() (*http.Client, error)
Config func() (config.Config, error) Config func() (config.Config, error)
IO *iostreams.IOStreams IO *iostreams.IOStreams
Name string Name string
Description string Description string
Homepage string Homepage string
Team string Team string
Template string Template string
EnableIssues bool EnableIssues bool
EnableWiki bool EnableWiki bool
Public bool Public bool
Private bool Private bool
Internal bool Internal bool
ConfirmSubmit bool ConfirmSubmit bool
GitIgnoreTemplate string
LicenseTemplate string
} }
func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co mmand { func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co mmand {
opts := &CreateOptions{ opts := &CreateOptions{
IO: f.IOStreams, IO: f.IOStreams,
HttpClient: f.HttpClient, HttpClient: f.HttpClient,
Config: f.Config, Config: f.Config,
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
skipping to change at line 95 skipping to change at line 97
A repository can be supplied as an argument in an y of the following formats: A repository can be supplied as an argument in an y of the following formats:
- "OWNER/REPO" - "OWNER/REPO"
- by URL, e.g. "https://github.com/OWNER/REPO" - by URL, e.g. "https://github.com/OWNER/REPO"
`), `),
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 { if len(args) > 0 {
opts.Name = args[0] opts.Name = args[0]
} }
if len(args) == 0 && (opts.GitIgnoreTemplate != "" || opt
s.LicenseTemplate != "") {
return &cmdutil.FlagError{Err: errors.New(".gitig
nore and license templates are added only when a specific repository name is pas
sed")}
}
if opts.Template != "" && (opts.GitIgnoreTemplate != "" |
| opts.LicenseTemplate != "") {
return &cmdutil.FlagError{Err: errors.New(".gitig
nore and license templates are not added when template is provided")}
}
if !opts.IO.CanPrompt() { if !opts.IO.CanPrompt() {
if opts.Name == "" { if opts.Name == "" {
return &cmdutil.FlagError{Err: errors.New ("name argument required when not running interactively")} return &cmdutil.FlagError{Err: errors.New ("name argument required when not running interactively")}
} }
if !opts.Internal && !opts.Private && !opts.Publi c { if !opts.Internal && !opts.Private && !opts.Publi c {
return &cmdutil.FlagError{Err: errors.New ("`--public`, `--private`, or `--internal` required when not running interactive ly")} return &cmdutil.FlagError{Err: errors.New ("`--public`, `--private`, or `--internal` required when not running interactive ly")}
} }
} }
skipping to change at line 126 skipping to change at line 136
cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Descri ption of the repository") cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Descri ption of the repository")
cmd.Flags().StringVarP(&opts.Homepage, "homepage", "h", "", "Repository h ome page `URL`") cmd.Flags().StringVarP(&opts.Homepage, "homepage", "h", "", "Repository h ome page `URL`")
cmd.Flags().StringVarP(&opts.Team, "team", "t", "", "The `name` of the or ganization team to be granted access") cmd.Flags().StringVarP(&opts.Team, "team", "t", "", "The `name` of the or ganization team to be granted access")
cmd.Flags().StringVarP(&opts.Template, "template", "p", "", "Make the new repository based on a template `repository`") cmd.Flags().StringVarP(&opts.Template, "template", "p", "", "Make the new repository based on a template `repository`")
cmd.Flags().BoolVar(&opts.EnableIssues, "enable-issues", true, "Enable is sues in the new repository") cmd.Flags().BoolVar(&opts.EnableIssues, "enable-issues", true, "Enable is sues in the new repository")
cmd.Flags().BoolVar(&opts.EnableWiki, "enable-wiki", true, "Enable wiki i n the new repository") cmd.Flags().BoolVar(&opts.EnableWiki, "enable-wiki", true, "Enable wiki i n the new repository")
cmd.Flags().BoolVar(&opts.Public, "public", false, "Make the new reposito ry public") cmd.Flags().BoolVar(&opts.Public, "public", false, "Make the new reposito ry public")
cmd.Flags().BoolVar(&opts.Private, "private", false, "Make the new reposi tory private") cmd.Flags().BoolVar(&opts.Private, "private", false, "Make the new reposi tory private")
cmd.Flags().BoolVar(&opts.Internal, "internal", false, "Make the new repo sitory internal") cmd.Flags().BoolVar(&opts.Internal, "internal", false, "Make the new repo sitory internal")
cmd.Flags().BoolVarP(&opts.ConfirmSubmit, "confirm", "y", false, "Skip th e confirmation prompt") cmd.Flags().BoolVarP(&opts.ConfirmSubmit, "confirm", "y", false, "Skip th e confirmation prompt")
cmd.Flags().StringVarP(&opts.GitIgnoreTemplate, "gitignore", "g", "", "Sp
ecify a gitignore template for the repository")
cmd.Flags().StringVarP(&opts.LicenseTemplate, "license", "l", "", "Specif
y an Open Source License for the repository")
return cmd return cmd
} }
func createRun(opts *CreateOptions) error { func createRun(opts *CreateOptions) error {
projectDir, projectDirErr := git.ToplevelDir() projectDir, projectDirErr := git.ToplevelDir()
isNameAnArg := false isNameAnArg := false
isDescEmpty := opts.Description == "" isDescEmpty := opts.Description == ""
isVisibilityPassed := false isVisibilityPassed := false
inLocalRepo := projectDirErr == nil inLocalRepo := projectDirErr == nil
skipping to change at line 167 skipping to change at line 178
enabledFlagCount++ enabledFlagCount++
visibility = "INTERNAL" visibility = "INTERNAL"
} }
if enabledFlagCount > 1 { if enabledFlagCount > 1 {
return fmt.Errorf("expected exactly one of `--public`, `--private `, or `--internal` to be true") return fmt.Errorf("expected exactly one of `--public`, `--private `, or `--internal` to be true")
} else if enabledFlagCount == 1 { } else if enabledFlagCount == 1 {
isVisibilityPassed = true isVisibilityPassed = true
} }
cfg, err := opts.Config()
if err != nil {
return err
}
var gitIgnoreTemplate, repoLicenseTemplate string
gitIgnoreTemplate = opts.GitIgnoreTemplate
repoLicenseTemplate = opts.LicenseTemplate
// Trigger interactive prompt if name is not passed // Trigger interactive prompt if name is not passed
if !isNameAnArg { if !isNameAnArg {
newName, newDesc, newVisibility, err := interactiveRepoCreate(isD escEmpty, isVisibilityPassed, opts.Name) newName, newDesc, newVisibility, err := interactiveRepoCreate(isD escEmpty, isVisibilityPassed, opts.Name)
if err != nil { if err != nil {
return err return err
} }
if newName != "" { if newName != "" {
opts.Name = newName opts.Name = newName
} }
if newDesc != "" { if newDesc != "" {
opts.Description = newDesc opts.Description = newDesc
} }
if newVisibility != "" { if newVisibility != "" {
visibility = newVisibility visibility = newVisibility
} }
} else { } else {
// Go for a prompt only if visibility isn't passed // Go for a prompt only if visibility isn't passed
if !isVisibilityPassed { if !isVisibilityPassed {
newVisibility, err := getVisibility() newVisibility, err := getVisibility()
if err != nil { if err != nil {
return nil return nil
} }
visibility = newVisibility visibility = newVisibility
} }
}
cfg, err := opts.Config() httpClient, err := opts.HttpClient()
if err != nil { if err != nil {
return err return err
}
host, err := cfg.DefaultHost()
if err != nil {
return err
}
// GitIgnore and License templates not added when a template repo
sitory is passed.
if gitIgnoreTemplate == "" && opts.Template == "" && opts.IO.CanP
rompt() {
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(htt
pClient), host)
if err != nil {
return err
}
gitIgnoreTemplate = gt
}
if repoLicenseTemplate == "" && opts.Template == "" && opts.IO.Ca
nPrompt() {
lt, err := interactiveLicense(api.NewClientFromHTTP(httpC
lient), host)
if err != nil {
return err
}
repoLicenseTemplate = lt
}
} }
var repoToCreate ghrepo.Interface var repoToCreate ghrepo.Interface
if strings.Contains(opts.Name, "/") { if strings.Contains(opts.Name, "/") {
var err error var err error
repoToCreate, err = ghrepo.FromFullName(opts.Name) repoToCreate, err = ghrepo.FromFullName(opts.Name)
if err != nil { if err != nil {
return fmt.Errorf("argument error: %w", err) return fmt.Errorf("argument error: %w", err)
} }
skipping to change at line 248 skipping to change at line 292
repo, err := api.GitHubRepo(apiClient, toClone) repo, err := api.GitHubRepo(apiClient, toClone)
if err != nil { if err != nil {
return err return err
} }
opts.Template = repo.ID opts.Template = repo.ID
templateRepoMainBranch = repo.DefaultBranchRef.Name templateRepoMainBranch = repo.DefaultBranchRef.Name
} }
input := repoCreateInput{ input := repoCreateInput{
Name: repoToCreate.RepoName(), Name: repoToCreate.RepoName(),
Visibility: visibility, Visibility: visibility,
OwnerID: repoToCreate.RepoOwner(), OwnerID: repoToCreate.RepoOwner(),
TeamID: opts.Team, TeamID: opts.Team,
Description: opts.Description, Description: opts.Description,
HomepageURL: opts.Homepage, HomepageURL: opts.Homepage,
HasIssuesEnabled: opts.EnableIssues, HasIssuesEnabled: opts.EnableIssues,
HasWikiEnabled: opts.EnableWiki, HasWikiEnabled: opts.EnableWiki,
GitIgnoreTemplate: gitIgnoreTemplate,
LicenseTemplate: repoLicenseTemplate,
} }
httpClient, err := opts.HttpClient() httpClient, err := opts.HttpClient()
if err != nil { if err != nil {
return err return err
} }
createLocalDirectory := opts.ConfirmSubmit createLocalDirectory := opts.ConfirmSubmit
if !opts.ConfirmSubmit { if !opts.ConfirmSubmit {
opts.ConfirmSubmit, err = confirmSubmission(input.Name, input.Own erID, inLocalRepo) opts.ConfirmSubmit, err = confirmSubmission(input.Name, input.Own erID, inLocalRepo)
if err != nil { if err != nil {
return err return err
} }
} }
if opts.ConfirmSubmit { if opts.ConfirmSubmit {
repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), inpu t, opts.Template) repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), inpu t, opts.Template)
if err != nil { if err != nil {
return err return err
} }
stderr := opts.IO.ErrOut stderr := opts.IO.ErrOut
stdout := opts.IO.Out stdout := opts.IO.Out
cs := opts.IO.ColorScheme() cs := opts.IO.ColorScheme()
isTTY := opts.IO.IsStdoutTTY() isTTY := opts.IO.IsStdoutTTY()
if isTTY { if isTTY {
fmt.Fprintf(stderr, "%s Created repository %s on GitHub\n ", cs.SuccessIconWithColor(cs.Green), ghrepo.FullName(repo)) fmt.Fprintf(stderr, "%s Created repository %s on GitHub\n ", cs.SuccessIconWithColor(cs.Green), ghrepo.FullName(repo))
} else { } else {
fmt.Fprintln(stdout, repo.URL) fmt.Fprintln(stdout, ghrepo.GenerateRepoURL(repo, ""))
} }
protocol, err := cfg.Get(repo.RepoHost(), "git_protocol") protocol, err := cfg.Get(repo.RepoHost(), "git_protocol")
if err != nil { if err != nil {
return err return err
} }
remoteURL := ghrepo.FormatRemoteURL(repo, protocol) remoteURL := ghrepo.FormatRemoteURL(repo, protocol)
if inLocalRepo { if inLocalRepo {
_, err = git.AddRemote("origin", remoteURL) _, err = git.AddRemote("origin", remoteURL)
if err != nil { if err != nil {
return err return err
} }
if isTTY { if isTTY {
fmt.Fprintf(stderr, "%s Added remote %s\n", cs.Su ccessIcon(), remoteURL) fmt.Fprintf(stderr, "%s Added remote %s\n", cs.Su ccessIcon(), remoteURL)
} }
} else { } else {
if opts.IO.CanPrompt() { if opts.IO.CanPrompt() {
if !createLocalDirectory { if !createLocalDirectory && (gitIgnoreTemplate == "" && repoLicenseTemplate == "") {
err := prompt.Confirm(fmt.Sprintf(`Create a local project directory for "%s"?`, ghrepo.FullName(repo)), &createLocalDirec tory) err := prompt.Confirm(fmt.Sprintf(`Create a local project directory for "%s"?`, ghrepo.FullName(repo)), &createLocalDirec tory)
if err != nil { if err != nil {
return err return err
} }
} else if !createLocalDirectory && (gitIgnoreTemp
late != "" || repoLicenseTemplate != "") {
err := prompt.Confirm(fmt.Sprintf(`Clone
the remote project directory "%s"?`, ghrepo.FullName(repo)), &createLocalDirecto
ry)
if err != nil {
return err
}
} }
} }
if createLocalDirectory { if createLocalDirectory && (gitIgnoreTemplate == "" && re
path := repo.Name poLicenseTemplate == "") {
path := repo.RepoName()
checkoutBranch := "" checkoutBranch := ""
if opts.Template != "" { if opts.Template != "" {
// NOTE: we cannot read `defaultBranchRef ` from the newly created repository as it will // NOTE: we cannot read `defaultBranchRef ` from the newly created repository as it will
// be null at this time. Instead, we assu me that the main branch name of the new // be null at this time. Instead, we assu me that the main branch name of the new
// repository will be the same as that of the template repository. // repository will be the same as that of the template repository.
checkoutBranch = templateRepoMainBranch checkoutBranch = templateRepoMainBranch
} }
if err := localInit(opts.IO, remoteURL, path, che ckoutBranch); err != nil { if err := localInit(opts.IO, remoteURL, path, che ckoutBranch); err != nil {
return err return err
} }
if isTTY { if isTTY {
fmt.Fprintf(stderr, "%s Initialized repos itory in \"%s\"\n", cs.SuccessIcon(), path) fmt.Fprintf(stderr, "%s Initialized repos itory in \"%s\"\n", cs.SuccessIcon(), path)
} }
} else if createLocalDirectory && (gitIgnoreTemplate != "
" || repoLicenseTemplate != "") {
_, err := git.RunClone(remoteURL, []string{})
if err != nil {
return err
}
} }
} }
return nil return nil
} }
fmt.Fprintln(opts.IO.Out, "Discarding...") fmt.Fprintln(opts.IO.Out, "Discarding...")
return nil return nil
} }
func interactiveGitIgnore(client *api.Client, hostname string) (string, error) {
var addGitIgnore bool
var addGitIgnoreSurvey []*survey.Question
addGitIgnoreQuestion := &survey.Question{
Name: "addGitIgnore",
Prompt: &survey.Confirm{
Message: "Would you like to add a .gitignore?",
Default: false,
},
}
addGitIgnoreSurvey = append(addGitIgnoreSurvey, addGitIgnoreQuestion)
err := prompt.SurveyAsk(addGitIgnoreSurvey, &addGitIgnore)
if err != nil {
return "", err
}
var wantedIgnoreTemplate string
if addGitIgnore {
var gitIg []*survey.Question
gitIgnoretemplates, err := ListGitIgnoreTemplates(client, hostnam
e)
if err != nil {
return "", err
}
gitIgnoreQuestion := &survey.Question{
Name: "chooseGitIgnore",
Prompt: &survey.Select{
Message: "Choose a .gitignore template",
Options: gitIgnoretemplates,
},
}
gitIg = append(gitIg, gitIgnoreQuestion)
err = prompt.SurveyAsk(gitIg, &wantedIgnoreTemplate)
if err != nil {
return "", err
}
}
return wantedIgnoreTemplate, nil
}
func interactiveLicense(client *api.Client, hostname string) (string, error) {
var addLicense bool
var addLicenseSurvey []*survey.Question
var wantedLicense string
addLicenseQuestion := &survey.Question{
Name: "addLicense",
Prompt: &survey.Confirm{
Message: "Would you like to add a license?",
Default: false,
},
}
addLicenseSurvey = append(addLicenseSurvey, addLicenseQuestion)
err := prompt.SurveyAsk(addLicenseSurvey, &addLicense)
if err != nil {
return "", err
}
licenseKey := map[string]string{}
if addLicense {
licenseTemplates, err := ListLicenseTemplates(client, hostname)
if err != nil {
return "", err
}
var licenseNames []string
for _, l := range licenseTemplates {
licenseNames = append(licenseNames, l.Name)
licenseKey[l.Name] = l.Key
}
var licenseQs []*survey.Question
licenseQuestion := &survey.Question{
Name: "chooseLicense",
Prompt: &survey.Select{
Message: "Choose a license",
Options: licenseNames,
},
}
licenseQs = append(licenseQs, licenseQuestion)
err = prompt.SurveyAsk(licenseQs, &wantedLicense)
if err != nil {
return "", err
}
return licenseKey[wantedLicense], nil
}
return "", nil
}
func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error { func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error {
gitInit, err := git.GitCommand("init", path) gitInit, err := git.GitCommand("init", path)
if err != nil { if err != nil {
return err return err
} }
isTTY := io.IsStdoutTTY() isTTY := io.IsStdoutTTY()
if isTTY { if isTTY {
gitInit.Stdout = io.Out gitInit.Stdout = io.Out
} }
gitInit.Stderr = io.ErrOut gitInit.Stderr = io.ErrOut
 End of changes. 16 change blocks. 
29 lines changed or deleted 198 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)