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

chore: add all sorts of linters and pre-commit hook #257

Merged
merged 2 commits into from
Dec 5, 2018

Conversation

zregvart
Copy link
Member

@zregvart zregvart commented Dec 5, 2018

This adds a myriad of linters from golangci/golangci-lint[1] and a pre-commit hook to execute them on git commit. Some lint checks needed to be disabled to keep the PR not to big to merge.

To install pre-commit look at the Installation instructions[2] of pre-commit project.

To install golangci-lint follow the Local Installation instructions[3].

[1] https://github.com/golangci/golangci-lint
[2] https://pre-commit.com/#install
[3] https://github.com/golangci/golangci-lint#local-installation

Copy link
Contributor

@oscerd oscerd left a comment

Choose a reason for hiding this comment

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

LGTM, lets see how much we are able to support the pedantic reports :-)

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Dec 5, 2018

for what it's worth, I do not like at all installing pre commit hooks :)

One of the reason is that I do commit often, maybe an incomplete work or something I know is not yet ready and having something that do fail to commit hurts me.

It would be better to have this as something we can run with make and/or something we can configure (and eventually disable) in our IDE.

@oscerd
Copy link
Contributor

oscerd commented Dec 5, 2018

why don't we check directly on CI instead of using pre-commit hooks?

@lburgazzoli
Copy link
Contributor

I think the best is to set-up lint on ci and have the option to run the checks locally
Then when and how the checks are running locally is IMHO a matter of preference and personal workflow.

@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

for what it's worth, I do not like at all installing pre commit hooks :)

Then don't add them to your local .git :)

@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

Was --cluster-setup removed?

From CI:

installing camel k cluster resources
Error: unknown flag: --cluster-setup
The command "./build/travis_build.sh" exited with 1.

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Dec 5, 2018 via email

@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

The pre commit hook config is part of the pr and the docs seems to require to set it up, I know I can skip them but imo it is better to put it as make target and eventually document how to set up hooks as part of the workfkow. Putting the pre commit hook config in the repo has also the drawback that may conflict whit others hook one may have as part of the personal workfkow.

I can update the docs to make it clear that installing pre-commit hook is optional and depends on personal preference. Adding .pre-commit-config.yaml should not affect any personal workflow you have, as it is read by the pre-commit hook installed via pre-commit framework.

Don't install pre-commit framework or don't run pre-commit install and your workflow should not be changed.

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Dec 5, 2018 via email

@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

if one has alteady a pre commit hook configured for camel-k, then the set up put in the repo will conflict with it, correct ?

Yes, if you a) install pre-commit, the framework and b) run pre-commit install.

@lburgazzoli
Copy link
Contributor

of course by if I have that file already it means that I have already done a and b :)

@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

of course by if I have that file already it means that I have already done a and b :)

By god man, if you have that file why don't you share it with others?

@lburgazzoli
Copy link
Contributor

Again, I may have my own workflow and I don't want to change other people workflow.

Anyway I do not have any problem to leave it in the repo but please add also a make target

...hook

This adds a myriad of linters from golangci/golangci-lint[1] and a
pre-commit hook to execute them on `git commit`. Some lint checks needed
to be disabled to keep the PR not to big to merge.

To install pre-commit look at the Installation instructions[2] of
pre-commit project.

To install golangci-lint follow the Local Installation instructions[3].

[1] https://github.com/golangci/golangci-lint
[2] https://pre-commit.com/#install
[3] https://github.com/golangci/golangci-lint#local-installation
@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

@lburgazzoli I've added make lint target and clarified the docs.

@@ -44,9 +44,12 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
cmd.Flags().StringVar(&options.registry, "registry", "", "A Docker registry that can be used to publish images")
cmd.Flags().StringVar(&options.organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
cmd.Flags().StringVar(&options.pushSecret, "push-secret", "", "A secret used to push images to the Docker registry")
cmd.ParseFlags(os.Args)
err := cmd.ParseFlags(os.Args)
Copy link
Contributor

@lburgazzoli lburgazzoli Dec 5, 2018

Choose a reason for hiding this comment

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

@zregvart I think the build problem comes from here: ParseFlags is not needed here, I guess it fails then the error is propagated up to the caller make so the command fails.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should I just remove it? Lemme see...

Copy link
Contributor

Choose a reason for hiding this comment

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

yes

@@ -57,21 +58,27 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) {
cmd.PersistentFlags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace to use for all operations")

// Parse the flags before setting the defaults
cmd.ParseFlags(os.Args)
err := cmd.ParseFlags(os.Args)
Copy link
Contributor

Choose a reason for hiding this comment

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

after digging a little bit more this one is also failing, don't know why btw

Copy link
Contributor

Choose a reason for hiding this comment

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

i guess we should ignore the error for the moment

@zregvart
Copy link
Member Author

zregvart commented Dec 5, 2018

@lburgazzoli I needed to remove the other ParseFlags in:

// Parse the flags before setting the defaults
cmd.ParseFlags(os.Args)

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Dec 5, 2018

@zregvart digget into it a little more, I think you can replace the method with something like:

// NewKamelCommand --
func NewKamelCommand(ctx context.Context) (*cobra.Command, error) {
	options := RootCmdOptions{
		Context: ctx,
	}
	var cmd = cobra.Command{
		Use:                    "kamel",
		Short:                  "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes",
		Long:                   kamelCommandLongDescription,
		BashCompletionFunction: bashCompletionFunction,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
			if options.Namespace == "" {
				current, err := kubernetes.GetClientCurrentNamespace(options.KubeConfig)
				if err != nil {
					return errors.Wrap(err, "cannot get current namespace")
				}
				err = cmd.Flag("namespace").Value.Set(current)
				if err != nil {
					return err
				}
			}

			// Let's use a fast refresh period when running with the CLI
			k8sclient.ResetCacheEvery(2 * time.Second)

			// Initialize the Kubernetes client to allow using the operator-sdk
			return kubernetes.InitKubeClient(options.KubeConfig)
		},
	}

	cmd.PersistentFlags().StringVar(&options.KubeConfig, "config", "", "Path to the config file to use for CLI requests")
	cmd.PersistentFlags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace to use for all operations")

	cmd.AddCommand(newCmdCompletion(&cmd))
	cmd.AddCommand(newCmdVersion())
	cmd.AddCommand(newCmdRun(&options))
	cmd.AddCommand(newCmdGet(&options))
	cmd.AddCommand(newCmdDelete(&options))
	cmd.AddCommand(newCmdInstall(&options))
	cmd.AddCommand(newCmdLog(&options))
	cmd.AddCommand(newCmdContext(&options))

	return &cmd, nil
}

This hooks setting up defaults and initializing the kubernetes client into the cobra lifecycle

@lburgazzoli lburgazzoli merged commit ee8dee2 into apache:master Dec 5, 2018
@zregvart zregvart deleted the pr/lint-lint-lint branch December 5, 2018 16:06
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.

None yet

3 participants