@@ -33,50 +33,58 @@ private CommandExecutor() {
33
33
// Empty constructor
34
34
}
35
35
36
- public static void searchGitReposAndExecuteCommand (String folderPath , String command ) {
37
- LOGGER .info ("Start searching git repositories in '{}' path and execute '{}' command" , folderPath , command );
36
+ public static File searchGitRepositoriesAndCreateScriptFile (String folderPath , String command ) {
38
37
File folder = new File (folderPath );
39
38
if (folder .exists () && folder .isDirectory ()) {
39
+ LOGGER .info ("Start searching git repositories in '{}' path." , folderPath );
40
40
List <File > repositoriesToRunCommand = new ArrayList <>();
41
41
searchGitRepositories (folder , repositoriesToRunCommand );
42
- File scriptFile = null ;
42
+ LOGGER . info ( "'{}' git repositories were found to run command." , repositoriesToRunCommand . size ()) ;
43
43
try {
44
- scriptFile = createAndFillTempFileWithCommand (command , repositoriesToRunCommand );
45
- executeCommand (scriptFile );
44
+ return createAndFillScriptFileWithCommand (command , repositoriesToRunCommand );
46
45
} catch (IOException e ) {
47
- LOGGER .error ("Something goes wrong with creation or executing of temp script file with git command." , e );
48
- } finally {
49
- if (scriptFile != null && scriptFile .exists ()) {
50
- if (scriptFile .delete ()) {
51
- LOGGER .info ("Temp file '{}' was removed." , scriptFile .getAbsolutePath ());
52
- } else {
53
- LOGGER .warn ("Temp script file '{}' with git command wasn't removed." , scriptFile .getAbsolutePath ());
54
- }
55
- }
46
+ LOGGER .error ("Something goes wrong with creation temp script file with command." , e );
56
47
}
57
48
} else {
58
- LOGGER .error ("Specified folder either doesn't exist or isn't a directory, running git command was skipped." );
49
+ LOGGER .error ("'{}' folder either doesn't exist or isn't a directory, running command was skipped." , folderPath );
50
+ }
51
+ return null ;
52
+ }
53
+
54
+ public static void executeScriptFileWithCommand (File scriptFile ) {
55
+ try {
56
+ LOGGER .info ("Start executing script file '{}'." , scriptFile .getAbsolutePath ());
57
+ executeScriptFile (scriptFile );
58
+ LOGGER .info ("Finish executing git command." );
59
+ } catch (IOException e ) {
60
+ LOGGER .error ("Something goes wrong with executing temp script file with command." , e );
61
+ } finally {
62
+ if (scriptFile != null && scriptFile .exists ()) {
63
+ if (scriptFile .delete ()) {
64
+ LOGGER .info ("Temp script file '{}' was removed." , scriptFile .getAbsolutePath ());
65
+ } else {
66
+ LOGGER .warn ("Temp script file '{}' with command wasn't removed." , scriptFile .getAbsolutePath ());
67
+ }
68
+ }
59
69
}
60
70
}
61
71
62
- private static void executeCommand (File scriptFile ) throws IOException {
72
+ private static void executeScriptFile (File scriptFile ) throws IOException {
63
73
Process powerShellProcess = Runtime .getRuntime ().exec (constructCmdCommand (scriptFile ));
64
74
String line ;
65
75
66
76
BufferedReader stdout = new BufferedReader (new InputStreamReader (powerShellProcess .getInputStream ()));
67
77
while ((line = stdout .readLine ()) != null ) {
68
- LOGGER .info ("Standard output of executed git command '{}'" , line );
78
+ LOGGER .info ("Standard output of executed command '{}'" , line );
69
79
}
70
80
stdout .close ();
71
81
72
82
BufferedReader stderr = new BufferedReader (new InputStreamReader (
73
83
powerShellProcess .getErrorStream ()));
74
84
while ((line = stderr .readLine ()) != null ) {
75
- LOGGER .error ("Error output of executed git command '{}'" , line );
85
+ LOGGER .error ("Error output of executed command '{}'" , line );
76
86
}
77
87
stderr .close ();
78
-
79
- LOGGER .info ("Finish executing git command." );
80
88
}
81
89
82
90
private static String [] constructCmdCommand (File scriptFile ) {
@@ -91,14 +99,14 @@ private static String[] constructCmdCommand(File scriptFile) {
91
99
return cmdCommand .toArray (new String []{});
92
100
}
93
101
94
- private static File createAndFillTempFileWithCommand (String gitCommand , List <File > repositoriesToRunCommand ) throws IOException {
102
+ private static File createAndFillScriptFileWithCommand (String gitCommand , List <File > repositoriesToRunCommand ) throws IOException {
95
103
File tempDir = new File ("temp" );
96
104
if (!tempDir .exists () && !tempDir .mkdirs ()) {
97
105
LOGGER .error ("Failed to create the temp directory '{}'." , "temp" );
98
106
}
99
107
100
108
File scriptFile = File .createTempFile ("script" , ".sh" , tempDir );
101
- LOGGER .info ("Temp file '{}' was created" , scriptFile .getAbsolutePath ());
109
+ LOGGER .info ("Temp script file '{}' was created" , scriptFile .getAbsolutePath ());
102
110
103
111
FileWriter writer = new FileWriter (scriptFile );
104
112
writer .write ("#!/bin/bash\n " );
@@ -110,7 +118,7 @@ private static File createAndFillTempFileWithCommand(String gitCommand, List<Fil
110
118
writer .write (gitCommand + "\n " );
111
119
}
112
120
writer .close ();
113
- LOGGER .info ("Git command '{}' was written to temp file." , gitCommand );
121
+ LOGGER .info ("Command '{}' was written to temp script file." , gitCommand );
114
122
return scriptFile ;
115
123
}
116
124
0 commit comments