weiro/cmds/sites.go

47 lines
865 B
Go
Raw Normal View History

package cmds
import (
"context"
"fmt"
"log"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"lmika.dev/lmika/weiro/config"
"lmika.dev/lmika/weiro/services"
)
func Sites() *cobra.Command {
cmd := &cobra.Command{
Use: "sites",
Short: "Manage sites",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
svcs, err := services.New(cfg)
if err != nil {
log.Fatal(err)
}
defer svcs.Close()
ctx := context.Background()
sites, err := svcs.Sites.ListAllSitesWithOwners(ctx)
if err != nil {
log.Fatal(err)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "GUID\tOWNER\tNAME")
for _, site := range sites {
fmt.Fprintf(w, "%s\t%s\t%s\n", site.GUID, site.Username, site.Title)
}
w.Flush()
},
}
return cmd
}