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

Move describe flags to utils and reformat #390

Merged
merged 4 commits into from
Sep 6, 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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ issues:
- path: _test\.go
linters:
- bodyclose
- errcheck
- funlen
- gochecknoglobals
- gocognit
Expand Down
156 changes: 0 additions & 156 deletions bind/describe_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/forwarder/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/saucelabs/forwarder/log"
"github.com/saucelabs/forwarder/log/stdlog"
"github.com/saucelabs/forwarder/runctx"
"github.com/saucelabs/forwarder/utils/cobrautil"
"github.com/saucelabs/forwarder/utils/httpbin"
"github.com/spf13/cobra"
)
Expand All @@ -24,7 +25,7 @@ type command struct {
}

func (c *command) runE(cmd *cobra.Command, _ []string) error {
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.Plain)
config, err := cobrautil.DescribeFlags(cmd.Flags(), false, cobrautil.Plain)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/forwarder/pac/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/saucelabs/forwarder/log/stdlog"
"github.com/saucelabs/forwarder/pac"
"github.com/saucelabs/forwarder/runctx"
"github.com/saucelabs/forwarder/utils/cobrautil"
"github.com/saucelabs/forwarder/utils/osdns"
"github.com/spf13/cobra"
)
Expand All @@ -31,7 +32,7 @@ type command struct {
}

func (c *command) runE(cmd *cobra.Command, _ []string) error {
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.Plain)
config, err := cobrautil.DescribeFlags(cmd.Flags(), false, cobrautil.Plain)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/forwarder/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/saucelabs/forwarder/log/stdlog"
"github.com/saucelabs/forwarder/pac"
"github.com/saucelabs/forwarder/runctx"
"github.com/saucelabs/forwarder/utils/cobrautil"
"github.com/saucelabs/forwarder/utils/osdns"
"github.com/spf13/cobra"
"go.uber.org/goleak"
Expand All @@ -43,7 +44,7 @@ type command struct {
}

func (c *command) runE(cmd *cobra.Command, _ []string) error {
config, err := bind.DescribeFlags(cmd.Flags(), false, bind.Plain)
config, err := cobrautil.DescribeFlags(cmd.Flags(), false, cobrautil.Plain)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion dialvia/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestHTTPProxyDialerDialContext(t *testing.T) {

done := make(chan struct{})
go func() {
serveOne(l, func(conn net.Conn) error { //nolint:errcheck // this function always returns nil error
serveOne(l, func(conn net.Conn) error {
cancel()
<-done
return nil
Expand Down
11 changes: 4 additions & 7 deletions bind/describe.go → utils/cobrautil/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
package cobrautil

import (
"encoding/json"
Expand All @@ -13,6 +13,7 @@ import (
"strings"

"github.com/spf13/pflag"
"golang.org/x/exp/maps"
)

type DescribeFormat int
Expand All @@ -24,13 +25,11 @@ const (

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

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

if flag.Hidden && !showHidden {
return
}
Expand All @@ -40,14 +39,12 @@ func DescribeFlags(fs *pflag.FlagSet, showHidden bool, format DescribeFormat) (s
} else {
args[flag.Name] = strings.Trim(flag.Value.String(), "[]")
}

keys = append(keys, flag.Name)
})

sort.Strings(keys)

switch format {
case Plain:
keys := maps.Keys(args)
sort.Strings(keys)
var b strings.Builder
for _, name := range keys {
b.WriteString(fmt.Sprintf("%s=%s\n", name, args[name]))
Expand Down
Loading