Skip to content

Commit

Permalink
chore(ifdata): Clean up function params
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 11, 2024
1 parent 71516fb commit d64260d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 19 deletions.
5 changes: 4 additions & 1 deletion cmd/ifdata/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ func run(cmd *cobra.Command, args []string) error {
}

for _, iface := range ifaces {
s, err := format.Sprint(cmd, iface)
s, err := format.Sprint(iface)
if err != nil {
if errors.Is(err, ErrUnknownFormatter) {
cmd.SilenceUsage = false
}
errs = append(errs, err)
continue
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/ifdata/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"slices"
"strconv"
"strings"

"github.com/spf13/cobra"
)

//go:generate go run github.com/dmarkham/enumer -type formatter -linecomment -output formatter_string.go
Expand Down Expand Up @@ -123,7 +121,7 @@ func (f formatter) supported() bool {

var ErrUnknownFormatter = errors.New("unknown formatter")

func (f formatter) Sprint(cmd *cobra.Command, iface *net.Interface) (string, error) {
func (f formatter) Sprint(iface *net.Interface) (string, error) {
switch f {
case fmtMTU:
return strconv.Itoa(iface.MTU), nil
Expand Down Expand Up @@ -163,10 +161,8 @@ func (f formatter) Sprint(cmd *cobra.Command, iface *net.Interface) (string, err
return strings.TrimSpace(buf.String()), nil
default:
if f.supported() {
return f.formatStatistics(cmd, iface)
return f.formatStatistics(iface)
}

cmd.SilenceUsage = false
return "", fmt.Errorf("%w: %s", ErrUnknownFormatter, f)
}
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/ifdata/statistics_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
"strconv"
"time"

"github.com/spf13/cobra"
"golang.org/x/sys/unix"
)

const statisticsSupported = true

func (f formatter) formatStatistics(cmd *cobra.Command, iface *net.Interface) (string, error) {
func (f formatter) formatStatistics(iface *net.Interface) (string, error) {
format := statsFormatter(f)
if format == nil {
cmd.SilenceUsage = false
return "", fmt.Errorf("%w: %s", ErrUnknownFormatter, f)
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/ifdata/statistics_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"time"

"github.com/prometheus/procfs"
"github.com/spf13/cobra"
)

const statisticsSupported = true

func (f formatter) formatStatistics(cmd *cobra.Command, iface *net.Interface) (string, error) {
func (f formatter) formatStatistics(iface *net.Interface) (string, error) {
format := statsFormatter(f)
if format == nil {
cmd.SilenceUsage = false
return "", fmt.Errorf("%w: %s", ErrUnknownFormatter, f)
}

Expand Down
8 changes: 2 additions & 6 deletions cmd/ifdata/statistics_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

package ifdata

import (
"net"

"github.com/spf13/cobra"
)
import "net"

const statisticsSupported = false

func (f formatter) formatStatistics(_ *cobra.Command, _ *net.Interface) (string, error) {
func (f formatter) formatStatistics(_ *net.Interface) (string, error) {
return "", ErrStatisticsUnsupported
}

0 comments on commit d64260d

Please sign in to comment.