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

utils/cobrautil: describe flags unredacted #480

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions utils/cobrautil/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func DescribeFlags(fs *pflag.FlagSet, format DescribeFormat) (string, error) {

type FlagsDescriber struct {
Format DescribeFormat
Unredacted bool
ShowNotChanged bool
ShowHidden bool
}
Expand All @@ -54,17 +55,24 @@ func (d FlagsDescriber) DescribeFlags(fs *pflag.FlagSet) (string, error) {
return
}

if f.Value.Type() == "bool" {
args[f.Name] = f.Value
val := f.Value
if d.Unredacted {
if v, ok := f.Value.(redactedValue); ok {
val = v.Unredacted()
}
}

if val.Type() == "bool" {
args[f.Name] = val
} else {
if sv, ok := f.Value.(sliceValue); ok {
if sv, ok := val.(sliceValue); ok {
if d.Format == Plain {
args[f.Name] = strings.Join(sv.GetSlice(), ",")
} else {
args[f.Name] = sv.GetSlice()
}
} else {
args[f.Name] = f.Value.String()
args[f.Name] = val.String()
}
}
})
Expand Down Expand Up @@ -100,3 +108,7 @@ func (d FlagsDescriber) DescribeFlags(fs *pflag.FlagSet) (string, error) {
type sliceValue interface {
GetSlice() []string
}

type redactedValue interface {
Unredacted() pflag.Value
}
47 changes: 47 additions & 0 deletions utils/cobrautil/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ d=false`,
`key=false`,
`key=val`,
`key=false`,
`a=val
b=redacted`,
`a=val
b=val`,
`a=val`,
`key=false`,
``,
Expand All @@ -36,6 +40,8 @@ func TestDescribeFlagsAsJSON(t *testing.T) {
`{"key":false}`,
`{"key":"val"}`,
`{"key":false}`,
`{"a":"val","b":"redacted"}`,
`{"a":"val","b":"val"}`,
`{"a":"val"}`,
`{"key":false}`,
`{}`,
Expand All @@ -52,6 +58,10 @@ d: false`,
`key: false`,
`key: val`,
`key: false`,
`a: val
b: redacted`,
`a: val
b: val`,
`a: val`,
`key: false`,
`{}`,
Expand Down Expand Up @@ -103,6 +113,31 @@ func testDescribeFlags(t *testing.T, f DescribeFormat, expected []string) { //no
return fs
},
},
{
name: "value is redacted",
flags: func() *pflag.FlagSet {
fs := pflag.NewFlagSet("flags", pflag.ContinueOnError)
fs.String("a", "", "")
v := mockRedactedValue{fs.Lookup("a").Value}
fs.Var(&v, "b", "")
fs.Set("a", "val")
return fs
},
},
{
name: "value is unredacted",
flags: func() *pflag.FlagSet {
fs := pflag.NewFlagSet("flags", pflag.ContinueOnError)
fs.String("a", "", "")
v := mockRedactedValue{fs.Lookup("a").Value}
fs.Var(&v, "b", "")
fs.Set("a", "val")
return fs
},
decorate: func(d *FlagsDescriber) {
d.Unredacted = true
},
},
{
name: "not changed is not shown",
flags: func() *pflag.FlagSet {
Expand Down Expand Up @@ -168,3 +203,15 @@ func testDescribeFlags(t *testing.T, f DescribeFormat, expected []string) { //no
})
}
}

type mockRedactedValue struct {
pflag.Value
}

func (v mockRedactedValue) Unredacted() pflag.Value {
return v.Value
}

func (v mockRedactedValue) String() string {
return "redacted"
}