-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkgs.go
205 lines (169 loc) · 3.91 KB
/
pkgs.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"fmt"
"os"
"strings"
)
type Pkgs struct {
pkgs []string
spacing_char rune
num_spacing int
with_pkgs bool
}
func (self Pkgs) toString() string {
lst := make([]string, 0)
for _, pkg := range self.pkgs {
prefix := strings.Repeat(string(self.spacing_char), int(self.num_spacing))
if !self.with_pkgs {
pkg = "pkgs." + pkg
}
payload := prefix + pkg
lst = append(lst, payload)
}
return strings.Join(lst, "\n")
}
func (self Pkgs) ToFile(filename, target_name string) {
data, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error reading from nix config file: ", err)
os.Exit(1)
}
text := string(data)
lines := strings.Split(text, "\n")
start_idx := -1
for i, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, target_name) && strings.Contains(line, "=") && strings.HasSuffix(line, "[") {
start_idx = i + 1
for strings.Contains(line, " ") {
line = strings.ReplaceAll(line, " ", " ")
}
for strings.Contains(line, " ;") {
line = strings.ReplaceAll(line, " ;", ";")
}
break
}
}
if start_idx == -1 {
fmt.Println("Error parsing target file, target not found")
os.Exit(1)
}
new_lines := make([]string, len(lines[:start_idx]))
copy(new_lines, lines[:start_idx])
for _, pkg := range strings.Split(self.toString(), "\n") {
new_lines = append(new_lines, pkg)
}
end_reached := false
for _, line := range lines[start_idx:] {
if !end_reached {
og_line := line
line = strings.TrimSpace(line)
for strings.Contains(line, " ;") {
line = strings.ReplaceAll(line, " ;", ";")
}
if strings.TrimSpace(line) == "];" {
end_reached = true
new_lines = append(new_lines, og_line)
}
} else {
new_lines = append(new_lines, line)
}
}
final_text := strings.Join(new_lines, "\n")
err = os.WriteFile(filename, []byte(final_text), 'w')
if err != nil {
fmt.Println("Error writing to file.")
os.Exit(1)
}
}
func (Pkgs) FromFile(filename, target_name string) Pkgs {
data, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error reading from nix config file: ", err)
os.Exit(1)
}
text := string(data)
lines := strings.Split(text, "\n")
start_idx := -1
with_pkgs := false
for i, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, target_name) && strings.Contains(line, "=") && strings.HasSuffix(line, "[") {
start_idx = i + 1
for strings.Contains(line, " ") {
line = strings.ReplaceAll(line, " ", " ")
}
for strings.Contains(line, " ;") {
line = strings.ReplaceAll(line, " ;", ";")
}
if strings.Contains(line, "with pkgs;") {
with_pkgs = true
}
break
}
}
if start_idx == -1 {
fmt.Println("Error parsing target file, target not found")
os.Exit(1)
}
lst := make([]string, 0)
for i := start_idx; i < len(lines); i++ {
line := lines[i]
for strings.Contains(line, " ;") {
line = strings.ReplaceAll(line, " ;", ";")
}
if strings.TrimSpace(line) == "];" {
break
}
lst = append(lst, lines[i])
}
pkgs := Pkgs{}.fromList(lst)
pkgs.with_pkgs = with_pkgs
return pkgs
}
func (Pkgs) fromList(lst []string) Pkgs {
if len(lst) == 0 {
return Pkgs{
pkgs: lst,
spacing_char: 0,
num_spacing: '\t',
with_pkgs: false,
}
}
num_spacing := 0
var spacing_char rune = 0
for _, ch := range lst[0] {
if num_spacing == 0 && spacing_char == 0 {
if ch == ' ' {
spacing_char = ' '
} else if ch == '\t' {
spacing_char = '\t'
} else {
break
}
num_spacing += 1
continue
}
if ch == spacing_char {
num_spacing += 1
} else {
break
}
}
for i, pkg := range lst {
lst[i] = strings.TrimSpace(pkg)
}
with_pkgs := true
if lst[0][:4] == "pkgs." {
with_pkgs = false
for i, pkg := range lst {
lst[i] = pkg[:4]
}
}
return Pkgs{
pkgs: lst,
spacing_char: spacing_char,
num_spacing: num_spacing,
with_pkgs: with_pkgs,
}
}