Skip to content

Commit

Permalink
bind: Add JSON formatting option to DescribeFlags
Browse files Browse the repository at this point in the history
This is useful for exporting or recording config settings.
  • Loading branch information
alexh-sauce committed Aug 25, 2023
1 parent 1abd995 commit 2c1dd94
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 8 deletions.
47 changes: 42 additions & 5 deletions bind/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
package bind

import (
"encoding/json"
"fmt"
"net/netip"
"net/url"
"os"
"sort"
"strings"

"github.com/mmatczuk/anyflag"
Expand All @@ -23,6 +25,13 @@ import (
"github.com/spf13/pflag"
)

type Format int

const (
Plain Format = 0
JSON Format = 1
)

func ConfigFile(fs *pflag.FlagSet, configFile *string) {
fs.StringVarP(configFile,
"config-file", "c", *configFile, "<path>"+
Expand Down Expand Up @@ -268,13 +277,41 @@ func MarkFlagFilename(cmd *cobra.Command, names ...string) {
}
}

func DescribeFlags(fs *pflag.FlagSet) string {
var b strings.Builder
func DescribeFlags(fs *pflag.FlagSet, showHidden bool, format Format) (string, error) {
args := make(map[string]any, 0)
keys := make([]string, 0)

fs.VisitAll(func(flag *pflag.Flag) {
if flag.Hidden || flag.Name == "help" {
if flag.Name == "help" {
return
}

if flag.Hidden && !showHidden {
return
}
b.WriteString(fmt.Sprintf("%s=%s\n", flag.Name, strings.Trim(flag.Value.String(), "[]")))

if flag.Value.Type() == "bool" {
args[flag.Name] = flag.Value
} else {
args[flag.Name] = strings.Trim(flag.Value.String(), "[]")
}

keys = append(keys, flag.Name)
})
return b.String()

sort.Strings(keys)

switch format {
case Plain:
var b strings.Builder
for _, name := range keys {
b.WriteString(fmt.Sprintf("%s=%s\n", name, args[name]))
}
return b.String(), nil
case JSON:
encoded, err := json.Marshal(args)
return string(encoded), err
default:
return "", fmt.Errorf("Unknown format requested")
}
}
156 changes: 156 additions & 0 deletions bind/flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2023 Sauce Labs Inc. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

package bind

import (
"testing"

"github.com/spf13/pflag"
)

func TestDescribeFlagsAsPlain(t *testing.T) {
tests := map[string]struct {
input map[string]interface{}
expected string
isErr bool
isHidden bool
showHidden bool
}{
"keys are sorted": {
input: map[string]interface{}{"foo": false, "bar": true},
expected: "bar=true\nfoo=false\n",
isErr: false,
},
"bool is correctly formatted": {
input: map[string]interface{}{"key": false},
expected: "key=false\n",
isErr: false,
},
"string is correctly formatted": {
input: map[string]interface{}{"key": "val"},
expected: "key=val\n",
isErr: false,
},
"help is not shown": {
input: map[string]interface{}{"key": false, "help": true},
expected: "key=false\n",
isErr: false,
},
"hidden is shown": {
input: map[string]interface{}{"key": false},
expected: "key=false\n",
isErr: false,
isHidden: true,
showHidden: true,
},
"hidden is not shown": {
input: map[string]interface{}{"key": false},
expected: ``,
isErr: false,
isHidden: true,
showHidden: false,
},
}

for name, tc := range tests {
fs := pflag.NewFlagSet("flags", pflag.ContinueOnError)

for k, v := range tc.input {
switch val := v.(type) {
case bool:
fs.Bool(k, val, "")
case string:
fs.String(k, val, "")
}

if tc.isHidden {
err := fs.MarkHidden(k)
if err != nil {
t.Errorf("%s: test setup failed: %s", name, err)
}
}
}
result, err := DescribeFlags(fs, tc.showHidden, Plain)

if (err != nil) != tc.isErr {
t.Errorf("%s: expected error: %v, got %s", name, tc.isErr, err)
}

if result != tc.expected {
t.Errorf("%s: expected %s, got %s", name, tc.expected, result)
}
}
}

func TestDescribeFlagsAsJSON(t *testing.T) {
tests := map[string]struct {
input map[string]interface{}
expected string
isErr bool
isHidden bool
showHidden bool
}{
"bool is not quoted": {
input: map[string]interface{}{"key": false},
expected: `{"key":false}`,
isErr: false,
},
"help is not shown": {
input: map[string]interface{}{"key": false, "help": true},
expected: `{"key":false}`,
isErr: false,
},
"hidden is shown": {
input: map[string]interface{}{"key": false},
expected: `{"key":false}`,
isErr: false,
isHidden: true,
showHidden: true,
},
"hidden is not shown": {
input: map[string]interface{}{"key": false},
expected: `{}`,
isErr: false,
isHidden: true,
showHidden: false,
},
"string is quoted": {
input: map[string]interface{}{"key": "val"},
expected: `{"key":"val"}`,
isErr: false,
},
}

for name, tc := range tests {
fs := pflag.NewFlagSet("flags", pflag.ContinueOnError)

for k, v := range tc.input {
switch val := v.(type) {
case bool:
fs.Bool(k, val, "")
case string:
fs.String(k, val, "")
}

if tc.isHidden {
err := fs.MarkHidden(k)
if err != nil {
t.Errorf("%s: test setup failed: %s", name, err)
}
}
}
result, err := DescribeFlags(fs, tc.showHidden, JSON)

if (err != nil) != tc.isErr {
t.Errorf("%s: expected error: %v, got %s", name, tc.isErr, err)
}

if result != tc.expected {
t.Errorf("%s: expected %s, got %s", name, tc.expected, result)
}
}
}
5 changes: 4 additions & 1 deletion cmd/forwarder/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type command struct {
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
config := bind.DescribeFlags(cmd.Flags())
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.Plain)
if err != nil {
return err
}

if f := c.logConfig.File; f != nil {
defer f.Close()
Expand Down
5 changes: 4 additions & 1 deletion cmd/forwarder/pac/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type command struct {
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
config := bind.DescribeFlags(cmd.Flags())
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.Plain)
if err != nil {
return err
}

if f := c.logConfig.File; f != nil {
defer f.Close()
Expand Down
5 changes: 4 additions & 1 deletion cmd/forwarder/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ type command struct {
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
config := bind.DescribeFlags(cmd.Flags())
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.JSON)
if err != nil {
return err
}

if f := c.logConfig.File; f != nil {
defer f.Close()
Expand Down

0 comments on commit 2c1dd94

Please sign in to comment.