-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Create openfeature pull command
#75
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
Closed
jsalaber
wants to merge
3
commits into
open-feature:refactor-manifest
from
jsalaber:feat/openfeature-pull
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,106 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "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/manifest" | ||
| "github.com/open-feature/cli/internal/requests" | ||
| "github.com/pterm/pterm" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func promptForDefaultValue(flag *flagset.Flag) (any) { | ||
| var prompt string | ||
| switch flag.Type { | ||
| case flagset.BoolType: | ||
| var options []string = []string{"false", "true"} | ||
| prompt = fmt.Sprintf("Enter default value for flag '%s' (%s)", flag.Key, flag.Type) | ||
| boolStr, _ := pterm.DefaultInteractiveSelect.WithOptions(options).WithFilter(false).Show(prompt) | ||
| boolValue, _ := strconv.ParseBool(boolStr) | ||
| return boolValue | ||
| case flagset.IntType: | ||
| var err error = errors.New("Input a valid integer") | ||
| prompt = fmt.Sprintf("Enter default value for flag '%s' (%s)", flag.Key, flag.Type) | ||
| var defaultValue int | ||
| for err != nil { | ||
| defaultValueString, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("0").Show(prompt) | ||
| defaultValue, err = strconv.Atoi(defaultValueString) | ||
| } | ||
| return defaultValue | ||
| case flagset.FloatType: | ||
| var err error = errors.New("Input a valid float") | ||
| prompt = fmt.Sprintf("Enter default value for flag '%s' (%s)", flag.Key, flag.Type) | ||
| var defaultValue float64 | ||
| for err != nil { | ||
| defaultValueString, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("0.0").Show(prompt) | ||
| defaultValue, err = strconv.ParseFloat(defaultValueString, 64) | ||
| if err != nil { | ||
| pterm.Error.Println("Input a valid float") | ||
| } | ||
| } | ||
| return defaultValue | ||
| case flagset.StringType: | ||
| prompt = fmt.Sprintf("Enter default value for flag '%s' (%s)", flag.Key, flag.Type) | ||
| defaultValue, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("").Show(prompt) | ||
| return defaultValue | ||
| // TODO: Add proper support for object type | ||
| case flagset.ObjectType: | ||
| return map[string]any{} | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func GetPullCmd() *cobra.Command { | ||
| pullCmd := &cobra.Command{ | ||
| Use: "pull", | ||
| Short: "Pull a flag manifest from a remote source", | ||
| Long: "Pull a flag manifest from a remote source.", | ||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||
| return initializeConfig(cmd, "pull") | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| flagSourceUrl := config.GetFlagSourceUrl(cmd) | ||
| manifestPath := config.GetManifestPath(cmd) | ||
| authToken := config.GetAuthToken(cmd) | ||
|
|
||
| if flagSourceUrl == "" { | ||
| url, err := filesystem.GetFromYaml("flagSourceUrl") | ||
| if err != nil { | ||
| return fmt.Errorf("error getting flagSourceUrl from config: %w", err) | ||
| } | ||
| flagSourceUrl = url | ||
| } | ||
|
|
||
| // fetch the flags from the remote source | ||
| flags, err := requests.FetchFlags(flagSourceUrl, authToken) | ||
| if err != nil { | ||
| return fmt.Errorf("error fetching flags: %w", err) | ||
| } | ||
|
|
||
| // Check each flag for null defaultValue | ||
| for index, flag := range flags.Flags { | ||
| if flag.DefaultValue == nil { | ||
| defaultValue := promptForDefaultValue(&flag) | ||
| flags.Flags[index].DefaultValue = defaultValue | ||
| } | ||
| } | ||
|
|
||
| pterm.Success.Printf("Successfully fetched flags from %s", flagSourceUrl) | ||
| err = manifest.Write(manifestPath, flags) | ||
| if err != nil { | ||
| return fmt.Errorf("error writing manifest: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| config.AddPullFlags(pullCmd) | ||
| return pullCmd | ||
| } | ||
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
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 |
|---|---|---|
|
|
@@ -8,10 +8,15 @@ import ( | |
|
|
||
| "github.com/spf13/afero" | ||
| "github.com/spf13/viper" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| var viperKey = "filesystem" | ||
|
|
||
| type Config struct { | ||
| FlagSourceUrl string `yaml:"flagSourceUrl"` | ||
| } | ||
|
|
||
| // Get the filesystem interface from the viper configuration. | ||
| // If the filesystem interface is not set, the default filesystem interface is returned. | ||
| func FileSystem() afero.Fs { | ||
|
|
@@ -48,6 +53,31 @@ func WriteFile(path string, data []byte) error { | |
| return nil | ||
| } | ||
|
|
||
| func ReadFile(path string) ([]byte, error) { | ||
| fs := FileSystem() | ||
| return afero.ReadFile(fs, path) | ||
| } | ||
|
|
||
| func GetFromYaml(key string) (string, error) { | ||
| var config map[string]string | ||
| fs, err := ReadFile(".openfeature.yaml") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| err = yaml.Unmarshal(fs, &config) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| value, exists := config[key] | ||
| if !exists { | ||
| return "", fmt.Errorf("unknown key: %s", key) | ||
|
Collaborator
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. I'm wondering if we could drop the switch statements if we take the map from unmarshal and do a key/value lookup on it. Then maybe we could return with something like this: |
||
| } | ||
|
|
||
| return value, nil | ||
| } | ||
|
|
||
| // Checks if a file exists at the given path using the filesystem interface. | ||
| func Exists(path string) (bool, error) { | ||
| fs := FileSystem() | ||
|
|
||
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
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 love the UX here, well done 🥇