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.
List;
9 +import java.
util.
function.
BiFunction;
10 11 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) {
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);
29 - throw fail(
"Expected a file containing symlinks at: {0}. Found nothing!",
12 + public static void main(
String[] arguments)
throws Throwable {
13 + var dorkfile_path_input
= ".";
14 + 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()));
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) {
52 - if (target
!= null) {
53 - link(root, source, target);
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()));
72 - log(
"\nNOPE {0} exists, but it points into nirvana. Removing broken link!",
73 - target_path.toString());
74 - Files.delete(target_path);
77 - log(
"\nLINK {0} TO {1}.", emphasize_local(source_path.toString()),
78 - emphasize_local(target_path.toString()));
79 - Files.createDirectories(target_path.getParent());
81 - Files.createSymbolicLink(target_path, source_path);
82 - }
catch (
Exception __) {
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");
96 - command.add(
"mklink");
16 + if (arguments.length
== 1) {
17 + if (
"clean".equals(arguments[
0])) {
20 + dorkfile_path_input
= arguments[
0];
22 + }
else if (arguments.length
== 2) {
23 + if (
"clean".equals(arguments[
0])) {
25 + dorkfile_path_input
= arguments[
1];
30 + }
else if (arguments.length
> 2) {
34 + var dorkfiles_root
= Path.of(dorkfile_path_input).toRealPath();
35 + log(
"Assuming dorkfiles root repo in {0}.",
36 + emphasize_local(dorkfiles_root.toString()));
38 + decrypt_secrets(dorkfiles_root.resolve(
"tresor"));
39 + var hostname
= hostname();
40 + log(
"Hostname: {0}", emphasize_global(hostname));
41 + var symlinks_file
= dorkfiles_root.resolve(hostname
+ ".symlinks");
42 + if (
Files.exists(symlinks_file)) {
44 + delete_symlinks(dorkfiles_root, symlinks_file);
46 + create_symlinks(dorkfiles_root, symlinks_file);
48 + throw fail(
"Expected a file containing symlinks at: {0}. Found nothing!",
100 - command.add(target_path.toString());
101 - command.add(source_path.toString());
102 - log(command.toString());
103 - process.inheritIO().start().onExit().join();
105 - __.printStackTrace();
106 - throw fail(
"Could not create symbolic link!");
111 - static String hostname()
throws Exception {
112 - var process_builder
= new ProcessBuilder().command(
"hostname");
115 - process
= process_builder.start().onExit().join();
116 - }
catch (
IOException __) {
117 - process
= new ProcessBuilder()
53 + static void decrypt_secrets(
Path secrets_root)
throws Throwable {
54 + log(
"{0}:Decrypting files in {1} folder.", emphasize_global(
"TODO"),
55 + emphasize_local(secrets_root.getFileName().toString()));
58 + static void create_symlinks(
Path root,
Path symlinks_file)
throws Throwable {
59 + log(
"Creating symlinks as defined in {0} file.",
60 + emphasize_local(symlinks_file.getFileName().toString()));
61 + var lines
= Files.readAllLines(symlinks_file);
62 + for_each_mapping(lines, (source, target)
-> link(root, source, target));
65 + static void delete_symlinks(
Path root,
Path symlinks_file)
throws Throwable {
66 + log(
"Deleting symlinks as defined in {0} file.",
67 + emphasize_local(symlinks_file.getFileName().toString()));
68 + var lines
= Files.readAllLines(symlinks_file);
69 + for_each_mapping(lines, (source, target)
-> delete(root, source, target));
72 + static void delete(
Path root,
String source,
String target)
throws Throwable {
73 + var home
= System.getProperty(
"user.home");
74 + var source_path
= root.resolve(source).toAbsolutePath();
75 + var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
77 + if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
78 + var symlink_target
= target_path.toRealPath();
79 + if (symlink_target.compareTo(source_path)
== 0) {
80 + log(
"\nOK {0} exists and points to {1} in dorkfiles!\nDeleting {0}.",
81 + emphasize_global(target_path.toString()),
82 + emphasize_local(source_path.toString()));
84 + Files.delete(target_path);
86 + log(
"\nNOPE {0} exists, but it points outside of dorkfiles! Clean manually.",
87 + target_path.toString());
92 + static void link(
Path root,
String source,
String target)
throws Throwable {
93 + var home
= System.getProperty(
"user.home");
94 + var source_path
= root.resolve(source).toAbsolutePath();
95 + var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
96 + var is_directory
= Files.isDirectory(source_path);
97 + if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
98 + if (
Files.exists(target_path)) {
99 + log(
"\nOK {0} already exists!\nNot linking {1}.",
100 + emphasize_global(target_path.toString()),
101 + emphasize_local(source_path.toString()));
104 + log(
"\nNOPE {0} exists, but it points into nirvana. Removing broken link!",
105 + target_path.toString());
106 + Files.delete(target_path);
109 + log(
"\nLINK {0} TO {1}.", emphasize_local(source_path.toString()),
110 + emphasize_local(target_path.toString()));
111 + Files.createDirectories(target_path.getParent());
113 + Files.createSymbolicLink(target_path, source_path);
114 + }
catch (
Exception __) {
122 + var operating_system
= System.getProperty(
"os.name");
123 + if (operating_system.toLowerCase().contains(
"win")) {
124 + var process
= new ProcessBuilder();
125 + var command
= process.command();
126 + command.add(
"cmd.exe");
128 + command.add(
"mklink");
129 + if (is_directory) {
132 + command.add(target_path.toString());
133 + command.add(source_path.toString());
134 + log(command.toString());
135 + process.inheritIO().start().onExit().join();
137 + __.printStackTrace();
138 + throw fail(
"Could not create symbolic link!");
143 + static String hostname()
throws Exception {
144 + var process_builder
= new ProcessBuilder().command(
"hostname");
147 + process
= process_builder.start().onExit().join();
148 + }
catch (
IOException __) {
149 + process
= new ProcessBuilder()
118 150 .command(
"hostnamectl",
"hostname")
155 + try (
var reader
= new InputStreamReader(process.getInputStream())) {
156 + int character
= -1;
157 + var output
= new StringBuilder();
158 + while ((character
= reader.read())
!= -1) {
159 + if (character
== '\n' || character
== '\r')
161 + output.append((
char) character);
163 + var name
= output.toString();
164 + if(
"localhost".equals(name) ) {
166 + assert System.getenv(
"PREFIX")
!= null;
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')
129 - output.append((
char)character);
131 - var name
= output.toString();
132 - if(
"localhost".equals(name) ) {
134 - assert System.getenv(
"PREFIX")
!= null;
137 - return output.toString();
173 + static void usage() {
174 + log(
"Bootstraps bugabingas {0}.", emphasize_local(
"dorkfiles"));
175 + log(
"{0}: java bootstripper.java [clean] [dotfiles root path]",
176 + emphasize_global(
"Usage"));
141 - static void usage() {
142 - log(
"Bootstraps bugabingas {0}.", emphasize_local(
"dorkfiles"));
143 - log(
"{0}: java bootstripper.java [dotfiles root path]",
144 - emphasize_global(
"Usage"));
179 + static String emphasize_global(
String message) {
180 + return "\033[4m" + message
+ "\033[0m";
147 - static String emphasize_global(
String message) {
148 - return "\033[4m" + message
+ "\033[0m";
183 + static String emphasize_local(
String message) {
184 + return "\033[3m" + message
+ "\033[0m";
151 - static String emphasize_local(
String message) {
152 - return "\033[3m" + message
+ "\033[0m";
187 + static Throwable fail(
String message,
Object... arguments) {
188 + log(message, arguments);
155 - static Throwable fail(
String message,
Object... arguments) {
156 - log(message, arguments);
192 + static Throwable fail() {
194 + return new Throwable(
"use 'throw fail()' for control flow");
160 - static Throwable fail() {
162 - return new Throwable(
"use 'throw fail()' for control flow");
197 + static void log(
String message,
Object... arguments) {
198 + System.out.println(format(message, arguments));
165 - static void log(
String message,
Object... arguments) {
166 - System.out.println(format(message, arguments));
201 + static interface ThrowUp<A, B> {
202 + default Throwable gurgh(
A a,
B b) {
206 + }
catch (
Throwable vomit) {
211 + void apply(
A a,
B b)
throws Throwable;
214 + static void for_each_mapping(
List<
String> lines,
ThrowUp<
String,
String> block)
216 + String source
= null;
217 + String target
= null;
218 + for (
String line
: lines) {
219 + if (line.isBlank())
221 + if (source
== null)
225 + if (target
!= null) {
226 + var vomit
= block.gurgh(source, target);