~nromdotcom/gemif

ref: 85dc63b9dbb0221579a7fd26c4a1feae4bd39338 gemif/cmd/gemifc/filemanager.go -rw-r--r-- 2.5 KiB
85dc63b9Norm MacLennan chore: upgrade linters and fix linting issues 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"regexp"

	"git.sr.ht/~nromdotcom/gemif/pkg/gamemanager"
	"gopkg.in/yaml.v2"
)

func readFile(path string) (string, error) {
	contents, err := ioutil.ReadFile(path)
	if err != nil {
		return "", fmt.Errorf("couldn't read file: %w", err)
	}

	return string(contents), nil
}

func findFilesWithExtension(root, pattern string) ([]string, error) {
	var matches []string

	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return fmt.Errorf("couldn't load directory %s: %w", root, err)
		}

		if info.IsDir() {
			return nil
		}

		if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
			return fmt.Errorf("couldn't check name of file %s: %w", path, err)
		} else if matched {
			matches = append(matches, path)
		}

		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("problem walking directory: %w", err)
	}

	return matches, nil
}

func processFile(path string) ([]gamemanager.Room, error) {
	roomParser := newRoomParser()
	fileRooms := []gamemanager.Room{}

	thisFile, err := readFile(path)
	if err != nil {
		return fileRooms, fmt.Errorf("couldn't read .gemif file: %w", err)
	}

	sceneSeparator := regexp.MustCompile("(?m)^---")
	for _, room := range sceneSeparator.Split(thisFile, -1) {
		if room == "" {
			continue
		}

		currentRoom, err := roomParser.findRoomInfo(room)
		if err != nil {
			log.Fatal(err)
		}

		currentRoom.Exits, err = roomParser.processExits(room)
		if err != nil {
			log.Fatal(err)
		}

		currentRoom.Description = roomParser.getRoomDescription(room)
		fileRooms = append(fileRooms, currentRoom)

		//nolint:exhaustivestruct
		currentRoom = gamemanager.Room{}
	}

	return fileRooms, nil
}

func writeStory(story *gamemanager.Story, file string) error {
	storyOut, err := yaml.Marshal(story)
	if err != nil {
		return fmt.Errorf("couldn't serialize story: %w", err)
	}

	err = ioutil.WriteFile(file, storyOut, 0644) // #nosec G306
	if err != nil {
		return fmt.Errorf("couldn't write compiled output: %w", err)
	}

	return nil
}

func loadMetadata(path string) (*gamemanager.StoryMetadata, error) {
	var metadata *gamemanager.StoryMetadata

	fileContents, err := readFile(fmt.Sprintf("%s/metadata.yml", path))
	if err != nil {
		return metadata, fmt.Errorf("couldn't read metadata file: %w", err)
	}

	if err := yaml.Unmarshal([]byte(fileContents), &metadata); err != nil {
		return metadata, fmt.Errorf("malformed metadata.yml: %w", err)
	}

	return metadata, nil
}