-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreadTemplate.go
50 lines (42 loc) · 1 KB
/
readTemplate.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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
)
type daisyTemplate struct {
GwLeftNet string
GwLeftNetmask string
LeftAddress string
GwRightNet string
GwRightNetmask string
RightAddress string
LeftPort int
NextHopAddress string
NextHopPort int
}
func parse(config daisyTemplate, fileName string) error {
f, err := ioutil.ReadFile("template.nacl")
if err != nil {
return fmt.Errorf("error reading template: %v", err)
}
// Parse requires a string
t, err := template.New("test").Parse(string(f))
if err != nil {
return fmt.Errorf("error parsing template: %v", err)
}
if err = os.MkdirAll("nacls", 0755); err != nil {
return fmt.Errorf("error creating nacls folder: %v", err)
}
fullPath := fmt.Sprintf("nacls/%s.nacl", fileName)
file, err := os.Create(fullPath)
if err != nil {
return fmt.Errorf("error creating file: %v", err)
}
err = t.Execute(file, config)
if err != nil {
return fmt.Errorf("error writing to file: %v", err)
}
return nil
}