forked from mercari/hcledit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
48 lines (40 loc) · 1.07 KB
/
write.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
package hcledit
import (
"fmt"
"io"
"os"
"path/filepath"
)
// WriteFile writes the new contents to the given file, creating it if it does
// not exist.
func (h *HCLEditor) WriteFile(path string) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return h.Write(f)
}
// Write writes the new contents to the given io.Writer.
func (h *HCLEditor) Write(w io.Writer) error {
_, err := fmt.Fprintf(w, "%s", h.Bytes())
return err
}
// OverWriteFile writes the new contents to the file that has first been read
// via ReadFile.
func (h *HCLEditor) OverWriteFile() error {
if h.path == "" {
return fmt.Errorf("OverWriteFile can be used only when you create editor via ReadFile()")
}
return h.WriteFile(h.path)
}
// Bytes returns a buffer containing the source code resulting from the
// tokens underlying the receiving file. If any updates have been made via
// the AST API, these will be reflected in the result.
func (h *HCLEditor) Bytes() []byte {
return h.writeFile.Bytes()
}