77import java .nio .file .Files ;
88import java .nio .file .Path ;
99import java .util .ArrayList ;
10+ import java .util .Arrays ;
1011import java .util .HashSet ;
1112import java .util .List ;
1213import java .util .NoSuchElementException ;
@@ -30,15 +31,23 @@ public class PostInstallUpdater {
3031 private static final String DNF_INSTALL_FILE = "dnf-install.cf" ;
3132 private static final String DNF_REMOVE_FILE = "dnf-remove.cf" ;
3233 private static final String FLATPAK_INSTALL_FILE = "flatpak-install.cf" ;
33- public static final String RESET = "\u001B [0m" ;
34- public static final String YELLOW = "\u001B [33m" ;
34+
35+ private static final String RESET = "\u001B [0m" ;
36+ private static final String YELLOW = "\u001B [33m" ;
37+
38+ private static boolean dryRun = false ;
3539
3640 public static void main (String [] args ) {
41+ setDryRun (Arrays .asList (args ).contains ("--dry-run" ));
3742 List <String > dnfInstallPackages = loadPackageNamesFrom (DNF_INSTALL_FILE );
3843 List <String > dnfRemovePackages = loadPackageNamesFrom (DNF_REMOVE_FILE );
3944 List <String > flatpakInstallPackages = loadPackageNamesFrom (FLATPAK_INSTALL_FILE );
4045 Scanner scanner = new Scanner (System .in );
46+
4147 System .out .println ("Fedora Post Install Actions\n " );
48+ if (isDryRun ()) {
49+ System .out .println ("---[Dry Run Mode] Commands will not be executed.---\n " );
50+ }
4251
4352 // 1. Install RPMFusion repos
4453 if (confirm (scanner , "Install RPMFusion repos?" )) {
@@ -60,7 +69,6 @@ public static void main(String[] args) {
6069 // 2. DNF install packages
6170 if (confirm (scanner , "Install additional packages with DNF?" )) {
6271 List <String > filtered = promptForExclusions (dnfInstallPackages , scanner );
63-
6472 String [] cmd = new String [filtered .size () + 5 ];
6573 cmd [0 ] = "sudo" ;
6674 cmd [1 ] = "dnf" ;
@@ -73,7 +81,7 @@ public static void main(String[] args) {
7381 runCommand (cmd );
7482 }
7583
76- // 3. DNF remove unneeded packages
84+ // 3. DNF remove packages
7785 if (confirm (scanner , "Remove all DNF packages marked for removal?" )) {
7886 List <String > filtered = promptForExclusions (dnfRemovePackages , scanner );
7987 String [] cmd = new String [filtered .size () + 4 ];
@@ -85,14 +93,12 @@ public static void main(String[] args) {
8593 cmd [4 + i ] = filtered .get (i );
8694 }
8795 runCommand (cmd );
88-
8996 runCommand (new String []{"sudo" , "dnf" , "autoremove" , "-y" });
9097 }
9198
9299 // 4. Flatpak install apps
93100 if (confirm (scanner , "Install Flatpak apps?" )) {
94101 runCommand (new String []{"flatpak" , "remote-add" , "--if-not-exists" , flatpakRemoteName , flatpakRemoteUrl });
95-
96102 List <String > filtered = promptForExclusions (flatpakInstallPackages , scanner );
97103 String [] cmd = new String [filtered .size () + 4 ];
98104 cmd [0 ] = "flatpak" ;
@@ -124,7 +130,15 @@ public static void main(String[] args) {
124130 runCommand (new String []{"sudo" , "systemctl" , "enable" , "--now" , "cockpit.socket" });
125131 }
126132
127- System .out .println ("\n All actions completed." );
133+ System .out .println ("\n All actions completed. Goodbye." );
134+ }
135+
136+ static boolean isDryRun () {
137+ return dryRun ;
138+ }
139+
140+ static void setDryRun (boolean dryRun ) {
141+ PostInstallUpdater .dryRun = dryRun ;
128142 }
129143
130144 static boolean confirm (Scanner scanner , String prompt ) {
@@ -138,14 +152,19 @@ static ProcessBuilder createProcessBuilder(String[] cmd) {
138152 }
139153
140154 static int runCommand (String [] command ) {
155+ System .out .println ("Executing shell command: " + String .join (" " , command ));
156+ if (isDryRun ()) {
157+ System .out .println ("Dry-run: command not executed." );
158+ return 0 ;
159+ }
160+
141161 int exitCode = -1 ;
142162 try {
143163 ProcessBuilder pb = createProcessBuilder (command );
144164 pb .redirectErrorStream (true );
145- System .out .println ("Executing shell command: " + String .join (" " , command ));
146165 Process process = pb .start ();
147- System .out .println ("Command output:" );
148166 BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
167+ System .out .println ("Command output:" );
149168 String line ;
150169 while ((line = reader .readLine ()) != null ) {
151170 System .out .println (YELLOW + line + RESET );
@@ -185,8 +204,7 @@ static List<String> promptForExclusions(List<String> packages, Scanner scanner)
185204 String excludeInput = scanner .nextLine ().trim ();
186205 Set <Integer > excludeIndexes = new HashSet <>();
187206 if (excludeInput .matches ("^\\ d+$|^(\\ d+,)+\\ d$" )) {
188- String [] parts = excludeInput .split ("," );
189- for (String part : parts ) {
207+ for (String part : excludeInput .split ("," )) {
190208 try {
191209 int idx = Integer .parseInt (part .trim ());
192210 if (idx >= 1 && idx <= packages .size ()) {
@@ -195,7 +213,6 @@ static List<String> promptForExclusions(List<String> packages, Scanner scanner)
195213 } catch (NumberFormatException ignored ) {}
196214 }
197215 }
198-
199216 List <String > filtered = new ArrayList <>();
200217 for (int i = 0 ; i < packages .size (); i ++) {
201218 if (!excludeIndexes .contains (i )) {
0 commit comments