Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@cristiand391
Copy link
Contributor

Fix #2130

Example:

gh config set git_protocol sshpps
failed to set "git_protocol" to "sshpps": invalid git protocol

samcoe
samcoe previously requested changes Oct 9, 2020
Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristiand391 Thanks for starting work on this! I like the initial direction. I can envision future scenarios where we would want to validate other configuration settings, which is why I think this should be made into a more generic solution for validating any key value pair that might be set through the configure command. Initially we can validate just the git_protocol setting but write the code in a way that makes it easy to add more validations in the future. Once we have that in place, we should add some tests for the new functionality.

@cristiand391
Copy link
Contributor Author

cristiand391 commented Oct 9, 2020

@samcoe Alright, what do you think about this?

func ValidateConfig(key, value string) error {
    switch key {
    case "git_protocol":
           if value != "ssh" && value != "https" {
                return fmt.Errorf("invalid git protocol")
           }
    }

    return nil
}

...
func (c *fileConfig) Set(hostname, key, value string) error {
    if err := ValidateConfig(key, value); err != nil {
        return err
    }
...

If there's an error it will be formated and printed here on pkg/cmd/config/config.go

@shrihankp
Copy link
Contributor

shrihankp commented Oct 11, 2020

What do you think about #2145 ?

EDIT: PR #2145 closed

@samcoe
Copy link
Contributor

samcoe commented Oct 11, 2020

@cristiand391 That looks like a good direction to me. Stylistically I find reading a map where the key is the configuration key and the value being an array of valid values a bit easier to parse than a huge switch statement. I was thinking something like this:

func validConfigEntries(key string) []string {
	configEntries := make(map[string][]string)

	configEntries["git_protocol"] = []string{"ssh", "https"}

	return configEntries[key]
}

func validateConfigEntry(key, value string) error {
	validValues := validConfigEntries(key)

	if len(validValues) == 0 {
		return nil
	}

	for _, v := range validValues {
		if v == value {
			return nil
		}
	}

	return fmt.Errorf("%s is not a valid value for %s", value, key)
}

What do you think?

@shrihankp
Copy link
Contributor

shrihankp commented Oct 11, 2020

@samcoe Actually, I have absolutely zero experience with Go. I actually understand a bit from this code, for I am comfortable with Java, JavaScript and C++ πŸ˜…

What I just assume is, a map is basically like an object in JavaScript, that stores some key-value pairs. If I am right, then, this looks like a lot cleaner code to me.

@cristiand391
Copy link
Contributor Author

It definitely looks cleaner than using if/else or switch conditionals, thanks πŸ‘πŸΎ

@shrihankp
Copy link
Contributor

shrihankp commented Oct 12, 2020

Will another property do: configEntries["prompt"] = []string{"enabled", "disabled"}

@cristiand391 cristiand391 requested a review from samcoe October 12, 2020 16:32
@cristiand391
Copy link
Contributor Author

@shrihanDev Yeah, I haven't added it in the last commit so you can open another PR for it if this gets merged.

Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really solid, and almost there. I have two last requests:

  1. Can we improve the error message coming from validateConfigEntry? I think it would be super helpful to see a list of valid values that the user can choose from right in the error message so that they don't need to go searching through the documentation to find valid values.

  2. Can we add some unit tests to validateConfigEntry? Both to verify its functionality and to make sure that refactors in the future don't break its functionality.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! Almost there

@mislav mislav force-pushed the validate-git-protocol-config branch from a237291 to 14a222b Compare October 16, 2020 18:55
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates! πŸ™‡

)

var configValues = map[string][]string{
"git_protocol": {"ssh", "https"},
Copy link
Contributor

@shrihankp shrihankp Oct 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we could also add

"prompt": {"enabled", "disabled"}

Maybe, in another PR, we can add more values to this map.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably go in a follow up PR πŸ‘

Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘ This looks great! I like having the caller format the error message, much cleaner.

@shrihankp
Copy link
Contributor

shrihankp commented Oct 19, 2020

This is ready to be gh pr merge-d πŸ˜„

@samcoe samcoe merged commit 5a8c071 into cli:trunk Oct 20, 2020
@cristiand391 cristiand391 deleted the validate-git-protocol-config branch October 20, 2020 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

do not allow setting invalid git_protocol

5 participants