1 1 import java.
nio.
file.
*;
6 - public static void main(
String[] arguments)
throws Exception {
7 - if(arguments.length
== 0){
4 +import static java.
text.
MessageFormat.
format;
7 + public static void main(
String[] arguments)
throws Throwable {
8 + var dorkfile_path_input
= ".";
9 + if (arguments.length
== 1) {
10 + dorkfile_path_input
= arguments[
0];
11 + }
else if (arguments.length
> 1){
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()));
15 + var dorkfiles_root
= Path.of(dorkfile_path_input).toRealPath();
16 + log(
"Assuming dorkfiles root repo in {0}.", emphasize_local(dorkfiles_root.toString()));
13 17 decrypt_secrets(dorkfiles_root.resolve(
"tresor"));
14 18 var hostname
= hostname();
15 - System.out.printf(
"Hostname: %s%n", emphasize_global(hostname));
19 + log(
"Hostname: {0}", emphasize_global(hostname));
16 20 var symlinks_file
= hostname
+ ".symlinks";
17 21 create_symlinks(dorkfiles_root, dorkfiles_root.resolve(symlinks_file));
20 - static void decrypt_secrets(
Path secrets_root)
throws Exception {
21 - System.out.printf(
"%s:Decrypting files in %s folder.%n", emphasize_global(
"TODO"), emphasize_local(secrets_root.getFileName().toString()));
24 + static void decrypt_secrets(
Path secrets_root)
throws Throwable {
25 + log(
"{0}:Decrypting files in {1} folder.", emphasize_global(
"TODO"),
26 + emphasize_local(secrets_root.getFileName().toString()));
24 - static void create_symlinks(
Path root,
Path symlinks_file)
throws Exception {
25 - System.out.printf(
"Creating symlinks as defined in %s file.%n", emphasize_local(symlinks_file.getFileName().toString()));
29 + static void create_symlinks(
Path root,
Path symlinks_file)
throws Throwable {
30 + log(
"Creating symlinks as defined in {0} file.",
31 + emphasize_local(symlinks_file.getFileName().toString()));
26 32 var lines
= Files.readAllLines(symlinks_file);
27 33 String source
= null;
28 34 String target
= null;
29 - for(
String line
: lines){
30 - if(line.isBlank())
continue;
31 - if (source
== null) source
= line;
32 - else if (target
== null) target
= line;
33 - if(source
!= null && target
!= null){
35 + for (
String line
: lines) {
42 + if (target
!= null) {
34 43 link(root, source, target);
41 - static void link(
Path root,
String source,
String target) throws
Exception {
50 + static void link(
Path root,
String source,
String target) throws
Throwable {
42 51 var home
= System.getProperty(
"user.home");
43 52 var source_path
= root.resolve(source).toAbsolutePath();
44 53 var target_path
= Path.of(target.replace(
"~", home)).toAbsolutePath();
45 54 var is_directory
= Files.isDirectory(source_path);
46 - if(
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)){
47 - if(
Files.exists(target_path)) {
48 - System.out.printf(
"%n✅ %s already exists!%nNot linking %s.%n", emphasize_global(target_path.toString()), emphasize_local(source_path.toString()));
55 + if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
56 + if (
Files.exists(target_path)) {
57 + log(
"\n✓ {0} already exists!\nNot linking {1}.", emphasize_global(target_path.toString()),
58 + emphasize_local(source_path.toString()));
52 - System.out.printf(
"%n❌ %s exists, but it points into nirvana. Removing broken link!%n", target_path.toString());
61 + log(
"\n❌ {0} exists, but it points into nirvana. Removing broken link!", target_path.toString());
53 62 Files.delete(target_path);
56 - System.out.printf(
"%n🔗 %s ➔ %s.%n", emphasize_local(source_path.toString()), emphasize_local(target_path.toString()));
65 + log(
"\n🔗 {0} ➔ {1}.", emphasize_local(source_path.toString()),
66 + emphasize_local(target_path.toString()));
57 67 Files.createDirectories(target_path.getParent());
59 69 Files.createSymbolicLink(target_path, source_path);
61 - catch (
Exception __) {
70 + }
catch (
Exception __) {
67 79 var operating_system
= System.getProperty(
"os.name");
68 - if(operating_system.toLowerCase().contains(
"win")){
80 + if (operating_system.toLowerCase().contains(
"win")) {
69 81 var process
= new ProcessBuilder();
70 82 var command
= process.command();
71 83 command.add(
"cmd.exe");
73 85 command.add(
"mklink");
77 89 command.add(target_path.toString());
78 90 command.add(source_path.toString());
79 91 process.inheritIO()
85 96 __.printStackTrace();
86 - fail(
"Could not create symbolic link!");
97 + throw fail(
"Could not create symbolic link!");
95 106 process
= process_builder
100 - catch(
IOException __ ) {
110 + }
catch (
IOException __) {
101 111 process
= new ProcessBuilder()
102 - .command(
"hostnamectl",
"hostname")
112 + .command(
"hostnamectl",
"hostname")
107 - try(
var reader
= new InputStreamReader(process.getInputStream()))
117 + try (
var reader
= new InputStreamReader(process.getInputStream())) {
109 118 int character
= -1;
110 119 var output
= new StringBuilder();
111 - while((character
= reader.read())
!= -1){
112 - if(character
== '\n' || character
== '\r')
continue;
113 - output.append((
char)character);
120 + while ((character
= reader.read())
!= -1) {
121 + if (character
== '\n' || character
== '\r')
123 + output.append((
char) character);
115 125 return output.toString();
119 129 static void usage() {
120 - System.out.printf(
"Bootstraps bugabingas %s.%n", emphasize_local(
"dorkfiles"));
121 - System.out.printf(
"%s: java bootstripper.java <dotfiles root path>%n", emphasize_global(
"Usage"));
124 - static void exit() {
130 + log(
"Bootstraps bugabingas {0}.", emphasize_local(
"dorkfiles"));
131 + log(
"{0}: java bootstripper.java [dotfiles root path]", emphasize_global(
"Usage"));
128 134 static String emphasize_global(
String message) {
133 139 return "\033[3m" + message
+ "\033[0m";
136 - static void fail(
String message,
Object... arguments) {
137 - System.out.printf(message, arguments);
142 + static Throwable fail(
String message,
Object... arguments) {
143 + log(message, arguments);
141 - static void fail() {
147 + static Throwable fail() {
149 + return new Throwable(
"use 'throw fail()' for control flow");
152 + static void log(
String message,
Object... arguments) {
153 + System.out.println(format(message, arguments));