Skip to content

Instantly share code, notes, and snippets.

@scottjbarr
Created September 5, 2016 22:45
Show Gist options
  • Save scottjbarr/f991f9888eb12bb8ec5a8fe2db76f3d3 to your computer and use it in GitHub Desktop.
Save scottjbarr/f991f9888eb12bb8ec5a8fe2db76f3d3 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
// Place worlds.yml in the same directory as this file.
//
// A Go version of https://gist.github.com/anonymous/4ea6a6c8762188d2031d9a229fe77389
func main() {
name := flag.String("name", "", "Name of world to export from worlds.yml")
flag.Parse()
if *name == "" {
flag.Usage()
os.Exit(1)
}
body, err := ioutil.ReadFile("worlds.yml")
if err != nil {
panic(err)
}
// unmarshal the file body into the map
var data map[string]map[string]interface{}
if err = yaml.Unmarshal(body, &data); err != nil {
panic(err)
}
// get the world we want
y, err := yaml.Marshal(data["worlds"][*name])
if err != nil {
panic(err)
}
// check for missing world
if len(y) < 6 {
fmt.Printf("404 world not found : %v\n", *name)
os.Exit(1)
}
// write the world out to a yaml file
filename := fmt.Sprintf("%v.yml", *name)
ioutil.WriteFile(filename, y, 0600)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment