@@ -49,8 +49,11 @@ get_help(Command) ->
49
49
% %% Spawning and gen_server implementation
50
50
% %%===================================================================
51
51
52
+ -spec start_link () ->
53
+ {ok , pid ()} | {error , {already_started , pid ()}} | {error , term ()}.
52
54
start_link () ->
53
- gen_server :start_link ({local , ? SERVER }, ? MODULE , [], []).
55
+ gen_server :start_link ({local , ? SERVER }, ? MODULE , [],
56
+ [{hibernate_after , timer :seconds (160 )}]).
54
57
55
58
-spec init (term ()) -> {ok , state ()}.
56
59
init (_ ) ->
@@ -65,9 +68,10 @@ handle_call({run, Command, Args}, From, State = #state{}) ->
65
68
[] ->
66
69
{reply , {error , command_not_found }, State };
67
70
[{_ , ArgsDef , Fun , _Description }] ->
68
- CommandPid = run_command (Command , Args , ArgsDef , From , Fun ),
71
+ CommandPid = spawn_run_command (Command , Args , ArgsDef , From , Fun ),
69
72
RunningCommands = State # state .running_commands ,
70
- {noreply , State # state {running_commands = [{CommandPid , From } | RunningCommands ]}}
73
+ {noreply ,
74
+ State # state {running_commands = [{CommandPid , From } | RunningCommands ]}}
71
75
end ;
72
76
handle_call ({get_help , Command }, _From , State = # state {}) ->
73
77
Help = do_get_help (Command ),
@@ -84,37 +88,39 @@ handle_cast(_Request, State = #state{}) ->
84
88
-spec handle_info (Info :: term (), State :: term ()) ->
85
89
{noreply , NewState :: term ()}.
86
90
handle_info ({'EXIT' , Pid , normal }, # state {running_commands = Rc } = State ) ->
87
- {noreply , State # state {running_commands = lists :delete (Pid , Rc )}};
91
+ {noreply , State # state {running_commands = proplists :delete (Pid , Rc )}};
88
92
handle_info ({'EXIT' , Pid , Reason }, # state {running_commands = Rc } = State ) ->
89
93
case lists :keyfind (Pid , 1 , Rc ) of
90
94
false ->
91
95
ok ;
92
96
{_ , From } ->
93
97
gen_server :reply (From , {error , Reason })
94
98
end ,
95
- {noreply , State # state {running_commands = lists :delete (Pid , Rc )}}.
99
+ {noreply , State # state {running_commands = proplists :delete (Pid , Rc )}}.
96
100
97
101
% %%===================================================================
98
102
% %% Internal functions
99
103
% %%===================================================================
100
-
101
- run_command (Command , Args , ArgsDef , From , Fun ) ->
104
+ spawn_run_command (Command , Args , ArgsDef , From , Fun ) ->
102
105
spawn_link (fun () ->
103
- Result =
104
- case convert (ArgsDef , Args ) of
105
- {ok , NewArgs } ->
106
- case is_not_in_arg_def_but_set (ArgsDef , Args , " help" ) of
107
- true ->
108
- get_help (Command );
109
- _ ->
110
- MissingArguments = get_missing_arguments (ArgsDef , NewArgs ),
111
- evaluate_argument_check (MissingArguments , fun () -> execute_fun (Fun , NewArgs ) end )
112
- end ;
113
- Else ->
114
- Else
115
- end ,
116
- gen_server :reply (From , Result )
117
- end ).
106
+ CommandResult = run_command (Command , Args , ArgsDef , Fun ),
107
+ gen_server :reply (From , CommandResult )
108
+ end ).
109
+
110
+ run_command (Command , Args , ArgsDef , Fun ) ->
111
+ case convert (ArgsDef , Args ) of
112
+ {ok , NewArgs } ->
113
+ case is_not_in_arg_def_but_set (ArgsDef , Args , " help" ) of
114
+ true ->
115
+ get_help (Command );
116
+ _ ->
117
+ MissingArguments = get_missing_arguments (ArgsDef , NewArgs ),
118
+ evaluate_argument_check (MissingArguments ,
119
+ fun () -> execute_fun (Fun , NewArgs ) end )
120
+ end ;
121
+ Else ->
122
+ Else
123
+ end .
118
124
119
125
execute_fun (Fun , NewArgs ) ->
120
126
try
@@ -262,22 +268,22 @@ format_command({Commands, Desc, Args}) ->
262
268
[format_arg (Arg ) || Arg <- Args ]];
263
269
format_command ({Commands , Desc }) ->
264
270
CommandStr = string :pad (string :join (Commands , " " ), 32 ),
265
- cli_console_formatter :text (" ~s ~s " , [CommandStr , Desc ]).
271
+ cli_console_formatter :text (" ~ts ~ts " , [CommandStr , Desc ]).
266
272
267
273
format_arg (# argument {name = Name , optional = Optional ,
268
274
default = Default , description = Desc }) ->
269
275
270
276
CommandStr = string :pad (Name , 30 ),
271
277
ArgData = get_default_string (Default , get_optional_string (Optional , " " )),
272
- cli_console_formatter :text (" -~s ~s ~n~s~s " ,
278
+ cli_console_formatter :text (" -~ts ~ts ~n~ts~ts " ,
273
279
[CommandStr , Desc , lists :duplicate (32 , " " ), ArgData ]).
274
280
275
281
get_default_string (undefined , Acc ) ->
276
282
Acc ;
277
283
get_default_string (Default , Acc ) when is_integer (Default ) ->
278
- Acc ++ " Default value: " ++ io_lib :format (" ~s " , [integer_to_list (Default )]);
284
+ Acc ++ " Default value: " ++ io_lib :format (" ~ts " , [integer_to_list (Default )]);
279
285
get_default_string (Default , Acc ) ->
280
- Acc ++ " Default value: " ++ io_lib :format (" ~s " , [Default ]).
286
+ Acc ++ " Default value: " ++ io_lib :format (" ~ts " , [Default ]).
281
287
282
288
get_optional_string (true , Acc ) ->
283
289
Acc ;
0 commit comments