-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Allow argument to gh repo set-default to be the name of a remote
#10002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -78,14 +78,6 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) * | |||||||||
| `), | ||||||||||
| Args: cobra.MaximumNArgs(1), | ||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||
| if len(args) > 0 { | ||||||||||
| var err error | ||||||||||
| opts.Repo, err = ghrepo.FromFullName(args[0]) | ||||||||||
| if err != nil { | ||||||||||
| return err | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if !opts.ViewMode && !opts.IO.CanPrompt() && opts.Repo == nil { | ||||||||||
| return cmdutil.FlagErrorf("repository required when not running interactively") | ||||||||||
| } | ||||||||||
|
|
@@ -96,6 +88,14 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) * | |||||||||
| return errors.New("must be run from inside a git repository") | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if len(args) > 0 { | ||||||||||
| var err error | ||||||||||
| opts.Repo, err = parseRepo(args[0], opts) | ||||||||||
| if err != nil { | ||||||||||
| return err | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
Comment on lines
+91
to
+98
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this logic needed to be moved to later within Depending on the motivation for moving the logic, this might have to be undone as non-interactive usage will break because cli/pkg/cmd/repo/setdefault/setdefault.go Lines 80 to 83 in b4b8e8a
|
||||||||||
| if runF != nil { | ||||||||||
| return runF(opts) | ||||||||||
| } | ||||||||||
|
|
@@ -252,3 +252,22 @@ func displayRemoteRepoName(remote *context.Remote) string { | |||||||||
|
|
||||||||||
| return ghrepo.FullName(repo) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func parseRepo(name string, opts *SetDefaultOptions) (ghrepo.Interface, error) { | ||||||||||
| if repo, err := ghrepo.FromFullName(name); err == nil { | ||||||||||
| return repo, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| remotes, err := opts.Remotes() | ||||||||||
| if err != nil { | ||||||||||
| return nil, err | ||||||||||
| } | ||||||||||
|
Comment on lines
+261
to
+264
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My major concern with the code as-is is that multiple calls to resolve Perhaps the expense isn't significant enough to move forward here. It is mostly a question whether instead of providing a function to resolve the remotes if an actual remote resolver is provided 🤔 |
||||||||||
|
|
||||||||||
| for _, remote := range remotes { | ||||||||||
| if remote.Name == name { | ||||||||||
| return remote.Repo, nil | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return nil, fmt.Errorf(`expected the "[HOST/]OWNER/REPO" format or a remote name, got %q`, name) | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to move processing
opts.Repoout ofRunEand intosetDefaultRun?The
spf13/cobracommand hooks should be focused on translatingcobraarguments and flags into variables that the application logic cares about, however this is no longer as simple as turningowner/repointoghrepo.Interface. With the changes as-is, we're duplicating logic around remotes and starting to do more application-level behavior outside of the application code.It might be cleaner and easier to test by changing
opts.Repoto a string likeopts.RepoOrRemoteand defer checking remotes or parsing this as a repo until later in the logic:cli/pkg/cmd/repo/setdefault/setdefault.go
Lines 172 to 182 in 9bdfa83
RunElogic keeps this logic within the testable code