15 15 var do_decrypt
= false;
17 17 if (arguments.length
== 1) {
18 - if (
"clean".equals(arguments[
0])) {
21 - else if(
"decrypt".equals(arguments[
0])) {
24 - dorkfile_path_input
= arguments[
0];
18 + if (
"clean".equals(arguments[
0])) {
21 + else if(
"decrypt".equals(arguments[
0])) {
24 + dorkfile_path_input
= arguments[
0];
26 26 }
else if (arguments.length
== 2) {
27 - if (
"clean".equals(arguments[
0])) {
29 - dorkfile_path_input
= arguments[
1];
31 - else if (
"decrypt".equals(arguments[
0])) {
33 - dorkfile_path_input
= arguments[
1];
38 - }
else if (arguments.length
> 2) {
27 + if (
"clean".equals(arguments[
0])) {
29 + dorkfile_path_input
= arguments[
1];
31 + else if (
"decrypt".equals(arguments[
0])) {
33 + dorkfile_path_input
= arguments[
1];
38 + }
else if (arguments.length
> 2) {
42 42 var dorkfiles_root
= Path.of(dorkfile_path_input).toRealPath();
43 43 log(
"Assuming dorkfiles root repo in {0}.",
44 - emphasize_local(dorkfiles_root.toString()));
44 + emphasize_local(dorkfiles_root.toString()));
46 - decrypt_secrets(dorkfiles_root.resolve(
"tresor"));
48 - var hostname
= hostname();
49 - log(
"Hostname: {0}", emphasize_global(hostname));
50 - var symlinks_file
= dorkfiles_root.resolve(hostname
+ ".symlinks");
51 - if (
Files.exists(symlinks_file)) {
53 - delete_symlinks(dorkfiles_root, symlinks_file);
55 - create_symlinks(dorkfiles_root, symlinks_file);
57 - throw fail(
"Expected a file containing symlinks at: {0}. Found nothing!",
46 + decrypt_secrets(dorkfiles_root.resolve(
"tresor"));
48 + var hostname
= hostname();
49 + log(
"Hostname: {0}", emphasize_global(hostname));
50 + var symlinks_file
= dorkfiles_root.resolve(hostname
+ ".symlinks");
51 + if (
Files.exists(symlinks_file)) {
53 + delete_symlinks(dorkfiles_root, symlinks_file);
55 + create_symlinks(dorkfiles_root, symlinks_file);
57 + throw fail(
"Expected a file containing symlinks at: {0}. Found nothing!",
63 63 static void decrypt_secrets(
Path secrets_root) throws
Throwable {
64 - log(
"{0}:Decrypting files in {1} folder, using rbw.", emphasize_global(
"TODO"),
65 - emphasize_local(secrets_root.getFileName().toString()));
64 + log(
"{0}:Decrypting files in {1} folder, using rbw.", emphasize_global(
"TODO"),
65 + emphasize_local(secrets_root.getFileName().toString()));
68 68 static void create_symlinks(
Path root,
Path symlinks_file) throws
Throwable {
69 - log(
"Creating symlinks as defined in {0} file.",
70 - emphasize_local(symlinks_file.getFileName().toString()));
71 - var lines
= Files.readAllLines(symlinks_file);
72 - for_each_mapping(lines, (source, target)
-> link(root, source, target));
69 + log(
"Creating symlinks as defined in {0} file.",
70 + emphasize_local(symlinks_file.getFileName().toString()));
71 + var lines
= Files.readAllLines(symlinks_file);
72 + for_each_mapping(lines, (source, target)
-> link(root, source, target));
75 75 static void delete_symlinks(
Path root,
Path symlinks_file) throws
Throwable {
76 - log(
"Deleting symlinks as defined in {0} file.",
77 - emphasize_local(symlinks_file.getFileName().toString()));
78 - var lines
= Files.readAllLines(symlinks_file);
79 - for_each_mapping(lines, (source, target)
-> delete(root, source, target));
76 + log(
"Deleting symlinks as defined in {0} file.",
77 + emphasize_local(symlinks_file.getFileName().toString()));
78 + var lines
= Files.readAllLines(symlinks_file);
79 + for_each_mapping(lines, (source, target)
-> delete(root, source, target));
82 82 static void delete(
Path root,
String source,
String target) throws
Throwable {
83 - var home
= System.getProperty(
"user.home");
84 - var source_path
= root.resolve(source).toAbsolutePath();
85 - var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
83 + var home
= System.getProperty(
"user.home");
84 + var source_path
= root.resolve(source).toAbsolutePath();
85 + var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
87 - if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
88 - if (
Files.isSymbolicLink(target_path)) {
89 - log(
"\nOK {0} exists and points to {1}, a symlink indeed!\nDeleting {0}.",
90 - emphasize_global(target_path.toString()),
91 - emphasize_local(source_path.toString()));
93 - Files.delete(target_path);
95 - log(
"\nNOPE {0} exists, but it is not a symlink! Clean manually.",
96 - target_path.toString());
87 + if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
88 + if (
Files.isSymbolicLink(target_path)) {
89 + log(
"\nOK {0} exists and points to {1}, a symlink indeed!\nDeleting {0}.",
90 + emphasize_global(target_path.toString()),
91 + emphasize_local(source_path.toString()));
93 + Files.delete(target_path);
95 + log(
"\nNOPE {0} exists, but it is not a symlink! Clean manually.",
96 + target_path.toString());
101 101 static void link(
Path root,
String source,
String target) throws
Throwable {
102 - var home
= System.getProperty(
"user.home");
103 - var source_path
= root.resolve(source).toAbsolutePath();
104 - var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
105 - var is_directory
= Files.isDirectory(source_path);
106 - if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
107 - if (
Files.exists(target_path)) {
108 - log(
"\nOK {0} already exists!\nNot linking {1}.",
109 - emphasize_global(target_path.toString()),
110 - emphasize_local(source_path.toString()));
113 - log(
"\nNOPE {0} exists, but it points into nirvana. Removing broken link!",
114 - target_path.toString());
115 - Files.delete(target_path);
102 + var home
= System.getProperty(
"user.home");
103 + var source_path
= root.resolve(source).toAbsolutePath();
104 + var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
105 + var is_directory
= Files.isDirectory(source_path);
106 + if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
107 + if (
Files.exists(target_path)) {
108 + log(
"\nOK {0} already exists!\nNot linking {1}.",
109 + emphasize_global(target_path.toString()),
110 + emphasize_local(source_path.toString()));
113 + log(
"\nNOPE {0} exists, but it points into nirvana. Removing broken link!",
114 + target_path.toString());
115 + Files.delete(target_path);
118 - log(
"\nLINK {0} TO {1}.", emphasize_local(source_path.toString()),
119 - emphasize_local(target_path.toString()));
120 - Files.createDirectories(target_path.getParent());
122 - Files.createSymbolicLink(target_path, source_path);
123 - }
catch (
Exception __) {
131 - var operating_system
= System.getProperty(
"os.name");
132 - if (operating_system.toLowerCase().contains(
"win")) {
133 - var process
= new ProcessBuilder();
134 - var command
= process.command();
135 - command.add(
"cmd.exe");
137 - command.add(
"mklink");
138 - if (is_directory) {
141 - command.add(target_path.toString());
142 - command.add(source_path.toString());
143 - log(command.toString());
144 - process.inheritIO().start().onExit().join();
146 - __.printStackTrace();
147 - throw fail(
"Could not create symbolic link!");
118 + log(
"\nLINK {0} TO {1}.", emphasize_local(source_path.toString()),
119 + emphasize_local(target_path.toString()));
120 + Files.createDirectories(target_path.getParent());
122 + Files.createSymbolicLink(target_path, source_path);
123 + }
catch (
Exception __) {
131 + var operating_system
= System.getProperty(
"os.name");
132 + if (operating_system.toLowerCase().contains(
"win")) {
133 + var process
= new ProcessBuilder();
134 + var command
= process.command();
135 + command.add(
"cmd.exe");
137 + command.add(
"mklink");
138 + if (is_directory) {
141 + command.add(target_path.toString());
142 + command.add(source_path.toString());
143 + log(command.toString());
144 + process.inheritIO().start().onExit().join();
146 + __.printStackTrace();
147 + throw fail(
"Could not create symbolic link!");
152 152 static String hostname() throws
Exception {
153 - var process_builder
= new ProcessBuilder().command(
"hostname");
156 - process
= process_builder.start().onExit().join();
157 - }
catch (
IOException __) {
158 - process
= new ProcessBuilder()
159 - .command(
"hostnamectl",
"hostname")
153 + var process_builder
= new ProcessBuilder().command(
"hostname");
156 + process
= process_builder.start().onExit().join();
157 + }
catch (
IOException __) {
158 + process
= new ProcessBuilder()
159 + .command(
"hostnamectl",
"hostname")
164 + try (
var reader
= new InputStreamReader(process.getInputStream())) {
165 + int character
= -1;
166 + var output
= new StringBuilder();
167 + while ((character
= reader.read())
!= -1) {
168 + if (character
== '\n' || character
== '\r')
170 + output.append((
char) character);
164 - try (
var reader
= new InputStreamReader(process.getInputStream())) {
165 - int character
= -1;
166 - var output
= new StringBuilder();
167 - while ((character
= reader.read())
!= -1) {
168 - if (character
== '\n' || character
== '\r')
170 - output.append((
char) character);
172 - var name
= output.toString();
173 - if(
"localhost".equals(name) ) {
175 - assert System.getenv(
"PREFIX")
!= null;
172 + var name
= output.toString();
173 + if(
"localhost".equals(name) ) {
175 + assert System.getenv(
"PREFIX")
!= null;
182 182 static void usage() {
183 - log(
"Bootstraps bugabingas {0}.", emphasize_local(
"dorkfiles"));
184 - log(
"{0}: java bootstripper.java [clean] [dotfiles root path]",
185 - emphasize_global(
"Usage"));
183 + log(
"Bootstraps bugabingas {0}.", emphasize_local(
"dorkfiles"));
184 + log(
"{0}: java bootstripper.java [clean] [dotfiles root path]",
185 + emphasize_global(
"Usage"));
188 188 static String emphasize_global(
String message) {
189 - return "\033[4m" + message
+ "\033[0m";
189 + return "\033[4m" + message
+ "\033[0m";
192 192 static String emphasize_local(
String message) {
193 - return "\033[3m" + message
+ "\033[0m";
193 + return "\033[3m" + message
+ "\033[0m";
196 196 static Throwable fail(
String message,
Object... arguments) {
197 - log(message, arguments);
197 + log(message, arguments);
201 201 static Throwable fail() {
203 - return new Throwable(
"use 'throw fail()' for control flow");
203 + return new Throwable(
"use 'throw fail()' for control flow");
206 206 static void log(
String message,
Object... arguments) {
207 - System.out.println(format(message, arguments));
207 + System.out.println(format(message, arguments));
210 210 static interface ThrowUp<A, B> {
211 - default Throwable gurgh(
A a,
B b) {
215 - }
catch (
Throwable vomit) {
211 + default Throwable gurgh(
A a,
B b) {
215 + }
catch (
Throwable vomit) {
220 - void apply(
A a,
B b)
throws Throwable;
220 + void apply(
A a,
B b)
throws Throwable;
223 223 static void for_each_mapping(
List<
String> lines,
ThrowUp<
String,
String> block)
225 225 String source
= null;
226 226 String target
= null;
227 227 for (
String line
: lines) {
228 - if (line.isBlank())
230 - if (source
== null)
234 - if (target
!= null) {
235 - var vomit
= block.gurgh(source, target);
228 + if (line.isBlank())
230 + if (source
== null)
234 + if (target
!= null) {
235 + var vomit
= block.gurgh(source, target);