Skip to content
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

Proposal: refactor validtors of cobra commond args #478

Closed
aFlyBird0 opened this issue May 12, 2022 · 6 comments
Closed

Proposal: refactor validtors of cobra commond args #478

aFlyBird0 opened this issue May 12, 2022 · 6 comments

Comments

@aFlyBird0
Copy link
Member

aFlyBird0 commented May 12, 2022

Description

To make validators more elegant:

  1. I want to separate args validators and important business code.
  2. I want to reduce duplicate code of validators.

Describe the Proposal

  1. For code separate
    take show cmd for example:
func showCMDFunc(cmd *cobra.Command, args []string) {
	if err := validateShowArgs(args); err != nil {
		log.Fatal(err)
	}

	// There is important business code
}

I will delete code like if err:= validate...Show(args); err!=nil{log.Fatal(err) in all commonds.

New code will be like:

var showCMD = &cobra.Command{
	Use:   "...",
	Short: "...",
	Long: `...`,
	Run: WithValidators(showCMDFunc, validator1, validator2),
}

func showCMDFunc(cmd *cobra.Command, args []string) {
	// Only important business code and following business errors
}
  1. For duplicate code
    There is duplicate code like:
if len(args) != 1 {
		return fmt.Errorf("illegal args count (expect 1, got %d)", len(args))
	}

I will define some common validators to program easier and keep error messages consistent.

However, I'am not sure where to put code about validators interface, maybe /cmd/devstream/validator/validator.go ? Or just in /cmd/devstream/validator.go to hide pkg names. Can you give me some advice? @daniel-hutao

Has the Feature been Requested Before?

Describe the Alternatives You Have Considered

Additional Context

@daniel-hutao
Copy link
Member

daniel-hutao commented May 13, 2022

/cmd/devstream/validator/validator.go looks better. Because maybe we'll add many validators in some separated go file under the validator dir. @aFlyBird0

@IronCore864
Copy link
Member

Hi @aFlyBird0, thanks for your enthusiastic contribution!

I have read your PR already; sorry for the late reply. I'll reply directly to the issue instead of in the PR.

We follow the standard go project layout, and according to those conventions, the cmd folder shouldn't contain a lot of code.

If you think the code can be imported and used in other projects, then it should live in the /pkg directory. If the code is not reusable or if you don't want others to reuse it, put that code in the /internal directory.

In this case, I think the validator.go file is better to be put under /internal/pkg. Maybe even a new subfolder there?

Feel free to have a discussion here or in our WeChat user group or slack channel :)

Thanks again @aFlyBird0

@daniel-hutao
Copy link
Member

@aFlyBird0

@daniel-hutao
Copy link
Member

@IronCore864 @aFlyBird0

The validator is a kind of entry logic. So it is more suitable under cmd. For example, k8s does this:

RUq0Rl59Vl

@aFlyBird0
Copy link
Member Author

Thank you both for your advice. @daniel-hutao @IronCore864 I have finished coding

@daniel-hutao
Copy link
Member

closed by #479

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

No branches or pull requests

3 participants