~nromdotcom/gemif

62be8d3af6f6a222333144aecad9c62d888b9c14 — Norm MacLennan 11 months ago 0661a68 0.1.0
gemifc refactoring and linting
1 files changed, 57 insertions(+), 36 deletions(-)

M cmd/gemifc/main.go
M cmd/gemifc/main.go => cmd/gemifc/main.go +57 -36
@@ 28,7 28,7 @@ func findFilesWithExtension(root, pattern string) ([]string, error) {

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

		if info.IsDir() {


@@ 36,21 36,69 @@ func findFilesWithExtension(root, pattern string) ([]string, error) {
		}

		if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
			return err
			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, err
		return nil, fmt.Errorf("problem walking directory: %w", err)
	}

	return matches, nil
}

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

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

	isMetadata := false

	currentRoom := gamemanager.Room{}

	for _, filePiece := range strings.Split(thisFile, "---") {
		if filePiece == "" {
			isMetadata = !isMetadata

			continue
		}

		if isMetadata {
			if err := yaml.Unmarshal([]byte(filePiece), &currentRoom); err != nil {
				return fileRooms, fmt.Errorf("couldn't parse story metadata from yaml: %w", err)
			}
		} else {
			currentRoom.Description = filePiece
			fileRooms = append(fileRooms, currentRoom)
			currentRoom = gamemanager.Room{}
		}

		isMetadata = !isMetadata
	}

	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 main() {
	correctNumArguments := 3
	if len(os.Args) != correctNumArguments {


@@ 80,33 128,12 @@ func main() {
	storyRooms := []gamemanager.Room{}

	for _, file := range files {
		thisFile, err := readFile(file)
		fileRooms, err := processFile(file)
		if err != nil {
			log.Fatalf("Couldn't read .gemif file: %s", err)
			log.Fatalf("Couldn't process file %s: %s", file, err)
		}

		isMetadata := false
		currentRoom := gamemanager.Room{}

		for _, filePiece := range strings.Split(thisFile, "---") {
			if filePiece == "" {
				isMetadata = !isMetadata

				continue
			}

			if isMetadata {
				if err := yaml.Unmarshal([]byte(filePiece), &currentRoom); err != nil {
					log.Fatalf("Couldn't parse story metadata from yaml: %s", err)
				}
			} else {
				currentRoom.Description = filePiece
				storyRooms = append(storyRooms, currentRoom)
				currentRoom = gamemanager.Room{}
			}

			isMetadata = !isMetadata
		}
		storyRooms = append(storyRooms, fileRooms...)
	}

	story := gamemanager.Story{


@@ 125,14 152,8 @@ Finished loading story:

	fmt.Println("Serializing and writing to disk...")

	storyOut, err := yaml.Marshal(story)
	if err != nil {
		log.Fatal("Couldn't load story metadata")
	}

	err = ioutil.WriteFile(fmt.Sprintf("%s/%s.yml", outputPath, story.Metadata.ID), storyOut, 0644) // #nosec G306
	if err != nil {
		log.Fatalf("Couldn't write compiled output: %s", err)
	if err := writeStory(&story, fmt.Sprintf("%s/%s.yml", outputPath, story.Metadata.ID)); err != nil {
		log.Fatalf("Couldn't save story: %s", err)
	}

	fmt.Println("Done!")