repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

bootstripper refactor

09c499626a65108c8a474af19fa944c3f4cfb55d

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

bootstripper.java

Modified
5 5 import java.nio.file.Files;
6 6 import java.nio.file.LinkOption;
7 7 import java.nio.file.Path;
8 -import java.util.Comparator;
8 +import java.util.List;
9 +import java.util.function.BiFunction;
10 11 class bootstripper {
11 12 public static void main(String[] arguments) throws Throwable {
58 59 log("Creating symlinks as defined in {0} file.",
59 60 emphasize_local(symlinks_file.getFileName().toString()));
60 61 var lines = Files.readAllLines(symlinks_file);
61 - String source = null;
62 - String target = null;
63 - for (String line : lines) {
64 - if (line.isBlank())
65 - continue;
66 - if (source == null)
67 - source = line;
68 - else
69 - target = line;
70 - if (target != null) {
71 - link(root, source, target);
72 - source = null;
73 - target = null;
74 - }
75 - }
62 + for_each_mapping(lines, (source, target) -> link(root, source, target));
78 65 static void delete_symlinks(Path root, Path symlinks_file) throws Throwable {
79 66 log("Deleting symlinks as defined in {0} file.",
80 67 emphasize_local(symlinks_file.getFileName().toString()));
81 - // TODO extract parse pattern into method
82 68 var lines = Files.readAllLines(symlinks_file);
83 - String source = null;
84 - String target = null;
85 - for (String line : lines) {
86 - if (line.isBlank())
87 - continue;
88 - if (source == null)
89 - source = line;
90 - else
91 - target = line;
92 - if (target != null) {
93 - delete(root, source, target);
94 - source = null;
95 - target = null;
96 - }
97 - }
69 + for_each_mapping(lines, (source, target) -> delete(root, source, target));
100 72 static void delete(Path root, String source, String target) throws Throwable {
219 191 static void log(String message, Object... arguments) {
220 192 System.out.println(format(message, arguments));
195 + static interface ThrowUp<A, B> {
196 + default Throwable gurgh(A a, B b) {
197 + try {
198 + apply(a, b);
199 + return null;
200 + } catch (Throwable vomit) {
201 + return vomit;
205 + void apply(A a, B b) throws Throwable;
208 + static void for_each_mapping(List<String> lines, ThrowUp<String, String> block)
209 + throws Throwable {
210 + String source = null;
211 + String target = null;
212 + for (String line : lines) {
213 + if (line.isBlank())
214 + continue;
215 + if (source == null)
216 + source = line;
217 + else
218 + target = line;
219 + if (target != null) {
220 + var vomit = block.gurgh(source, target);
221 + if (vomit != null)
222 + throw vomit;
223 + source = null;
224 + target = null;