142 lines
4.2 KiB
Go
142 lines
4.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"resty.dev/v3"
|
|
)
|
|
|
|
var exportCmd = &cobra.Command{
|
|
Use: "export",
|
|
Short: "Export Kontor Database",
|
|
Long: "Export Kontor Database to given file",
|
|
// Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
slog.Info("export kontor")
|
|
slog.Info("Verbosity level", "level", verbose)
|
|
var login LoginResponse
|
|
|
|
client := resty.New()
|
|
PostJSON(client, "http://127.0.0.1:8550/api/login", &login)
|
|
|
|
type ExportData struct {
|
|
Sport []models.SportDTO `json:"sport"`
|
|
Player []models.PlayerDTO `json:"player"`
|
|
Team []models.TeamDTO `json:"team"`
|
|
Position []models.PositionDTO `json:"field_position"`
|
|
Rooster []models.RoosterDTO `json:"rooster"`
|
|
Vendor []models.VendorDTO `json:"vendor"`
|
|
CardSet []models.CardSetDTO `json:"card_set"`
|
|
Card []models.CardDTO `json:"card"`
|
|
Artist []models.ArtistDTO `json:"artist"`
|
|
Publisher []models.PublisherDTO `json:"publisher"`
|
|
Worktype []models.WorkTypeDTO `json:"worktype"`
|
|
Comic []models.ComicDTO `json:"comic"`
|
|
Volume []models.VolumeDTO `json:"volume"`
|
|
StoryArc []models.StoryArcDTO `json:"story_arc"`
|
|
Issue []models.IssueDTO `json:"issue"`
|
|
}
|
|
|
|
var (
|
|
sports []models.SportDTO
|
|
players []models.PlayerDTO
|
|
teams []models.TeamDTO
|
|
positions []models.PositionDTO
|
|
roosters []models.RoosterDTO
|
|
vendors []models.VendorDTO
|
|
cardsets []models.CardSetDTO
|
|
cards []models.CardDTO
|
|
artists []models.ArtistDTO
|
|
publishers []models.PublisherDTO
|
|
worktypes []models.WorkTypeDTO
|
|
comics []models.ComicDTO
|
|
volumes []models.VolumeDTO
|
|
storyarcs []models.StoryArcDTO
|
|
issues []models.IssueDTO
|
|
)
|
|
|
|
registry := map[string]any{
|
|
"http://127.0.0.1:8550/api/v1/tysc/sports": &sports,
|
|
"http://127.0.0.1:8550/api/v1/tysc/players": &players,
|
|
"http://127.0.0.1:8550/api/v1/tysc/teams": &teams,
|
|
"http://127.0.0.1:8550/api/v1/tysc/positions": &positions,
|
|
"http://127.0.0.1:8550/api/v1/tysc/roosters": &roosters,
|
|
"http://127.0.0.1:8550/api/v1/tysc/vendors": &vendors,
|
|
"http://127.0.0.1:8550/api/v1/tysc/cardsets": &cardsets,
|
|
"http://127.0.0.1:8550/api/v1/tysc/cards": &cards,
|
|
"http://127.0.0.1:8550/api/v1/comics/artists": &artists,
|
|
"http://127.0.0.1:8550/api/v1/comics/publishers": &publishers,
|
|
"http://127.0.0.1:8550/api/v1/comics/worktypes": &worktypes,
|
|
"http://127.0.0.1:8550/api/v1/comics/comics": &comics,
|
|
"http://127.0.0.1:8550/api/v1/comics/volumes": &volumes,
|
|
"http://127.0.0.1:8550/api/v1/comics/storyarcs": &storyarcs,
|
|
"http://127.0.0.1:8550/api/v1/comics/issues": &issues,
|
|
}
|
|
for url, value := range registry {
|
|
GetJSON(client, login.Token, url, value)
|
|
}
|
|
export := ExportData{
|
|
Sport: sports,
|
|
Player: players,
|
|
Team: teams,
|
|
Position: positions,
|
|
Rooster: roosters,
|
|
Vendor: vendors,
|
|
CardSet: cardsets,
|
|
Card: cards,
|
|
Artist: artists,
|
|
Publisher: publishers,
|
|
Worktype: worktypes,
|
|
Comic: comics,
|
|
Volume: volumes,
|
|
StoryArc: storyarcs,
|
|
Issue: issues,
|
|
}
|
|
defer client.Close()
|
|
jsonData, err := json.MarshalIndent(export, "", " ")
|
|
if err != nil {
|
|
slog.Info("JSON MarshalIndent failed", "Error:", err)
|
|
}
|
|
// Write bytes to file
|
|
err = os.WriteFile("data.json", jsonData, 0644)
|
|
if err != nil {
|
|
slog.Info("Error writing file:", "Error", err)
|
|
}
|
|
slog.Info("export kontor finished")
|
|
},
|
|
}
|
|
|
|
func GetJSON(client *resty.Client, token string, url string, v any) {
|
|
_, err := client.R().
|
|
SetAuthToken(token).
|
|
SetHeader("Accept", "application/json").
|
|
SetResult(v).
|
|
Get(url)
|
|
if err != nil {
|
|
slog.Info(url, "error", err.Error())
|
|
}
|
|
}
|
|
|
|
func PostJSON(client *resty.Client, url string, v any) {
|
|
_, err := client.R().
|
|
SetBody(Login{
|
|
Email: "thomas.peetz@thpeetz.de",
|
|
Password: "admin",
|
|
}).
|
|
SetHeader("Accept", "application/json").
|
|
SetResult(v).
|
|
Post(url)
|
|
if err != nil {
|
|
slog.Info("Health", "error", err.Error())
|
|
}
|
|
slog.Info(url, "token", v)
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(exportCmd)
|
|
}
|