This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
interfacesWriter.go
147 lines (128 loc) · 3 KB
/
interfacesWriter.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package netif
import (
"fmt"
"os"
"strings"
"github.com/n-marshall/fn"
cp "github.com/n-marshall/go-cp"
)
func BackupPath(path string) fn.Option {
return fn.MakeOption("backupPath", path)
}
func (is *InterfaceSet) Write(opts ...fn.Option) error {
fnConfig := fn.MakeConfig(
fn.Defaults{"path": "output"},
opts,
)
path := fnConfig.GetString("path")
backupPath := fnConfig.GetString("backupPath")
if backupPath == "" {
backupPath = path + ".backup"
}
// Backup interface file
err := copyFileIfExists(path, backupPath)
if err != nil {
return err
}
// try to open the interface file for writing
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
// Restore interface file
err := copyFileIfExists(backupPath, path)
if err != nil {
return err
}
return err
}
defer f.Close()
// write interface file
err = is.WriteToFile(f)
if err != nil {
// Restore interface file
err := copyFileIfExists(backupPath, path)
if err != nil {
return err
}
}
return err
}
func copyFileIfExists(path, backupPath string) error {
if _, err := os.Stat(path); err == nil {
err2 := cp.CopyFile(path, backupPath)
if err2 != nil {
return err
}
}
return nil
}
func (is *InterfaceSet) WriteToFile(f *os.File) error {
for _, adapter := range is.Adapters {
adapterString, err := adapter.writeString()
if err != nil {
return err
}
fmt.Fprintf(f, "%s\n\n", adapterString)
}
return nil
}
func (a *NetworkAdapter) writeString() (string, error) {
var lines []string
if a.Auto {
lines = append(lines, fmt.Sprintf("auto %s", a.Name))
}
if a.Hotplug {
lines = append(lines, fmt.Sprintf("allow-hotplug %s", a.Name))
}
lines = append(lines, a.writeAddressFamily())
if a.AddrSource == STATIC || a.AddrSource == MANUAL {
for _, line := range a.writeIPLines() {
lines = append(lines, line)
}
}
return strings.Join(lines, "\n"), nil
}
func (a *NetworkAdapter) GetAddrFamilyString() string {
switch a.AddrFamily {
case INET:
return "inet"
case INET6:
return "inet6"
}
return "inet"
}
func (a *NetworkAdapter) GetSourceFamilyString() string {
switch a.AddrSource {
case DHCP:
return "dhcp"
case STATIC:
return "static"
case LOOPBACK:
return "loopback"
case MANUAL:
return "manual"
}
return "dhcp"
}
func (a *NetworkAdapter) writeAddressFamily() string {
var familyStr = a.GetAddrFamilyString()
var sourceStr = a.GetSourceFamilyString()
return fmt.Sprintf("iface %s %s %s", a.Name, familyStr, sourceStr)
}
func (a *NetworkAdapter) writeIPLines() (lines []string) {
if a.Address != nil {
lines = append(lines, fmt.Sprintf(" address %s", a.Address))
}
if a.Netmask != nil {
lines = append(lines, fmt.Sprintf(" netmask %s", a.Netmask))
}
if a.Network != nil {
lines = append(lines, fmt.Sprintf(" network %s", a.Network))
}
if a.Broadcast != nil {
lines = append(lines, fmt.Sprintf(" broadcast %s", a.Broadcast))
}
if a.Gateway != nil {
lines = append(lines, fmt.Sprintf(" gateway %s", a.Gateway))
}
return
}