Created
February 5, 2017 22:16
-
-
Save Komosa/413514ede87f415740591a5e4aa0a845 to your computer and use it in GitHub Desktop.
unyaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"io/ioutil" | |
"os" | |
yaml "gopkg.in/yaml.v2" | |
) | |
type My []MyItem | |
type MyItem struct { | |
Name string | |
Content string | |
} | |
func saveContent(item MyItem, destBase string) error { | |
return ioutil.WriteFile(destBase+item.Name, []byte(item.Content), 0664) | |
} | |
func run() error { | |
destBaseFlag := flag.String("dest-base", ".", "location to where create new files") | |
flag.Parse() | |
destBase := *destBaseFlag + "/" | |
input, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
return err | |
} | |
err = os.MkdirAll(destBase, 0775) | |
if err != nil { | |
return err | |
} | |
var my My | |
err = yaml.Unmarshal(input, &my) | |
if err != nil { | |
return err | |
} | |
for _, x := range my { | |
err = saveContent(x, destBase) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func main() { | |
err := run() | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment