arcast/internal/command/client/cast.go

60 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-12-13 20:07:22 +01:00
package client
import (
"os"
"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 Cast() *cli.Command {
return &cli.Command{
Name: "cast",
ArgsUsage: "<url>",
Flags: flag.ComposeFlags(
playerFlags...,
),
Action: func(ctx *cli.Context) error {
baseFlags := flag.GetBaseFlags(ctx)
url := ctx.Args().First()
if url == "" {
return errors.New("you must specify an url to cast")
}
statuses := make([]wrappedStatus, 0)
err := forEachPlayer(ctx, func(cl *client.Client, playerAddr string) error {
status, err := cl.Cast(ctx.Context, playerAddr, url)
if err != nil {
return errors.Wrap(err, "could not cast")
}
statuses = append(statuses, wrappedStatus{
Status: status.Status,
URL: status.URL,
Title: status.Title,
ID: status.ID,
Address: playerAddr,
})
return nil
})
if err != nil {
return errors.WithStack(err)
}
hints := wrappedStatusHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, AsAnySlice(statuses...)...); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}