repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

bye bye cosmopolitan, hello java

52636d45e086a7f26643b3ab99203401164bb4a7

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

unsigned

Browse filesCompare with HEAD

Changed files (2)

TODO.md

Modified
1 1 # TODO
3 -- [x] remove cosmopolitan from git
4 -- [ ] add download script for cross9 and cosmopolitan
5 - - [x] win32
6 - - [ ] linux
7 3 - [ ] what do we do about ssh and gpg keys? or secrets in general?
8 4 - [x] say goodbye to gpg, too hard for me...
9 -- [ ] how to handle finding the symlink file in bootstripper? turns out, getting path of executable is not trivial.
10 -- [ ] cosmopolitan exe rewrites itself on first use. how do we handle this so a pristine copy is in git...?

bootstripper.java

Added
new file mode 100644
1 +import java.nio.file.*;
2 +import java.io.*;
3 +import java.util.*;
5 +class bootstripper{
6 + public static void main(String[] arguments) throws Exception {
7 + if(arguments.length == 0){
8 + usage();
9 + fail();
10 + }
11 + var dorkfiles_root = Path.of(arguments[0]).toRealPath();
12 + System.out.printf("Assuming dorkfiles root repo in %s.%n", emphasize_local(dorkfiles_root.toString()));
13 + decrypt_secrets(dorkfiles_root.resolve("secrets"));
14 + var hostname = hostname();
15 + System.out.printf("Hostname: %s%n", emphasize_global(hostname));
16 + var symlinks_file = hostname + ".symlinks";
17 + create_symlinks(dorkfiles_root.resolve(symlinks_file));
18 + }
19 + static void decrypt_secrets(Path secrets_root) throws Exception {
20 + System.out.printf("Decrypting files in %s folder.%n", emphasize_local(secrets_root.getFileName().toString()));
21 + }
22 + static void create_symlinks(Path symlinks_file) throws Exception {
23 + System.out.printf("Creating symlinks as defined in %s file.%n", emphasize_local(symlinks_file.getFileName().toString()));
24 + var lines = Files.readAllLines(symlinks_file);
25 + String source = null;
26 + String target = null;
27 + for(String line : lines){
28 + if(line.isBlank()) continue;
29 + if (source == null) source = line;
30 + else if (target == null) target = line;
31 + if(source != null && target != null){
32 + link(source, target);
33 + source = null;
34 + target = null;
35 + }
36 + }
37 + }
38 + static void link(String source, String target) throws Exception {
39 + var home = System.getProperty("user.home");
40 + var source_path = Path.of(source.replace("~", home)).toAbsolutePath();
41 + var target_path = Path.of(target.replace("~", home)).toAbsolutePath();
42 + var is_directory = Files.isDirectory(source_path);
43 + if(Files.exists(target_path)){
44 + System.out.printf("%s already exists! Not linking %s.%n", emphasize_global(target_path.toString()), emphasize_local(source_path.toString()));
45 + }
46 + else{
47 + System.out.printf("%s ➔ %s.%n", emphasize_local(source_path.toString()), emphasize_local(target_path.toString()));
48 + if(is_directory){
49 + Files.createDirectories(target_path);
50 + }
51 + else{
52 + Files.createDirectories(target_path.getParent());
53 + }
54 + try{
55 + Files.createSymbolicLink(source_path, target_path);
56 + } catch (Exception __) {
57 + /*
58 + * Creating symbolic links on Windows only recently became possible without admin rights.
59 + * However, most tools (including JDK) have not yet adapted to this change and still require admin rights.
60 + * Until that is fixed, we escape to a system tool that is known to behave correcty in this regard.
61 + */
62 + var operating_system = System.getProperty("os.name");
63 + if(operating_system.toLowerCase().contains("win")){
64 + var process = new ProcessBuilder();
65 + var command = process.command();
66 + command.add("cmd.exe");
67 + command.add("/c");
68 + command.add("mklink");
69 + if(is_directory){
70 + command.add("/d");
71 + }
72 + command.add(target_path.toString());
73 + command.add(source_path.toString());
74 + process.inheritIO()
75 + .start()
76 + .onExit()
77 + .join();
78 + }
79 + }
80 + }
81 + }
82 + static String hostname() throws Exception {
83 + var process = new ProcessBuilder()
84 + .command("hostname")
85 + .start()
86 + .onExit()
87 + .join();
88 + try(var reader = new InputStreamReader(process.getInputStream()))
89 + {
90 + int character = -1;
91 + var output = new StringBuilder();
92 + while((character = reader.read()) != -1){
93 + if(character == '\n' || character == '\r') continue;
94 + output.append((char)character);
95 + }
96 + return output.toString();
97 + }
98 + }
99 + static void usage() {
100 + System.out.printf("Bootstraps bugabingas %s.%n", emphasize_local("dorkfiles"));
101 + System.out.printf("%s: java bootstripper.java <dotfiles root path>%n", emphasize_global("Usage"));
103 + static void exit() {
104 + System.exit(0);
106 + static String emphasize_global(String message) {
107 + return "\033[4m" + message + "\033[0m";
109 + static String emphasize_local(String message) {
110 + return "\033[3m" + message + "\033[0m";
112 + static void fail(String message, Object... arguments) {
113 + System.out.printf(message, arguments);
114 + System.exit(-1);
116 + static void fail() {
117 + System.exit(-1);