Skip to content

Commit

Permalink
Update format method signature and refactor redact to `createReda…
Browse files Browse the repository at this point in the history
…ctor` for clarity (#2)
  • Loading branch information
plar authored Feb 1, 2024
1 parent 87380cd commit b59d7df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import "strings"
// ArgumentFormatter defines a function that formats a command argument to the string.
type ArgumentFormatter func(a Argument) string

// format combines arguments using ArgumentFormatter.
func (f ArgumentFormatter) format(args []Argument) []string {
// formatArguments formats the given arguments using the provided formatter.
func formatArguments(f ArgumentFormatter, args []Argument) []string {
var c []string
for _, arg := range args {
c = append(c, f(arg))
Expand Down
14 changes: 7 additions & 7 deletions safecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type Redactor interface {
String() string
}

// redactor defines a function that converts value to Redactor.
type redactor func(value string) Redactor
// createRedactor creates a new Redactor from the given value.
type createRedactor func(value string) Redactor

// Argument stores a key=value pair where the value is subject to redaction.
type Argument struct {
Expand Down Expand Up @@ -91,23 +91,23 @@ type Builder struct {
}

// append adds a single argument to the builder with a custom redactor.
func (b *Builder) append(key, value string, redactor redactor) {
func (b *Builder) append(key, value string, redactor createRedactor) {
b.Args = append(b.Args, Argument{
Key: key,
Value: redactor(value),
})
}

// appendValues adds values to the builder with a custom redactor.
func (b *Builder) appendValues(values []string, redactor redactor) *Builder {
func (b *Builder) appendValues(values []string, redactor createRedactor) *Builder {
for _, value := range values {
b.append("", value, redactor)
}
return b
}

// appendKeyValuePairs adds key=value pairs to the builder with a custom redactor.
func (b *Builder) appendKeyValuePairs(kvPairs []string, redactor redactor) *Builder {
func (b *Builder) appendKeyValuePairs(kvPairs []string, redactor createRedactor) *Builder {
for i := 0; i < len(kvPairs); i += 2 {
key, value := kvPairs[i], ""
if i+1 < len(kvPairs) {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (b *Builder) Arguments() []Argument {

// Build builds and returns the command.
func (b *Builder) Build() []string {
return b.Formatter.format(b.Args)
return formatArguments(b.Formatter, b.Args)
}

// String returns a string representation of the builder with hidden sensitive fields.
Expand All @@ -174,7 +174,7 @@ type Logger struct {

// Log builds the loggable command string from the command arguments.
func (l *Logger) Log() string {
c := l.Formatter.format(l.Command.Arguments())
c := formatArguments(l.Formatter, l.Command.Arguments())
return strings.Join(c, " ")
}

Expand Down

0 comments on commit b59d7df

Please sign in to comment.