MCPcopy
hub / github.com/cli/cli / NewCmdSetDefault

Function NewCmdSetDefault

pkg/cmd/repo/setdefault/setdefault.go:53–124  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*SetDefaultOptions) error)

Source from the content-addressed store, hash-verified

51}
52
53func 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 }

Callers 1

TestNewCmdSetDefaultFunction · 0.85

Calls 9

FromFullNameFunction · 0.92
FlagErrorfFunction · 0.92
explainerFunction · 0.85
setDefaultRunFunction · 0.85
IsLocalGitRepoMethod · 0.80
FindByNameMethod · 0.80
CanPromptMethod · 0.80
RemotesMethod · 0.65
ErrorfMethod · 0.65

Tested by 1

TestNewCmdSetDefaultFunction · 0.68