repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

implements unlinking symlinks of dotfiles

d4b7872365658000749cf6c2489bf6fc440476e4

Oliver Jan Krylow <olver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

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.regex.Pattern;
8 +import java.util.Comparator;
10 10 class bootstripper {
11 - public static void main(String[] arguments) throws Throwable {
12 - var dorkfile_path_input = ".";
13 - if (arguments.length == 1) {
14 - dorkfile_path_input = arguments[0];
15 - } else if (arguments.length > 1) {
16 - usage();
17 - throw fail();
18 - }
19 - var dorkfiles_root = Path.of(dorkfile_path_input).toRealPath();
20 - log("Assuming dorkfiles root repo in {0}.",
21 - emphasize_local(dorkfiles_root.toString()));
22 - decrypt_secrets(dorkfiles_root.resolve("tresor"));
23 - var hostname = hostname();
24 - log("Hostname: {0}", emphasize_global(hostname));
25 - var symlinks_file = dorkfiles_root.resolve(hostname + ".symlinks");
26 - if (Files.exists(symlinks_file)) {
27 - create_symlinks(dorkfiles_root, symlinks_file);
28 - } else {
29 - throw fail("Expected a file containing symlinks at: {0}. Found nothing!",
30 - symlinks_file);
31 - }
32 - }
11 + public static void main(String[] arguments) throws Throwable {
12 + var dorkfile_path_input = ".";
13 + var do_clean = false;
34 - static void decrypt_secrets(Path secrets_root) throws Throwable {
35 - log("{0}:Decrypting files in {1} folder.", emphasize_global("TODO"),
36 - emphasize_local(secrets_root.getFileName().toString()));
37 - }
39 - static void create_symlinks(Path root, Path symlinks_file) throws Throwable {
40 - log("Creating symlinks as defined in {0} file.",
41 - emphasize_local(symlinks_file.getFileName().toString()));
42 - var lines = Files.readAllLines(symlinks_file);
43 - String source = null;
44 - String target = null;
45 - for (String line : lines) {
46 - if (line.isBlank())
47 - continue;
48 - if (source == null)
49 - source = line;
50 - else
51 - target = line;
52 - if (target != null) {
53 - link(root, source, target);
54 - source = null;
55 - target = null;
56 - }
57 - }
58 - }
60 - static void link(Path root, String source, String target) throws Throwable {
61 - var home = System.getProperty("user.home");
62 - var source_path = root.resolve(source).toAbsolutePath();
63 - var target_path = Path.of(target.replace("~", home)).toAbsolutePath();
64 - var is_directory = Files.isDirectory(source_path);
65 - if (Files.exists(target_path, LinkOption.NOFOLLOW_LINKS)) {
66 - if (Files.exists(target_path)) {
67 - log("\nOK {0} already exists!\nNot linking {1}.",
68 - emphasize_global(target_path.toString()),
69 - emphasize_local(source_path.toString()));
70 - return;
71 - } else {
72 - log("\nNOPE {0} exists, but it points into nirvana. Removing broken link!",
73 - target_path.toString());
74 - Files.delete(target_path);
75 - }
76 - }
77 - log("\nLINK {0} TO {1}.", emphasize_local(source_path.toString()),
78 - emphasize_local(target_path.toString()));
79 - Files.createDirectories(target_path.getParent());
80 - try {
81 - Files.createSymbolicLink(target_path, source_path);
82 - } catch (Exception __) {
83 - /*
84 - * Creating symbolic links on Windows only recently became possible
85 - * without admin rights. And even that, is only available in Developer
86 - * Mode. However, most tools (including JDK) have not yet adapted to this
87 - * change and still require admin rights. Until that is fixed, we escape
88 - * to a system tool that is known to behave correctly in this regard.
89 - */
90 - var operating_system = System.getProperty("os.name");
91 - if (operating_system.toLowerCase().contains("win")) {
92 - var process = new ProcessBuilder();
93 - var command = process.command();
94 - command.add("cmd.exe");
95 - command.add("/c");
96 - command.add("mklink");
97 - if (is_directory) {
98 - command.add("/d");
15 + if (arguments.length == 1) {
16 + if ("clean".equals(arguments[0])) {
17 + do_clean = true;
18 + } else {
19 + dorkfile_path_input = arguments[0];
20 + }
21 + } else if (arguments.length == 2) {
22 + if ("clean".equals(arguments[0])) {
23 + do_clean = true;
24 + dorkfile_path_input = arguments[1];
25 + } else {
26 + usage();
27 + throw fail();
28 + }
29 + } else if (arguments.length > 2) {
30 + usage();
31 + throw fail();
32 + }
33 + var dorkfiles_root = Path.of(dorkfile_path_input).toRealPath();
34 + log("Assuming dorkfiles root repo in {0}.",
35 + emphasize_local(dorkfiles_root.toString()));
36 + if (!do_clean)
37 + decrypt_secrets(dorkfiles_root.resolve("tresor"));
38 + var hostname = hostname();
39 + log("Hostname: {0}", emphasize_global(hostname));
40 + var symlinks_file = dorkfiles_root.resolve(hostname + ".symlinks");
41 + if (Files.exists(symlinks_file)) {
42 + if (do_clean)
43 + delete_symlinks(dorkfiles_root, symlinks_file);
44 + else
45 + create_symlinks(dorkfiles_root, symlinks_file);
46 + } else {
47 + throw fail("Expected a file containing symlinks at: {0}. Found nothing!",
48 + symlinks_file);
100 - command.add(target_path.toString());
101 - command.add(source_path.toString());
102 - log(command.toString());
103 - process.inheritIO().start().onExit().join();
104 - } else {
105 - __.printStackTrace();
106 - throw fail("Could not create symbolic link!");
107 - }
109 - }
111 - static String hostname() throws Exception {
112 - var process_builder = new ProcessBuilder().command("hostname");
113 - Process process;
114 - try {
115 - process = process_builder.start().onExit().join();
116 - } catch (IOException __) {
117 - process = new ProcessBuilder()
52 + static void decrypt_secrets(Path secrets_root) throws Throwable {
53 + log("{0}:Decrypting files in {1} folder.", emphasize_global("TODO"),
54 + emphasize_local(secrets_root.getFileName().toString()));
55 + }
57 + static void create_symlinks(Path root, Path symlinks_file) throws Throwable {
58 + log("Creating symlinks as defined in {0} file.",
59 + emphasize_local(symlinks_file.getFileName().toString()));
60 + 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 + }
76 + }
78 + static void delete_symlinks(Path root, Path symlinks_file) throws Throwable {
79 + log("Deleting symlinks as defined in {0} file.",
80 + emphasize_local(symlinks_file.getFileName().toString()));
81 + // TODO extract parse pattern into method
82 + 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 + }
98 + }
100 + static void delete(Path root, String source, String target) throws Throwable {
101 + var home = System.getProperty("user.home");
102 + var source_path = root.resolve(source).toAbsolutePath();
103 + var target_path = Path.of(target.replace("~", home)).toAbsolutePath();
105 + if (Files.exists(target_path, LinkOption.NOFOLLOW_LINKS)) {
106 + var symlink_target = target_path.toRealPath();
107 + if (symlink_target.compareTo(source_path) == 0) {
108 + log("\nOK {0} exists and points to {1} in dorkfiles!\nDeleting {0}.",
109 + emphasize_global(target_path.toString()),
110 + emphasize_local(source_path.toString()));
111 + // a symlink is always a file. no need to handle folders
112 + Files.delete(target_path);
113 + } else {
114 + log("\nNOPE {0} exists, but it points outside of dorkfiles! Clean manually.",
115 + target_path.toString());
120 + static void link(Path root, String source, String target) throws Throwable {
121 + var home = System.getProperty("user.home");
122 + var source_path = root.resolve(source).toAbsolutePath();
123 + var target_path = Path.of(target.replace("~", home)).toAbsolutePath();
124 + var is_directory = Files.isDirectory(source_path);
125 + if (Files.exists(target_path, LinkOption.NOFOLLOW_LINKS)) {
126 + if (Files.exists(target_path)) {
127 + log("\nOK {0} already exists!\nNot linking {1}.",
128 + emphasize_global(target_path.toString()),
129 + emphasize_local(source_path.toString()));
130 + return;
131 + } else {
132 + log("\nNOPE {0} exists, but it points into nirvana. Removing broken link!",
133 + target_path.toString());
134 + Files.delete(target_path);
137 + log("\nLINK {0} TO {1}.", emphasize_local(source_path.toString()),
138 + emphasize_local(target_path.toString()));
139 + Files.createDirectories(target_path.getParent());
140 + try {
141 + Files.createSymbolicLink(target_path, source_path);
142 + } catch (Exception __) {
143 + /*
144 + * Creating symbolic links on Windows only recently became possible
145 + * without admin rights. And even that, is only available in Developer
146 + * Mode. However, most tools (including JDK) have not yet adapted to this
147 + * change and still require admin rights. Until that is fixed, we escape
148 + * to a system tool that is known to behave correctly in this regard.
149 + */
150 + var operating_system = System.getProperty("os.name");
151 + if (operating_system.toLowerCase().contains("win")) {
152 + var process = new ProcessBuilder();
153 + var command = process.command();
154 + command.add("cmd.exe");
155 + command.add("/c");
156 + command.add("mklink");
157 + if (is_directory) {
158 + command.add("/d");
160 + command.add(target_path.toString());
161 + command.add(source_path.toString());
162 + log(command.toString());
163 + process.inheritIO().start().onExit().join();
164 + } else {
165 + __.printStackTrace();
166 + throw fail("Could not create symbolic link!");
171 + static String hostname() throws Exception {
172 + var process_builder = new ProcessBuilder().command("hostname");
173 + Process process;
174 + try {
175 + process = process_builder.start().onExit().join();
176 + } catch (IOException __) {
177 + process = new ProcessBuilder()
118 178 .command("hostnamectl", "hostname")
119 179 .start()
120 180 .onExit()
121 181 .join();
183 + try (var reader = new InputStreamReader(process.getInputStream())) {
184 + int character = -1;
185 + var output = new StringBuilder();
186 + while ((character = reader.read()) != -1) {
187 + if (character == '\n' || character == '\r')
188 + continue;
189 + output.append((char) character);
191 + return output.toString();
123 - try (var reader = new InputStreamReader(process.getInputStream())) {
124 - int character = -1;
125 - var output = new StringBuilder();
126 - while ((character = reader.read()) != -1) {
127 - if (character == '\n' || character == '\r')
128 - continue;
129 - output.append((char)character);
130 - }
131 - return output.toString();
195 + static void usage() {
196 + log("Bootstraps bugabingas {0}.", emphasize_local("dorkfiles"));
197 + log("{0}: java bootstripper.java [clean] [dotfiles root path]",
198 + emphasize_global("Usage"));
133 - }
135 - static void usage() {
136 - log("Bootstraps bugabingas {0}.", emphasize_local("dorkfiles"));
137 - log("{0}: java bootstripper.java [dotfiles root path]",
138 - emphasize_global("Usage"));
139 - }
201 + static String emphasize_global(String message) {
202 + return "\033[4m" + message + "\033[0m";
141 - static String emphasize_global(String message) {
142 - return "\033[4m" + message + "\033[0m";
143 - }
205 + static String emphasize_local(String message) {
206 + return "\033[3m" + message + "\033[0m";
145 - static String emphasize_local(String message) {
146 - return "\033[3m" + message + "\033[0m";
147 - }
209 + static Throwable fail(String message, Object... arguments) {
210 + log(message, arguments);
211 + return fail();
149 - static Throwable fail(String message, Object... arguments) {
150 - log(message, arguments);
151 - return fail();
152 - }
214 + static Throwable fail() {
215 + System.exit(-1);
216 + return new Throwable("use 'throw fail()' for control flow");
154 - static Throwable fail() {
155 - System.exit(-1);
156 - return new Throwable("use 'throw fail()' for control flow");
157 - }
159 - static void log(String message, Object... arguments) {
160 - System.out.println(format(message, arguments));
161 - }
219 + static void log(String message, Object... arguments) {
220 + System.out.println(format(message, arguments));

pop-os.symlinks

Modified
7 7 cargo/config.toml
8 8 ~/.cargo/config.toml
10 -nushell
11 -~/.config/nushell
10 +nushell/completions
11 +~/.config/nushell/completions
13 +nushell/config.nu
14 +~/.config/nushell/config.nu
16 +nushell/env.nu
17 +~/.config/nushell/env.nu
19 +nushell/modules
20 +~/.config/nushell/modules
22 +nushell/plugin.nu
23 +~/.config/nushell/plugin.nu
13 25 stackoverflow/so
14 26 ~/.config/so

x230.symlinks

Modified
7 7 cargo/config.toml
8 8 ~/.cargo/config.toml
10 -nushell
11 -~/.config/nushell
10 +nushell/completions
11 +~/.config/nushell/completions
13 +nushell/config.nu
14 +~/.config/nushell/config.nu
16 +nushell/env.nu
17 +~/.config/nushell/env.nu
19 +nushell/modules
20 +~/.config/nushell/modules
22 +nushell/plugin.nu
23 +~/.config/nushell/plugin.nu
13 25 icc/thinkpad_x230.icc
14 26 ~/.config/icc/thinkpad_x230.icc