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

bind: Add JSON parameter to DescribeFlags #370

Merged
merged 1 commit into from
Aug 28, 2023

Conversation

alexh-sauce
Copy link
Contributor

@alexh-sauce alexh-sauce commented Aug 25, 2023

This PR adds an option to describe flags in JSON format.

This is useful for exporting or recording config settings.

@alexh-sauce alexh-sauce force-pushed the alexh/add-describe-flags-as-json branch 2 times, most recently from 788b290 to 2c1dd94 Compare August 25, 2023 19:39
@alexh-sauce alexh-sauce changed the title bind: Add DescribeFlagsAsJSON bind: Add JSON parameter to DescribeFlags Aug 25, 2023
@alexh-sauce alexh-sauce force-pushed the alexh/add-describe-flags-as-json branch 2 times, most recently from 945ac9a to 736327c Compare August 25, 2023 19:46
bind/flag.go Outdated
@@ -25,6 +27,13 @@ import (
"github.com/spf13/pflag"
)

type Format int
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's name it DescribeFormat. This is bind package and bind.Format might be misleading.

bind/flag.go Outdated
Comment on lines 32 to 35
const (
Plain Format = 0
JSON Format = 1
)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

const (
	Plain Format = iota
	JSON
)

bind/flag.go Outdated Show resolved Hide resolved
bind/flag.go Outdated
Comment on lines 341 to 342
args := make(map[string]any, 0)
keys := make([]string, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use fs.NFlag() to preallocate

@@ -43,7 +43,10 @@ type command struct {
}

func (c *command) RunE(cmd *cobra.Command, _ []string) error {
config := bind.DescribeFlags(cmd.Flags())
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.JSON)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about we stick to the Plain format here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oof, yes. Thanks for catching that.

@Choraden
Copy link
Contributor

How about we move it to a separate file e.g. describe.go?

@alexh-sauce alexh-sauce force-pushed the alexh/add-describe-flags-as-json branch from 736327c to 9b1e25c Compare August 28, 2023 17:05
@alexh-sauce
Copy link
Contributor Author

@Choraden Make sense, I've moved the function to a new file.

@alexh-sauce alexh-sauce requested a review from Choraden August 28, 2023 17:09
bind/describe.go Outdated

func DescribeFlags(fs *pflag.FlagSet, showHidden bool, format DescribeFormat) (string, error) {
args := make(map[string]any, fs.NFlag())
keys := make([]string, fs.NFlag())
Copy link
Contributor

Choose a reason for hiding this comment

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

keys := make([]string, 0, fs.NFlag()) - allocate the capacity, but make it 0 elements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, gotcha.

This is useful for exporting or recording config settings.
@alexh-sauce alexh-sauce force-pushed the alexh/add-describe-flags-as-json branch from 9b1e25c to 266f93e Compare August 28, 2023 17:38
Copy link
Contributor

@Choraden Choraden left a comment

Choose a reason for hiding this comment

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

LGTM

@alexh-sauce alexh-sauce merged commit 6061b8e into main Aug 28, 2023
@alexh-sauce alexh-sauce deleted the alexh/add-describe-flags-as-json branch August 28, 2023 17:46
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.

2 participants