Skip to content

Commit c528843

Browse files
committed
Add javadocs
1 parent 12b1fbc commit c528843

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

src/main/java/cf/maybelambda/fedora/ConfigManager.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ public class ConfigManager {
3030

3131
private static final List<String> adminGroups = List.of("docker", "libvirt", "vboxsf", "vboxusers");
3232

33+
/**
34+
* Reads all lines of a resource / configuration file.
35+
*
36+
* <p>The method first attempts to locate the file on the real filesystem under
37+
* {@code CONFIG_DIR}/{@code filename}. If that file exists, its contents are read.
38+
* When the file is not found in the configured directory, the method falls back
39+
* to loading it from the class‑path (e.g. {@code /filename}).
40+
*
41+
* <p>The resulting list contains each line of the resource exactly as it appears,
42+
* without any trailing line‑separator characters.
43+
*
44+
* @param filename The name of the file to read
45+
* @return List of lines from the requested resource
46+
* @throws IOException If an I/O error occurs while reading the file,
47+
* or if the resource cannot be located on either the filesystem
48+
* nor the class‑path
49+
*/
3350
static List<String> readResourceLines(String filename) throws IOException {
3451
Path path = Path.of(CONFIG_DIR, filename);
3552
if (Files.exists(path)) {
@@ -41,6 +58,19 @@ static List<String> readResourceLines(String filename) throws IOException {
4158
}
4259
}
4360

61+
/**
62+
* Loads package names from a configuration file.
63+
*
64+
* <p>Reads the resource identified by {@code filename} located under
65+
* {@link #CONFIG_DIR CONFIG_DIR} or the class-path.
66+
* Each non‑empty line that does not start with '#' is treated as a package name.
67+
* Lines beginning with '#' are considered comments and ignored. If the file cannot be
68+
* accessed, an error message is written to {@code System.err} and an empty list is returned.
69+
*
70+
* @param filename The resource file name (e.g., {@code dnf-install.cf})
71+
* @return List containing the package identifiers found in the
72+
* file; may be empty if no valid entries are present or an I/O error occurs
73+
*/
4474
static List<String> loadPackageNamesFrom(String filename) {
4575
List<String> packages = new ArrayList<>();
4676
try {

src/main/java/cf/maybelambda/fedora/ConsoleIOHelper.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,33 @@ public class ConsoleIOHelper {
1717
private static final String EXCLUDE_PROMPT = "Type in the number and/or range of numbers of packages to exclude "
1818
+ "(comma-separated, i.e. 2,3,5..9)\nor press Enter to keep all: ";
1919

20+
/**
21+
* Prompts the user for a yes/no confirmation based on the provided {@code prompt}.
22+
*
23+
* @param scanner {@link Scanner} used to read user input; must not be {@code null}
24+
* @param prompt The message displayed before the confirmation prompt
25+
* @return {@code true} if the user confirms with "y" or "yes" (case insensitive),
26+
* {@code false} otherwise
27+
*/
2028
static boolean confirm(Scanner scanner, String prompt) {
2129
System.out.print(prompt + " [y/N]: ");
2230
String input = scanner.nextLine().trim().toLowerCase();
2331
return input.equals("y") || input.equals("yes");
2432
}
2533

34+
/**
35+
* Prompts the user to specify which packages should be excluded from further processing.
36+
*
37+
* <p>The method prints an enumerated list of available package names and then reads a line
38+
* containing comma‑separated indices, ranges (e.g., "2..4"), or both. It validates that the
39+
* input is empty or contains number/s and/or range/s.
40+
* A list containing only the non‑excluded items is returned.
41+
*
42+
* @param packages A list of package identifiers presented for selection; must not be {@code null}
43+
* @param scanner {@link Scanner} used to read user input; must not be {@code null}
44+
* @return List of packages that were not selected for exclusion
45+
* @throws RuntimeException If the user input does not conform to the expected format
46+
*/
2647
static List<String> promptForExclusions(List<String> packages, Scanner scanner) {
2748
for (int i = 0; i < packages.size(); i++) {
2849
System.out.printf("%2d. %s\n", i + 1, packages.get(i));
@@ -66,6 +87,13 @@ static List<String> promptForExclusions(List<String> packages, Scanner scanner)
6687
return filtered;
6788
}
6889

90+
/**
91+
* Prints help documentation to standard output.
92+
*
93+
* <p>Retrieves help info from {@link ConfigManager#getHelpText()} and outputs it.
94+
* If an {@link IOException} occurs while reading the help file,
95+
* an error message is written to standard error.
96+
*/
6997
static void printHelp() {
7098
try {
7199
List<String> lines = ConfigManager.getHelpText();
@@ -75,10 +103,34 @@ static void printHelp() {
75103
}
76104
}
77105

106+
/**
107+
* Wraps a string in ANSI color codes when the underlying terminal supports it.
108+
*
109+
* <p>If ANSI escape sequences are supported for the given {@code term} and an active console is present,
110+
* the resulting string will be prefixed with {@code ansiColorCode} and suffixed with a reset code.
111+
* If coloring is not supported, the original string is returned unchanged.
112+
*
113+
* @param str The text to be colored; must not be {@code null}
114+
* @param ansiColorCode One of the predefined ANSI color codes (e.g., {@link #YELLOW});
115+
* may also be an empty string for no coloring
116+
* @return A possibly-colored version of {@code str}, or {@code str} itself if ANSI is unsupported
117+
*/
78118
static String color(String str, String ansiColorCode) {
79119
return isANSISupported(System.getenv("TERM"), System.console()) ? ansiColorCode + str + RESET : str;
80120
}
81121

122+
/**
123+
* Determines whether the current environment likely supports ANSI escape sequences.
124+
*
125+
* <p>Checks that the terminal type ({@code term}) is non‑null and not equal to "dumb",
126+
* and verifies that an active console stream exists. Returns {@code true} when these
127+
* conditions indicate that colored output can be rendered; otherwise returns {@code false}.
128+
*
129+
* @param term Value of the TERM environment variable
130+
* @param console Underlying console object from {@link Console}; may be {@code null}
131+
* on some platforms
132+
* @return {@code true} if ANSI escape sequences are supported, {@code false} otherwise
133+
*/
82134
static boolean isANSISupported(String term, Console console) {
83135
return console != null && term != null && !term.isEmpty() && !"dumb".equals(term);
84136
}

src/main/java/cf/maybelambda/fedora/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,23 @@ public class Main {
2424
static List<String> CMD_ADD_USER_TO_GROUP = asList("sudo", "usermod", "-aG");
2525
static List<String> CMD_SYSTEMCTL_ENABLE = asList("sudo", "systemctl", "enable", "--now", "cockpit.socket"); // single arg appended to cmd
2626

27+
/**
28+
* Entry point of the program.
29+
* Delegates execution to the {@code Main.run} method.
30+
*/
2731
public static void main(String[] args) {
2832
run(args, new PostInstallUpdater());
2933
}
3034

35+
/**
36+
* Executes the setup workflow based on provided command-line arguments.
37+
*
38+
* <p>This method parses command-line arguments and checks for known flags, sets up interactive prompts,
39+
* and orchestrates various system configuration steps.
40+
*
41+
* @param args Command-line arguments passed to the program at startup
42+
* @param updater {@link PostInstallUpdater} responsible for executing OS commands
43+
*/
3144
static void run(String[] args, PostInstallUpdater updater) {
3245
if (asList(args).contains("-h") || asList(args).contains("--help")) {
3346
ConsoleIOHelper.printHelp();

src/main/java/cf/maybelambda/fedora/PostInstallUpdater.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ void setDryRun(boolean dryRun) {
2222
this.dryRun = dryRun;
2323
}
2424

25+
/**
26+
* Creates a {@link ProcessBuilder} configured with the given command array.
27+
*
28+
* @param cmd An array containing the executable command parts
29+
* @return New {@code ProcessBuilder} instance initialized with {@code cmd}
30+
*
31+
*/
2532
ProcessBuilder createProcessBuilder(String[] cmd) {
2633
return new ProcessBuilder(cmd);
2734
}
2835

36+
/**
37+
* Executes a shell command built from {@code baseCmd} followed by {@code args}.
38+
*
39+
* <p>Prints the full command, displays each line of output,
40+
* waits for termination, reports the exit status, and returns that status code.
41+
* In dry‑run mode, it only informs the caller that no execution will occur.
42+
*
43+
* @param baseCmd List containing the initial command tokens
44+
* @param args Additional arguments to append to {@code baseCmd}
45+
* @return Exit code of the executed process, or {@code -1} if execution was not performed
46+
* due to an error, or {@code 0} if dry-run was enabled
47+
*/
2948
int runCommand(List<String> baseCmd, List<String> args) {
3049
String[] command = concat(baseCmd.stream(), args.stream()).toArray(String[]::new);
3150
System.out.println("Executing shell command: " + color(String.join(" ", command), BLUE));

0 commit comments

Comments
 (0)