-
Notifications
You must be signed in to change notification settings - Fork 18
feat(cli): add manifest delete command #191
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
Open
reckziegelwilliam
wants to merge
1
commit into
open-feature:main
Choose a base branch
from
reckziegelwilliam:feat/manifest-delete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+559
−0
Open
Changes from all commits
Commits
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
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,90 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package cmd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/open-feature/cli/internal/config" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/open-feature/cli/internal/filesystem" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "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 GetManifestDeleteCmd() *cobra.Command { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manifestDeleteCmd := &cobra.Command{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Use: "delete <flag-name>", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Short: "Delete a flag from the manifest", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Long: `Delete a flag from the manifest file by its key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Delete a flag named 'old-feature' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| openfeature manifest delete old-feature | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Delete a flag from a specific manifest file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| openfeature manifest delete old-feature --manifest path/to/flags.json`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: cobra.ExactArgs(1), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return initializeConfig(cmd, "manifest.delete") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flagName := args[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manifestPath := config.GetManifestPath(cmd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if manifest exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exists, err := afero.Exists(filesystem.FileSystem(), manifestPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to check manifest existence: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !exists { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("manifest file does not exist: %s", manifestPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Load existing manifest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fs, err := manifest.LoadFlagSet(manifestPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to load manifest: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if manifest has any flags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(fs.Flags) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("manifest contains no flags") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if flag exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flagIndex := -1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i, flag := range fs.Flags { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if flag.Key == flagName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flagIndex = i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if flagIndex == -1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("flag '%s' not found in manifest", flagName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Remove the flag by creating a new slice without it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fs.Flags = append(fs.Flags[:flagIndex], fs.Flags[flagIndex+1:]...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The zero length check can be skipped as it's not really an edge case and is covered by the "flag not found in manifest" check. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 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' deleted successfully from %s", flagName, manifestPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.Default.Debug(fmt.Sprintf("Deleted flag: name=%s, manifestPath=%s", flagName, manifestPath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Add command-specific flags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.AddManifestDeleteFlags(manifestDeleteCmd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addStabilityInfo(manifestDeleteCmd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return manifestDeleteCmd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dig this one-liner, elegant!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slices.DeleteFuncwill be a better option here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see how I used it here in the go-sdk:
https://github.com/open-feature/go-sdk/blob/794d397d0f0c47fd917f80e4e5114a71100246cf/openfeature/event_executor.go#L94
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason to use
slices.DeleteFuncoverslices.Deletehere?I didn't mind the append pattern but if we want to change it,
.Deleteseems fine here over.DeleteFuncThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a suggestion using
slices.DeleteFunc. It allows us to get rid of theforloop entirely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oic, thanks!