diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4f0c385..0359bd3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,7 @@ +# Contributing to OpenFeature CLI + +Thank you for your interest in contributing to the OpenFeature CLI! This document provides guidelines and instructions to help you get started with contributing to the project. Whether you're fixing a bug, adding a new feature, or improving documentation, your contributions are greatly appreciated. + ## Contributing New Generators We welcome contributions for new generators to extend the functionality of the OpenFeature CLI. Below are the steps to contribute a new generator: @@ -42,6 +46,7 @@ make test-csharp ``` This will: + 1. Build the CLI 2. Generate a C# client 3. Compile the C# code in a Docker container @@ -49,6 +54,55 @@ This will: Consider adding more integration tests for new generators. +## Setting Up Lefthook + +To streamline the setup of Git hooks for this project, we utilize [Lefthook](https://github.com/evilmartians/lefthook). Lefthook automates pre-commit and pre-push checks, ensuring consistent enforcement of best practices across the team. These checks include code formatting, documentation generation, and running tests. + +This tool is particularly helpful for new contributors or those returning to the project after some time, as it provides a seamless way to align with the project's standards. By catching issues early in your local development environment, Lefthook helps you address potential problems before opening a Pull Request, saving time and effort for both contributors and maintainers. + +### Installation + +1. Install Lefthook using Homebrew: + + ```bash + brew install lefthook + ``` + +2. Install the Lefthook configuration into your Git repository: + + ```bash + lefthook install + ``` + +### Pre-Commit Hook + +The pre-commit hook is configured to run the following check: + +1. **Code Formatting**: Ensures all files are properly formatted using `go fmt`. Any changes made by `go fmt` will be automatically staged. + +### Pre-Push Hook + +The pre-push hook is configured to run the following checks: + +1. **Documentation Generation**: Runs `make generate-docs` to ensure documentation is up-to-date. If any changes are detected, the push will be blocked until the changes are committed. +2. **Tests**: Executes `make test` to verify that all tests pass. If any tests fail, the push will be blocked. + +### Running Hooks Manually + +You can manually run the hooks using the following commands: + +- Pre-commit hook: + + ```bash + lefthook run pre-commit + ``` + +- Pre-push hook: + + ```bash + lefthook run pre-push + ``` + ## Templates ### Data diff --git a/Makefile b/Makefile index 5e87ad0..2b68a76 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ - .PHONY: test test: @echo "Running tests..." @@ -18,4 +17,10 @@ generate-docs: generate-schema: @echo "Generating schema..." @go run ./schema/generate-schema.go - @echo "Schema generated successfully!" \ No newline at end of file + @echo "Schema generated successfully!" + +.PHONY: fmt +fmt: + @echo "Running go fmt..." + @go fmt ./... + @echo "Code formatted successfully!" \ No newline at end of file diff --git a/internal/flagset/flagset.go b/internal/flagset/flagset.go index 33d3155..b96eca3 100644 --- a/internal/flagset/flagset.go +++ b/internal/flagset/flagset.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "sort" + "strings" "github.com/open-feature/cli/internal/filesystem" "github.com/open-feature/cli/internal/manifest" @@ -64,7 +65,7 @@ func Load(manifestPath string) (*Flagset, error) { if err != nil { return nil, err } else if len(validationErrors) > 0 { - return nil, fmt.Errorf("validation failed: %v", validationErrors) + return nil, errors.New(FormatValidationError(validationErrors)) } var flagset Flagset @@ -132,3 +133,43 @@ func (fs *Flagset) UnmarshalJSON(data []byte) error { return nil } +func FormatValidationError(issues []manifest.ValidationError) string { + var sb strings.Builder + sb.WriteString("flag manifest validation failed:\n\n") + + // Group messages by flag path + grouped := make(map[string]struct { + flagType string + messages []string + }) + + for _, issue := range issues { + entry := grouped[issue.Path] + entry.flagType = issue.Type + entry.messages = append(entry.messages, issue.Message) + grouped[issue.Path] = entry + } + + // Sort paths for consistent output + paths := make([]string, 0, len(grouped)) + for path := range grouped { + paths = append(paths, path) + } + sort.Strings(paths) + + // Format each row + for _, path := range paths { + entry := grouped[path] + flagType := entry.flagType + if flagType == "" { + flagType = "missing" + } + sb.WriteString(fmt.Sprintf( + "- flagType: %s\n flagPath: %s\n errors:\n ~ %s\n \tSuggestions:\n \t- flagType: boolean\n \t- defaultValue: true\n\n", + flagType, + path, + strings.Join(entry.messages, "\n ~ "), + )) + } + return sb.String() +} diff --git a/lefthook.yml b/lefthook.yml new file mode 100644 index 0000000..758bdac --- /dev/null +++ b/lefthook.yml @@ -0,0 +1,21 @@ +# This file configures Lefthook, a Git hooks manager, for the project. +# For detailed instructions on how to contribute and set up Lefthook, +# please refer to the relevant section in the contributing documentation (CONTRIBUTING.md). +pre-commit: + commands: + go-fmt: + run: go fmt ./... + stage_fixed: true +pre-push: + commands: + generate-docs: + run: | + make generate-docs + if ! git diff --quiet; then + echo "Documentation is outdated. Please run 'make generate-docs' and commit the changes." + exit 1 + fi + skip: false + tests: + run: make test + skip: false