-
Notifications
You must be signed in to change notification settings - Fork 18
feat: manifest command group for adding and listing flags in manifest #168
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
Merged
beeme1mr
merged 4 commits into
open-feature:main
from
georgesdugue:feat/153-manifest-add-flag
Oct 14, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f07376e
feat(manifest): add command to manage flag manifests with add functio…
georgesdugue 620af57
feat(manifest): add 'list' command to display all flags in the manifest
georgesdugue f4dcd5f
chore(manifest): doc generation for 'manifest add' and 'manifest list…
georgesdugue a4097cf
Merge branch 'main' into feat/153-manifest-add-flag
beeme1mr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func GetManifestCmd() *cobra.Command { | ||
| manifestCmd := &cobra.Command{ | ||
| Use: "manifest", | ||
| Short: "Manage flag manifest files", | ||
| Long: `Commands for managing OpenFeature flag manifest files.`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Help() | ||
| }, | ||
| SilenceErrors: true, | ||
| SilenceUsage: true, | ||
| DisableSuggestions: false, | ||
| SuggestionsMinimumDistance: 2, | ||
| } | ||
|
|
||
| // Add subcommands | ||
| manifestCmd.AddCommand(GetManifestAddCmd()) | ||
| manifestCmd.AddCommand(GetManifestListCmd()) | ||
|
|
||
| addStabilityInfo(manifestCmd) | ||
|
|
||
| return manifestCmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/open-feature/cli/internal/config" | ||
| "github.com/open-feature/cli/internal/filesystem" | ||
| "github.com/open-feature/cli/internal/flagset" | ||
| "github.com/open-feature/cli/internal/logger" | ||
| "github.com/open-feature/cli/internal/manifest" | ||
| "github.com/pterm/pterm" | ||
| "github.com/spf13/afero" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func GetManifestAddCmd() *cobra.Command { | ||
| manifestAddCmd := &cobra.Command{ | ||
| Use: "add [flag-name]", | ||
| Short: "Add a new flag to the manifest", | ||
| Long: `Add a new flag to the manifest file with the specified configuration. | ||
|
|
||
| Examples: | ||
| # Add a boolean flag (default type) | ||
| openfeature manifest add new-feature --default-value false | ||
|
|
||
| # Add a string flag with description | ||
| openfeature manifest add welcome-message --type string --default-value "Hello!" --description "Welcome message for users" | ||
|
|
||
| # Add an integer flag | ||
| openfeature manifest add max-retries --type integer --default-value 3 | ||
|
|
||
| # Add a float flag | ||
| openfeature manifest add discount-rate --type float --default-value 0.15 | ||
|
|
||
| # Add an object flag | ||
| openfeature manifest add config --type object --default-value '{"key":"value"}'`, | ||
| Args: cobra.ExactArgs(1), | ||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||
| return initializeConfig(cmd, "manifest.add") | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| flagName := args[0] | ||
| manifestPath := config.GetManifestPath(cmd) | ||
|
|
||
| // Get flag configuration from command flags | ||
| flagType, _ := cmd.Flags().GetString("type") | ||
| defaultValueStr, _ := cmd.Flags().GetString("default-value") | ||
| description, _ := cmd.Flags().GetString("description") | ||
|
|
||
| // Validate that default-value is provided | ||
| if !cmd.Flags().Changed("default-value") { | ||
| return errors.New("--default-value is required") | ||
| } | ||
|
|
||
| // Parse flag type | ||
| parsedType, err := parseFlagTypeString(flagType) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid flag type: %w", err) | ||
| } | ||
|
|
||
| // Parse and validate default value | ||
| defaultValue, err := parseDefaultValue(defaultValueStr, parsedType) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid default value for type %s: %w", flagType, err) | ||
| } | ||
|
|
||
| // Load existing manifest | ||
| var fs *flagset.Flagset | ||
| exists, err := afero.Exists(filesystem.FileSystem(), manifestPath) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to check manifest existence: %w", err) | ||
| } | ||
|
|
||
| if exists { | ||
| fs, err = manifest.LoadFlagSet(manifestPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load manifest: %w", err) | ||
| } | ||
| } else { | ||
| // If manifest doesn't exist, create a new one | ||
| fs = &flagset.Flagset{ | ||
| Flags: []flagset.Flag{}, | ||
| } | ||
| } | ||
|
|
||
| // Check if flag already exists | ||
| for _, flag := range fs.Flags { | ||
| if flag.Key == flagName { | ||
| return fmt.Errorf("flag '%s' already exists in the manifest", flagName) | ||
| } | ||
| } | ||
|
|
||
| // Add new flag | ||
| newFlag := flagset.Flag{ | ||
| Key: flagName, | ||
| Type: parsedType, | ||
| Description: description, | ||
| DefaultValue: defaultValue, | ||
| } | ||
| fs.Flags = append(fs.Flags, newFlag) | ||
|
|
||
| // Write updated manifest | ||
| if err := manifest.Write(manifestPath, *fs); err != nil { | ||
| return fmt.Errorf("failed to write manifest: %w", err) | ||
| } | ||
|
|
||
| // Success message | ||
| pterm.Success.Printfln("Flag '%s' added successfully to %s", flagName, manifestPath) | ||
| logger.Default.Debug(fmt.Sprintf("Added flag: name=%s, type=%s, defaultValue=%v, description=%s", | ||
| flagName, flagType, defaultValue, description)) | ||
|
|
||
| // Display all current flags | ||
| displayFlagList(fs, manifestPath) | ||
beeme1mr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pterm.Println("Use the 'generate' command to update type-safe clients with the new flag.") | ||
| pterm.Println() | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| // Add command-specific flags | ||
| config.AddManifestAddFlags(manifestAddCmd) | ||
| addStabilityInfo(manifestAddCmd) | ||
|
|
||
| return manifestAddCmd | ||
| } | ||
|
|
||
| // parseFlagTypeString converts a string flag type to FlagType enum | ||
| func parseFlagTypeString(typeStr string) (flagset.FlagType, error) { | ||
| switch strings.ToLower(typeStr) { | ||
| case "boolean", "bool": | ||
georgesdugue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return flagset.BoolType, nil | ||
| case "string": | ||
| return flagset.StringType, nil | ||
| case "integer", "int": | ||
| return flagset.IntType, nil | ||
| case "float", "number": | ||
beeme1mr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return flagset.FloatType, nil | ||
| case "object", "json": | ||
| return flagset.ObjectType, nil | ||
| default: | ||
| return flagset.UnknownFlagType, fmt.Errorf("unknown flag type: %s", typeStr) | ||
| } | ||
| } | ||
|
|
||
| // parseDefaultValue parses and validates the default value based on flag type | ||
| func parseDefaultValue(value string, flagType flagset.FlagType) (interface{}, error) { | ||
| switch flagType { | ||
| case flagset.BoolType: | ||
| switch strings.ToLower(value) { | ||
| case "true": | ||
| return true, nil | ||
| case "false": | ||
| return false, nil | ||
| default: | ||
| return nil, fmt.Errorf("boolean value must be 'true' or 'false', got '%s'", value) | ||
| } | ||
| case flagset.StringType: | ||
| return value, nil | ||
| case flagset.IntType: | ||
| intVal, err := strconv.Atoi(value) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid integer value: %s", value) | ||
| } | ||
| return intVal, nil | ||
| case flagset.FloatType: | ||
| floatVal, err := strconv.ParseFloat(value, 64) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid float value: %s", value) | ||
| } | ||
| return floatVal, nil | ||
| case flagset.ObjectType: | ||
| var jsonObj interface{} | ||
| if err := json.Unmarshal([]byte(value), &jsonObj); err != nil { | ||
| return nil, fmt.Errorf("invalid JSON object: %s", err.Error()) | ||
| } | ||
| return jsonObj, nil | ||
| default: | ||
| return nil, fmt.Errorf("unsupported flag type: %v", flagType) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.