blocky/cmd/lists.go

51 lines
911 B
Go
Raw Normal View History

2021-02-08 21:56:58 +01:00
package cmd
import (
"fmt"
2022-08-19 22:04:35 +02:00
"io"
2021-02-08 21:56:58 +01:00
"net/http"
2021-08-25 22:06:34 +02:00
"github.com/0xERR0R/blocky/api"
"github.com/0xERR0R/blocky/log"
2021-02-08 21:56:58 +01:00
"github.com/spf13/cobra"
)
// NewListsCommand creates new command instance
2021-02-08 21:56:58 +01:00
func NewListsCommand() *cobra.Command {
c := &cobra.Command{
Use: "lists",
Short: "lists operations",
}
c.AddCommand(newRefreshCommand())
return c
}
func newRefreshCommand() *cobra.Command {
return &cobra.Command{
Use: "refresh",
Short: "refreshes all lists",
RunE: refreshList,
2021-02-08 21:56:58 +01:00
}
}
func refreshList(_ *cobra.Command, _ []string) error {
2021-02-08 21:56:58 +01:00
resp, err := http.Post(apiURL(api.PathListsRefresh), "application/json", nil)
if err != nil {
return fmt.Errorf("can't execute %w", err)
2021-02-08 21:56:58 +01:00
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
2022-08-19 22:04:35 +02:00
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("response NOK, %s %s", resp.Status, string(body))
2021-02-08 21:56:58 +01:00
}
log.Log().Info("OK")
return nil
2021-02-08 21:56:58 +01:00
}