Skip to content

Commit

Permalink
Allow --invert with --count and --error to invert selection (#75)
Browse files Browse the repository at this point in the history
* Allow --invert with --count and --error to invert selection
  • Loading branch information
gammazero authored Oct 11, 2023
1 parent 43a6dd6 commit 7ad954c
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var providerFlags = []cli.Flag{
},
&cli.BoolFlag{
Name: "invert",
Usage: "Invert selection to show all providers except those specified",
Usage: "Invert selection to show all providers except those specified. If used with --error shows those without LastError",
},
&cli.StringFlag{
Name: "update-interval",
Expand Down Expand Up @@ -102,19 +102,9 @@ var providerFlags = []cli.Flag{

func providerAction(cctx *cli.Context) error {
if cctx.Bool("count") {
if cctx.Bool("invert") {
return errors.New("cannot use --count with --invert")
}
return countProviders(cctx)
}

if cctx.Bool("all") {
if cctx.Bool("invert") {
return errors.New("cannot use --all with --invert")
}
return listProviders(cctx, nil)
}

pids := cctx.StringSlice("pid")
if len(pids) == 0 {
if isatty.IsTerminal(os.Stdin.Fd()) {
Expand Down Expand Up @@ -213,9 +203,17 @@ func countProviders(cctx *cli.Context) error {

if cctx.Bool("error") {
var count int
for _, pinfo := range provs {
if pinfo.LastError != "" {
count++
if cctx.Bool("invert") {
for _, pinfo := range provs {
if pinfo.LastError == "" {
count++
}
}
} else {
for _, pinfo := range provs {
if pinfo.LastError != "" {
count++
}
}
}
fmt.Println(count)
Expand All @@ -242,14 +240,18 @@ func listProviders(cctx *cli.Context, exclude map[peer.ID]struct{}) error {
return nil
}

onlyWithError := cctx.Bool("error")
var errFilter, onlyWithError bool
if cctx.Bool("error") {
errFilter = true
onlyWithError = !cctx.Bool("invert")
}

if cctx.Bool("id-only") {
for _, pinfo := range provs {
if _, ok := exclude[pinfo.AddrInfo.ID]; ok {
continue
}
if onlyWithError && pinfo.LastError == "" {
if errFilter && (onlyWithError == (pinfo.LastError == "")) {
continue
}
fmt.Println(pinfo.AddrInfo.ID)
Expand All @@ -261,7 +263,7 @@ func listProviders(cctx *cli.Context, exclude map[peer.ID]struct{}) error {
if _, ok := exclude[pinfo.AddrInfo.ID]; ok {
continue
}
if onlyWithError && pinfo.LastError == "" {
if errFilter && (onlyWithError == (pinfo.LastError == "")) {
continue
}
showProviderInfo(cctx, pinfo)
Expand Down

0 comments on commit 7ad954c

Please sign in to comment.