Bladeren bron

Support writing directly to a file

Lucas Stadler 9 jaren geleden
bovenliggende
commit
9cf0b55b98
1 gewijzigde bestanden met toevoegingen van 19 en 10 verwijderingen
  1. 19 10
      go/blog/blog.go

+ 19 - 10
go/blog/blog.go

@ -77,6 +77,14 @@ func main() {
77 77
	}
78 78
	defer f.Close()
79 79
80
	out := os.Stdout
81
	if flag.NArg() > 1 {
82
		out, err = os.Create(flag.Arg(1))
83
		if err != nil {
84
			exit(err)
85
		}
86
	}
87
80 88
	data, err := ioutil.ReadAll(f)
81 89
	if err != nil {
82 90
		exit(err)
@ -88,7 +96,7 @@ func main() {
88 96
		exit(err)
89 97
	}
90 98
91
	fmt.Printf(`<!doctype html>
99
	fmt.Fprintf(out, `<!doctype html>
92 100
<html>
93 101
<head>
94 102
	<meta charset="utf-8" />
@ -117,15 +125,15 @@ func main() {
117 125
		var err error
118 126
		switch post.Type {
119 127
		case "shell":
120
			err = shellTmpl.Execute(os.Stdout, post)
128
			err = shellTmpl.Execute(out, post)
121 129
		case "link":
122
			err = linkTmpl.Execute(os.Stdout, post)
130
			err = linkTmpl.Execute(out, post)
123 131
		case "image":
124
			err = imageTmpl.Execute(os.Stdout, post)
132
			err = imageTmpl.Execute(out, post)
125 133
		case "song":
126
			err = songTmpl.Execute(os.Stdout, post)
134
			err = songTmpl.Execute(out, post)
127 135
		case "text":
128
			err = textTmpl.Execute(os.Stdout, post)
136
			err = textTmpl.Execute(out, post)
129 137
		case "video":
130 138
			u, err := url.Parse(post.URL)
131 139
			if err != nil {
@ -136,7 +144,7 @@ func main() {
136 144
				exit(fmt.Errorf("unsupported video url '%s'", post.URL))
137 145
			}
138 146
			post.URL = id
139
			err = videoTmpl.Execute(os.Stdout, post)
147
			err = videoTmpl.Execute(out, post)
140 148
		default:
141 149
			fmt.Fprintf(os.Stderr, "Error: no output for type '%s'\n", post.Type)
142 150
			os.Exit(1)
@ -146,14 +154,15 @@ func main() {
146 154
		}
147 155
	}
148 156
149
	fmt.Printf("\n</body>\n</html>\n")
157
	fmt.Fprintf(out, "\n</body>\n</html>\n")
158
	out.Close()
150 159
151 160
	if flags.writeBack {
152
		out, err := yaml.Marshal(posts)
161
		dataOut, err := yaml.Marshal(posts)
153 162
		if err != nil {
154 163
			exit(err)
155 164
		}
156
		ioutil.WriteFile(dataPath, out, 0664)
165
		ioutil.WriteFile(dataPath, dataOut, 0664)
157 166
	}
158 167
}
159 168