Avoid reconstructing config Options on each use

This commit is contained in:
William Martin 2024-05-08 13:49:37 +02:00
parent 8a4f32b4e2
commit d0e436b369
4 changed files with 50 additions and 52 deletions

View file

@ -163,7 +163,7 @@ func (c *cfg) CacheDir() string {
}
func defaultFor(key string) o.Option[string] {
for _, co := range ConfigOptions() {
for _, co := range Options {
if co.Key == key {
return o.Some(co.DefaultValue)
}
@ -516,59 +516,57 @@ type ConfigOption struct {
CurrentValue func(c gh.Config, hostname string) string
}
func ConfigOptions() []ConfigOption {
return []ConfigOption{
{
Key: gitProtocolKey,
Description: "the protocol to use for git clone and push operations",
DefaultValue: "https",
AllowedValues: []string{"https", "ssh"},
CurrentValue: func(c gh.Config, hostname string) string {
return c.GitProtocol(hostname)
},
var Options = []ConfigOption{
{
Key: gitProtocolKey,
Description: "the protocol to use for git clone and push operations",
DefaultValue: "https",
AllowedValues: []string{"https", "ssh"},
CurrentValue: func(c gh.Config, hostname string) string {
return c.GitProtocol(hostname)
},
{
Key: editorKey,
Description: "the text editor program to use for authoring text",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.Editor(hostname)
},
},
{
Key: editorKey,
Description: "the text editor program to use for authoring text",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.Editor(hostname)
},
{
Key: promptKey,
Description: "toggle interactive prompting in the terminal",
DefaultValue: "enabled",
AllowedValues: []string{"enabled", "disabled"},
CurrentValue: func(c gh.Config, hostname string) string {
return c.Prompt(hostname)
},
},
{
Key: promptKey,
Description: "toggle interactive prompting in the terminal",
DefaultValue: "enabled",
AllowedValues: []string{"enabled", "disabled"},
CurrentValue: func(c gh.Config, hostname string) string {
return c.Prompt(hostname)
},
{
Key: pagerKey,
Description: "the terminal pager program to send standard output to",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.Pager(hostname)
},
},
{
Key: pagerKey,
Description: "the terminal pager program to send standard output to",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.Pager(hostname)
},
{
Key: httpUnixSocketKey,
Description: "the path to a Unix socket through which to make an HTTP connection",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.HTTPUnixSocket(hostname)
},
},
{
Key: httpUnixSocketKey,
Description: "the path to a Unix socket through which to make an HTTP connection",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.HTTPUnixSocket(hostname)
},
{
Key: browserKey,
Description: "the web browser to use for opening URLs",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.Browser(hostname)
},
},
{
Key: browserKey,
Description: "the web browser to use for opening URLs",
DefaultValue: "",
CurrentValue: func(c gh.Config, hostname string) string {
return c.Browser(hostname)
},
}
},
}
func HomeDirPath(subdir string) (string, error) {

View file

@ -17,7 +17,7 @@ func NewCmdConfig(f *cmdutil.Factory) *cobra.Command {
longDoc := strings.Builder{}
longDoc.WriteString("Display or change configuration settings for gh.\n\n")
longDoc.WriteString("Current respected settings:\n")
for _, co := range config.ConfigOptions() {
for _, co := range config.Options {
longDoc.WriteString(fmt.Sprintf("- `%s`: %s", co.Key, co.Description))
if len(co.AllowedValues) > 0 {
longDoc.WriteString(fmt.Sprintf(" {%s}", strings.Join(co.AllowedValues, "|")))

View file

@ -55,7 +55,7 @@ func listRun(opts *ListOptions) error {
host, _ = cfg.Authentication().DefaultHost()
}
configOptions := config.ConfigOptions()
configOptions := config.Options
for _, option := range configOptions {
fmt.Fprintf(opts.IO.Out, "%s=%s\n", option.Key, option.CurrentValue(cfg, host))

View file

@ -88,7 +88,7 @@ func setRun(opts *SetOptions) error {
}
func ValidateKey(key string) error {
for _, configKey := range config.ConfigOptions() {
for _, configKey := range config.Options {
if key == configKey.Key {
return nil
}
@ -108,7 +108,7 @@ func (e InvalidValueError) Error() string {
func ValidateValue(key, value string) error {
var validValues []string
for _, v := range config.ConfigOptions() {
for _, v := range config.Options {
if v.Key == key {
validValues = v.AllowedValues
break