67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
// First-party packages
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
|
|
// Third-party packages
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type OfflineTitle struct {
|
|
Title string `yaml:"title"`
|
|
}
|
|
|
|
type OfflineDate struct {
|
|
Date string `yaml:"Date"`
|
|
}
|
|
|
|
type OfflinePost struct {
|
|
Post struct {
|
|
Text string `yaml:"text"`
|
|
Links string `yaml:"links"`
|
|
} `yaml:"post"`
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println("Parsing YAML file")
|
|
|
|
var fileName string
|
|
flag.StringVar(&fileName, "f", "", "YAML file to parse.")
|
|
flag.Parse()
|
|
|
|
if fileName == "" {
|
|
fmt.Println("Please provide yaml file by using -f option")
|
|
return
|
|
}
|
|
|
|
yamlFile, err := ioutil.ReadFile(fileName)
|
|
if err != nil {
|
|
fmt.Printf("Error reading YAML file: %s\n", err)
|
|
return
|
|
}
|
|
|
|
var title OfflineTitle
|
|
err = yaml.Unmarshal(yamlFile, &title)
|
|
if err != nil {
|
|
fmt.Printf("Error parsing YAML file: %s\n", err)
|
|
}
|
|
|
|
var date OfflineDate
|
|
err = yaml.Unmarshal(yamlFile, &date)
|
|
if err != nil {
|
|
fmt.Printf("Error parsing YAML file: %s\n", err)
|
|
}
|
|
|
|
var post OfflinePost
|
|
err = yaml.Unmarshal(yamlFile, &post)
|
|
if err != nil {
|
|
fmt.Printf("Error parsing YAML file: %s\n", err)
|
|
}
|
|
|
|
fmt.Println(title)
|
|
fmt.Println(date)
|
|
fmt.Println(post)
|
|
} |