| 51 | } |
| 52 | |
| 53 | func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) *cobra.Command { |
| 54 | opts := &SetDefaultOptions{ |
| 55 | IO: f.IOStreams, |
| 56 | HttpClient: f.HttpClient, |
| 57 | Remotes: f.Remotes, |
| 58 | Prompter: f.Prompter, |
| 59 | GitClient: f.GitClient, |
| 60 | } |
| 61 | |
| 62 | cmd := &cobra.Command{ |
| 63 | Use: "set-default [<repository>]", |
| 64 | Short: "Configure default repository for this directory", |
| 65 | Long: explainer(), |
| 66 | Example: heredoc.Doc(` |
| 67 | # Interactively select a default repository |
| 68 | $ gh repo set-default |
| 69 | |
| 70 | # Set a repository explicitly |
| 71 | $ gh repo set-default owner/repo |
| 72 | |
| 73 | # Set a repository using a git remote name |
| 74 | $ gh repo set-default origin |
| 75 | |
| 76 | # View the current default repository |
| 77 | $ gh repo set-default --view |
| 78 | |
| 79 | # Show more repository options in the interactive picker |
| 80 | $ git remote add newrepo https://github.com/owner/repo |
| 81 | $ gh repo set-default |
| 82 | `), |
| 83 | Args: cobra.MaximumNArgs(1), |
| 84 | RunE: func(cmd *cobra.Command, args []string) error { |
| 85 | if isLocal, err := opts.GitClient.IsLocalGitRepo(cmd.Context()); err != nil { |
| 86 | return err |
| 87 | } else if !isLocal { |
| 88 | return errors.New("must be run from inside a git repository") |
| 89 | } |
| 90 | |
| 91 | if len(args) > 0 { |
| 92 | var err error |
| 93 | opts.Repo, err = ghrepo.FromFullName(args[0]) |
| 94 | if err != nil { |
| 95 | remotes, remoteErr := opts.Remotes() |
| 96 | if remoteErr != nil { |
| 97 | return remoteErr |
| 98 | } |
| 99 | |
| 100 | remote, findErr := remotes.FindByName(args[0]) |
| 101 | if findErr != nil { |
| 102 | return fmt.Errorf("given arg is not a valid repo or git remote: %w", err) |
| 103 | } |
| 104 | opts.Repo = remote.Repo |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if !opts.ViewMode && !opts.IO.CanPrompt() && opts.Repo == nil { |
| 109 | return cmdutil.FlagErrorf("repository required when not running interactively") |
| 110 | } |