~nromdotcom/gemif

4b82d8371f2c1e1cac4fe3e3e3a674595407356a — Norm MacLennan 11 months ago 914458c
Swap binaries to use flags for arg parsing
M cmd/gemif/main.go => cmd/gemif/main.go +10 -0
@@ 2,9 2,11 @@
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"git.sr.ht/~nromdotcom/gemif/pkg/gamemanager"
	"git.sr.ht/~nromdotcom/gemif/pkg/gemserver"


@@ 20,6 22,14 @@ var (
)

func main() {
	versionFlag := flag.Bool("version", false, "Print version information and exit")
	flag.Parse()

	if *versionFlag {
		fmt.Printf("%s %s (%s) - built %s\n\n", appName, appVersion, appCommit, buildTime)
		os.Exit(0)
	}

	file, err := ioutil.ReadFile("./config.toml")
	if err != nil {
		log.Fatalf("Couldn't load config file: %s", err)

M cmd/gemifc/main.go => cmd/gemifc/main.go +28 -6
@@ 3,6 3,7 @@ package main
// yaml.v3 doesn't serialize multi-line strings
// correctly for some reason, so we use v2.
import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"


@@ 14,6 15,14 @@ import (
	"gopkg.in/yaml.v2"
)

//nolint:gochecknoglobals
var (
	appName    string = "GemIF"
	appVersion string = "0.0.0"
	appCommit  string = "local"
	buildTime  string = "just now!"
)

func readFile(path string) (string, error) {
	contents, err := ioutil.ReadFile(path)
	if err != nil {


@@ 97,15 106,28 @@ func writeStory(story *gamemanager.Story, file string) error {
	return nil
}

// Temporarily ignore this function's length while we work on
// refactoring some stuff.
//nolint:funlen
func main() {
	correctNumArguments := 3
	if len(os.Args) != correctNumArguments {
		log.Fatal("Usage: gemifc [input_path] [output_path]")
	var inputPath string

	var outputPath string

	versionFlag := flag.Bool("version", false, "Print version information and exit")
	flag.StringVar(&inputPath, "in", "", "The path of the story source directory")
	flag.StringVar(&outputPath, "out", "", "The path to write the compile story")
	flag.Parse()

	if *versionFlag {
		fmt.Printf("%s %s (%s) - built %s\n\n", appName, appVersion, appCommit, buildTime)
		os.Exit(0)
	} else if inputPath == "" || outputPath == "" {
		fmt.Printf("%v, %v\n\n", inputPath, outputPath)
		flag.PrintDefaults()
		os.Exit(1)
	}

	inputPath := os.Args[1]
	outputPath := os.Args[2]

	fmt.Printf("Compiling story %s to %s\n", inputPath, outputPath)

	fileContents, err := readFile(fmt.Sprintf("%s/metadata.yml", inputPath))

M stories/compiled/tutorial.yml => stories/compiled/tutorial.yml +2 -2
@@ 173,13 173,13 @@ rooms:
    Once you've written your story (or probably more like as you write your story), you can use `gemifc` to compile your story to YAML format for use by the `gemif` server.

    ```
    $ gemifc [input directory] [output directory]
    $ gemifc -in [input directory] -out [output directory]
    ```

    So to compile this story:

    ```
    $ gemifc ./stories/src/tutorial ./stories/compiled
    $ gemifc -in ./stories/src/tutorial -out ./stories/compiled
    Compiling story ./stories/src/tutorial to ./stories/compiled/

    Finished loading story:

M stories/src/tutorial/01-basics.gemif => stories/src/tutorial/01-basics.gemif +2 -2
@@ 193,13 193,13 @@ You can check the Golang text/template documentation for ways you can use templa
Once you've written your story (or probably more like as you write your story), you can use `gemifc` to compile your story to YAML format for use by the `gemif` server.

```
$ gemifc [input directory] [output directory]
$ gemifc -in [input directory] -out [output directory]
```

So to compile this story:

```
$ gemifc ./stories/src/tutorial ./stories/compiled
$ gemifc -in ./stories/src/tutorial -out ./stories/compiled
Compiling story ./stories/src/tutorial to ./stories/compiled/

Finished loading story: