-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtmpfile.go
39 lines (33 loc) · 803 Bytes
/
tmpfile.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
type tempFile struct {
f *os.File
name string
}
func mustNewTempFile() *tempFile {
tmpfile, err := ioutil.TempFile("", "chartData")
if err != nil {
log.Fatalf("Could not create temporary file to store the chart: %v", err)
}
return &tempFile{tmpfile, tmpfile.Name()}
}
func (f *tempFile) mustClose() {
if err := f.f.Close(); err != nil {
log.Fatalf("Could not close temporary file after saving chart to it: %v", err)
}
}
func (f *tempFile) mustRenameWithHTMLSuffix() {
newName := f.name + ".html"
if err := os.Rename(f.name, newName); err != nil {
log.Fatalf("Could not add html extension to the temporary file: %v", err)
}
f.name = newName
}
func (f *tempFile) url() string {
return fmt.Sprintf("file://%v", f.name)
}