Fortran · 2392 bytes Raw Blame History
1 module server_installer_module
2 use raw_mode_module, only: disable_raw_mode, enable_raw_mode
3 use terminal_io_module, only: terminal_clear_screen, terminal_show_cursor
4 implicit none
5 private
6
7 public :: run_install_command
8 public :: install_result_t
9
10 type :: install_result_t
11 logical :: success = .false.
12 integer :: exit_code = -1
13 character(len=1024) :: message = ''
14 end type install_result_t
15
16 contains
17
18 function run_install_command(command) result(result)
19 character(len=*), intent(in) :: command
20 type(install_result_t) :: result
21 integer :: exit_status, cmd_status
22 logical :: raw_disabled
23
24 result%success = .false.
25 result%exit_code = -1
26 result%message = ''
27
28 if (len_trim(command) == 0) then
29 result%message = 'No command specified'
30 return
31 end if
32
33 ! Must exit raw mode before running external commands
34 ! Otherwise child process inherits broken terminal state
35 raw_disabled = disable_raw_mode()
36
37 ! Show cursor and clear screen for command output
38 call terminal_show_cursor()
39 call terminal_clear_screen()
40
41 ! Execute the command with cmdstat to catch errors gracefully
42 ! Without cmdstat, invalid commands cause Fortran runtime errors
43 call execute_command_line(trim(command), wait=.true., exitstat=exit_status, cmdstat=cmd_status)
44
45 ! Re-enable raw mode for editor
46 raw_disabled = enable_raw_mode()
47
48 ! Check for command execution errors (cmdstat /= 0 means execute failed)
49 if (cmd_status /= 0) then
50 result%exit_code = cmd_status
51 result%message = 'Failed to execute command (cmdstat=' // trim(itoa(cmd_status)) // ')'
52 return
53 end if
54
55 result%exit_code = exit_status
56 result%success = (exit_status == 0)
57
58 if (result%success) then
59 result%message = 'Installation completed successfully'
60 else
61 write(result%message, '(A,I0)') 'Installation failed with exit code: ', exit_status
62 end if
63 end function run_install_command
64
65 ! Simple integer to string helper
66 function itoa(i) result(str)
67 integer, intent(in) :: i
68 character(len=20) :: str
69 write(str, '(I0)') i
70 end function itoa
71
72 end module server_installer_module
73