arcast/internal/command/client/scan.go

50 lines
1.2 KiB
Go
Raw Normal View History

2023-12-13 20:07:22 +01:00
package client
import (
"context"
"os"
"time"
"forge.cadoles.com/arcad/arcast/internal/command/client/flag"
"forge.cadoles.com/arcad/arcast/pkg/client"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/cli/format"
)
func Scan() *cli.Command {
return &cli.Command{
Name: "scan",
Flags: flag.ComposeFlags(
&cli.DurationFlag{
Name: "timeout",
EnvVars: []string{"ARCAST_CLIENT_SCAN_TIMEOUT"},
Usage: "Use `TIMEOUT` as maximum scan duration",
Value: 5 * time.Second,
},
),
Action: func(ctx *cli.Context) error {
baseFlags := flag.GetBaseFlags(ctx)
timeout := ctx.Duration("timeout")
client := client.New()
scanCtx, cancel := context.WithTimeout(ctx.Context, timeout)
defer cancel()
players, err := client.Scan(scanCtx)
if err != nil && (!errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded)) {
return errors.Wrap(err, "could not scan for players")
}
hints := playerHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, AsAnySlice(players...)...); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}