forked from vanadium-archive/go.devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injector.go
72 lines (64 loc) · 1.44 KB
/
injector.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"go/format"
"go/token"
"io"
"io/ioutil"
"os"
"text/template"
)
// injectors allow you to rewrite a file, adding content at points specified in the source file's
// address space.
type injector struct {
read int
r io.Reader
w bytes.Buffer
fname string
}
func newInjector(fname string) (*injector, error) {
var err error
i := &injector{fname: fname}
i.r, err = os.Open(fname)
return i, err
}
func (i *injector) copyTo(p token.Position) error {
toread := p.Offset + 1 - i.read
i.read += toread
_, err := io.CopyN(&i.w, i.r, int64(toread))
return err
}
func (i *injector) inject(p token.Position, content string) error {
if err := i.copyTo(p); err != nil {
return err
}
_, err := i.w.Write([]byte(content))
return err
}
func (i *injector) format() error {
if _, err := io.Copy(&i.w, i.r); err != nil {
return err
}
out, err := format.Source(i.w.Bytes())
if err != nil {
return err
}
f, err := os.Create(i.fname)
if err != nil {
return err
}
stat, err := f.Stat()
if err != nil {
return err
}
return ioutil.WriteFile(i.fname, out, stat.Mode())
}
func (i *injector) execute(p token.Position, t *template.Template, data interface{}) error {
if err := i.copyTo(p); err != nil {
return err
}
return t.Execute(&i.w, data)
}