-
Notifications
You must be signed in to change notification settings - Fork 13
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
Conversation
788b290
to
2c1dd94
Compare
945ac9a
to
736327c
Compare
bind/flag.go
Outdated
@@ -25,6 +27,13 @@ import ( | |||
"github.com/spf13/pflag" | |||
) | |||
|
|||
type Format int |
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.
Let's name it DescribeFormat
. This is bind
package and bind.Format
might be misleading.
bind/flag.go
Outdated
const ( | ||
Plain Format = 0 | ||
JSON Format = 1 | ||
) |
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.
nit:
const (
Plain Format = iota
JSON
)
bind/flag.go
Outdated
args := make(map[string]any, 0) | ||
keys := make([]string, 0) |
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.
nit: use fs.NFlag() to preallocate
cmd/forwarder/run/run.go
Outdated
@@ -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) |
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.
How about we stick to the Plain format 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.
Oof, yes. Thanks for catching that.
How about we move it to a separate file e.g. |
736327c
to
9b1e25c
Compare
@Choraden Make sense, I've moved the function to a new file. |
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()) |
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.
keys := make([]string, 0, fs.NFlag())
- allocate the capacity, but make it 0 elements
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.
Ah, gotcha.
This is useful for exporting or recording config settings.
9b1e25c
to
266f93e
Compare
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.
LGTM
This PR adds an option to describe flags in JSON format.
This is useful for exporting or recording config settings.