2024-11-16 00:48:48 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-11-21 12:30:26 -05:00
|
|
|
"go.uber.org/zap"
|
2024-11-20 06:09:04 -05:00
|
|
|
"pcgamedb/db"
|
|
|
|
"pcgamedb/log"
|
2024-11-16 00:48:48 -05:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type exportCommandConfig struct {
|
|
|
|
output string
|
|
|
|
}
|
|
|
|
|
|
|
|
var exportCmdCfg exportCommandConfig
|
|
|
|
|
|
|
|
var exportCmd = &cobra.Command{
|
|
|
|
Use: "export",
|
|
|
|
Long: "Export data to json files",
|
|
|
|
Short: "Export data to json files",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
infoJson, gameJson, err := db.Export()
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Error exporting data", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
infoFilePath := filepath.Join(exportCmdCfg.output, "game_infos.json")
|
|
|
|
gameFilePath := filepath.Join(exportCmdCfg.output, "games.json")
|
|
|
|
|
|
|
|
err = os.WriteFile(infoFilePath, infoJson, 0644)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Error exporting data", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = os.WriteFile(gameFilePath, gameJson, 0644)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Error exporting data", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Logger.Info("Data exported successfully")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
exportCmd.Flags().StringVarP(&exportCmdCfg.output, "output", "o", "", "Output directory for json files")
|
|
|
|
RootCmd.AddCommand(exportCmd)
|
|
|
|
}
|