60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"kontor-api-gin/internal/api"
|
|
"kontor-api-gin/internal/config"
|
|
"kontor-api-gin/internal/helpers"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"resty.dev/v3"
|
|
)
|
|
|
|
var (
|
|
servername string
|
|
)
|
|
|
|
var addLinksCmd = &cobra.Command{
|
|
Use: "add-links [file]",
|
|
Short: "Add Links",
|
|
Long: "Add Links",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fileName := args[0]
|
|
slog.Info(fmt.Sprintf("add Links from %s", fileName))
|
|
lines, err := helpers.LinesInFile(fileName)
|
|
if err != nil {
|
|
slog.Warn("add links failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
client := resty.New()
|
|
localServer := config.Config.Server[servername]
|
|
token := api.GetToken(client, localServer)
|
|
for _, line := range lines {
|
|
lofiAdd := models.MediaLofiAdd{Url: line}
|
|
addResult := &models.MediaLoFi{}
|
|
response, err := client.R().
|
|
SetBody(lofiAdd).
|
|
SetAuthToken(token).
|
|
SetHeader("Accept", "application/json").
|
|
SetResult(addResult).
|
|
Post("http://localhost:8550/api/v1/media/lofis")
|
|
if err != nil {
|
|
slog.Info("Add Lofi", "error", err.Error())
|
|
}
|
|
if addResult != nil {
|
|
slog.Info("Lofi added", "lofi", addResult)
|
|
}
|
|
slog.Info("Add Lofi", "status", response.StatusCode(), "body", response.Body)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(addLinksCmd)
|
|
}
|