package main import ( "encoding/json" "errors" "flag" "fmt" "os" "path/filepath" ) // runExport writes the games_published webhook payload: every game.yaml as JSON, ready for the panel. func runExport(args []string) error { fs := flag.NewFlagSet("export", flag.ExitOnError) gamesDir := fs.String("games", "games", "directory holding one folder per game") repo := fs.String("repo", "", "repository full name, e.g. gbreed-public/gbr-games") commit := fs.String("commit", "", "commit SHA the templates come from") out := fs.String("out", "", "file to write (default stdout)") fs.Parse(args) entries, err := os.ReadDir(*gamesDir) if err != nil { return err } games := []any{} for _, e := range entries { if !e.IsDir() { continue } raw, err := os.ReadFile(filepath.Join(*gamesDir, e.Name(), "game.yaml")) if err != nil { return fmt.Errorf("%s: %w", e.Name(), err) } game, err := loadYAML(raw) if err != nil { return fmt.Errorf("%s/game.yaml: %w", e.Name(), err) } games = append(games, game) } if len(games) == 0 { return errors.New("no games to export") } payload := map[string]any{ "repository": map[string]any{"full_name": *repo}, "after": *commit, "games": games, } b, err := json.Marshal(payload) if err != nil { return err } if *out == "" { _, err = os.Stdout.Write(b) return err } return os.WriteFile(*out, b, 0o644) }