X Tutup
Skip to content

Commit 1eefda0

Browse files
jonathan-enfmislav
andauthored
gh auth login: add git protocol flag (cli#5158)
Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent f4b4a4e commit 1eefda0

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

pkg/cmd/auth/login/login.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ type LoginOptions struct {
2727

2828
Interactive bool
2929

30-
Hostname string
31-
Scopes []string
32-
Token string
33-
Web bool
30+
Hostname string
31+
Scopes []string
32+
Token string
33+
Web bool
34+
GitProtocol string
3435
}
3536

3637
func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Command {
@@ -54,8 +55,8 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
5455
Alternatively, pass in a token on standard input by using %[1]s--with-token%[1]s.
5556
The minimum required scopes for the token are: "repo", "read:org".
5657
57-
The --scopes flag accepts a comma separated list of scopes you want your gh credentials to have. If
58-
absent, this command ensures that gh has access to a minimum set of scopes.
58+
The %[1]s--scopes%[1]s flag accepts a comma separated list of scopes you want your gh credentials to
59+
have. If absent, this command ensures that gh has access to a minimum set of scopes.
5960
`, "`"),
6061
Example: heredoc.Doc(`
6162
# start interactive setup
@@ -64,7 +65,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
6465
# authenticate against github.com by reading the token from a file
6566
$ gh auth login --with-token < mytoken.txt
6667
67-
# authenticate with a specific GitHub Enterprise Server instance
68+
# authenticate with a specific GitHub instance
6869
$ gh auth login --hostname enterprise.internal
6970
`),
7071
RunE: func(cmd *cobra.Command, args []string) error {
@@ -111,6 +112,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
111112
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes for gh to have")
112113
cmd.Flags().BoolVar(&tokenStdin, "with-token", false, "Read token from standard input")
113114
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open a browser to authenticate")
115+
cmdutil.StringEnumFlag(cmd, &opts.GitProtocol, "git-protocol", "p", "", []string{"ssh", "https"}, "The protocol to use for git operations")
114116

115117
return cmd
116118
}
@@ -186,6 +188,7 @@ func loginRun(opts *LoginOptions) error {
186188
Web: opts.Web,
187189
Scopes: opts.Scopes,
188190
Executable: opts.MainExecutable,
191+
GitProtocol: opts.GitProtocol,
189192
})
190193
}
191194

pkg/cmd/auth/shared/login_flow.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type LoginOptions struct {
2929
Web bool
3030
Scopes []string
3131
Executable string
32+
GitProtocol string
3233

3334
sshContext sshContext
3435
}
@@ -39,8 +40,8 @@ func Login(opts *LoginOptions) error {
3940
httpClient := opts.HTTPClient
4041
cs := opts.IO.ColorScheme()
4142

42-
var gitProtocol string
43-
if opts.Interactive {
43+
gitProtocol := strings.ToLower(opts.GitProtocol)
44+
if opts.Interactive && gitProtocol == "" {
4445
var proto string
4546
err := prompt.SurveyAskOne(&survey.Select{
4647
Message: "What is your preferred protocol for Git operations?",

pkg/cmdutil/flags.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cmdutil
22

33
import (
4+
"fmt"
45
"strconv"
6+
"strings"
57

68
"github.com/spf13/cobra"
79
"github.com/spf13/pflag"
@@ -21,6 +23,20 @@ func NilBoolFlag(cmd *cobra.Command, p **bool, name string, shorthand string, us
2123
return f
2224
}
2325

26+
// StringEnumFlag defines a new string flag that only allows values listed in options.
27+
func StringEnumFlag(cmd *cobra.Command, p *string, name, shorthand, defaultValue string, options []string, usage string) *pflag.Flag {
28+
val := &enumValue{string: p, options: options}
29+
f := cmd.Flags().VarPF(val, name, shorthand, fmt.Sprintf("%s: %s", usage, formatValuesForUsageDocs(options)))
30+
_ = cmd.RegisterFlagCompletionFunc(name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
31+
return options, cobra.ShellCompDirectiveNoFileComp
32+
})
33+
return f
34+
}
35+
36+
func formatValuesForUsageDocs(values []string) string {
37+
return fmt.Sprintf("{%s}", strings.Join(values, "|"))
38+
}
39+
2440
type stringValue struct {
2541
string **string
2642
}
@@ -75,3 +91,31 @@ func (b *boolValue) Type() string {
7591
func (b *boolValue) IsBoolFlag() bool {
7692
return true
7793
}
94+
95+
type enumValue struct {
96+
string *string
97+
options []string
98+
}
99+
100+
func (e *enumValue) Set(value string) error {
101+
found := false
102+
for _, opt := range e.options {
103+
if strings.EqualFold(opt, value) {
104+
found = true
105+
break
106+
}
107+
}
108+
if !found {
109+
return fmt.Errorf("valid values are %s", formatValuesForUsageDocs(e.options))
110+
}
111+
*e.string = value
112+
return nil
113+
}
114+
115+
func (e *enumValue) String() string {
116+
return *e.string
117+
}
118+
119+
func (e *enumValue) Type() string {
120+
return "string"
121+
}

0 commit comments

Comments
 (0)
X Tutup