Parcourir la Source

Add a flag to reverse the order of the posts

Lucas Stadler 9 ans auparavant
Parent
commit
62cb860145
1 fichiers modifiés avec 11 ajouts et 0 suppressions
  1. 11 0
      go/blog/blog.go

+ 11 - 0
go/blog/blog.go

@ -25,11 +25,13 @@ type Post struct {
25 25
26 26
var flags struct {
27 27
	writeBack bool
28
	reverse   bool
28 29
}
29 30
var dataPath string = "blog.yaml"
30 31
31 32
func init() {
32 33
	flag.BoolVar(&flags.writeBack, "write-back", false, "Rewrite the YAML file with the generated ids")
34
	flag.BoolVar(&flags.reverse, "reverse", false, "Reverse the order of the articles in the file")
33 35
}
34 36
35 37
func main() {
@ -65,6 +67,15 @@ func main() {
65 67
<body>
66 68
`)
67 69
70
	if flags.reverse {
71
		l := len(posts)
72
		reversePosts := make([]Post, l)
73
		for i := 0; i < l; i++ {
74
			reversePosts[i] = posts[l-i-1]
75
		}
76
		posts = reversePosts
77
	}
78
68 79
	for i, post := range posts {
69 80
		if post.Id == "" {
70 81
			posts[i].Id = generateId(post)