This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.go
116 lines (101 loc) · 2.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main // import "github.com/petermbenjamin/awesome-podcasts"
import (
"bufio"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
const (
jsonFile = "awesome-podcasts.json"
yamlFile = "awesome-podcasts.yaml"
)
// Podcast represents the list of awesome podcasts
type Podcast struct {
Category string `json:"category" yaml:"category"`
Pods []Pod `json:"pods" yaml:"pods"`
Subtitle string `json:"subtitle" yaml:"subtitle"`
}
// Pod represents a podcast object
type Pod struct {
Desc string `json:"desc" yaml:"desc"`
Name string `json:"name" yaml:"name"`
URL string `json:"url" yaml:"url"`
}
func main() {
// Read YAML file
b, err := ioutil.ReadFile(yamlFile)
if err != nil {
fmt.Println(fmt.Errorf("YAML file not found: %+s", err))
os.Exit(1)
}
// Load data into Go structs
var podcasts []Podcast
err = yaml.Unmarshal(b, &podcasts)
if err != nil {
fmt.Println(fmt.Errorf("could not unmarshal YAML: %+s", err))
os.Exit(1)
}
// Sort alphabetically by category
sort.Slice(podcasts, func(i, j int) bool {
return podcasts[i].Category < podcasts[j].Category
})
// Sort alphabetically by podcast, ignoring case-sensitivity
for _, c := range podcasts {
sort.Slice(c.Pods, func(i, j int) bool {
return strings.ToUpper(c.Pods[i].Name) <
strings.ToUpper(c.Pods[j].Name)
})
}
// Generate JSON
marshaledBytes, err := json.MarshalIndent(podcasts, "", " ")
if err != nil {
fmt.Println(fmt.Errorf("could not marshal sorted JSON: %+v", err))
os.Exit(1)
}
// Write JSON
err = ioutil.WriteFile(jsonFile, marshaledBytes, 0644)
if err != nil {
fmt.Println(fmt.Errorf("could not write sorted JSON: %+v", err))
os.Exit(1)
}
// helper functions
funcMap := template.FuncMap{
"dashed": func(word string) string {
word = strings.ToLower(word)
word = strings.Replace(word, " ", "-", -1)
word = strings.Replace(word, "/", "", -1)
return word
},
"titled": strings.Title,
}
// Generate README
// Load README template
paths := []string{
filepath.Join("tmpl", "readme.md.tmpl"),
}
t := template.Must(template.
New("main").
Funcs(funcMap).
ParseFiles(paths...))
// Create file
f, err := os.Create("README.md")
if err != nil {
fmt.Println(fmt.Errorf("could not create README file: %s", err))
}
defer f.Close()
// Create buffered writer
w := bufio.NewWriter(f)
defer w.Flush()
// Write data out
err = t.ExecuteTemplate(w, "readme.md.tmpl", podcasts)
if err != nil {
fmt.Println(fmt.Errorf("could not write README file: %s", err))
os.Exit(1)
}
}