25
25
register_command /4 ,
26
26
register_config /2 ,
27
27
register_formatter /2 ,
28
+ register_writer /2 ,
28
29
register_config_whitelist /1 ,
29
30
register_usage /2 ,
30
31
run /1 ,
@@ -56,6 +57,11 @@ register_config(Key, Callback) ->
56
57
register_formatter (Key , Callback ) ->
57
58
clique_config :register_formatter (Key , Callback ).
58
59
60
+ % % @doc Register a module for writing output in a specific format
61
+ -spec register_writer (string (), module ()) -> true .
62
+ register_writer (Name , Module ) ->
63
+ clique_writer :register (Name , Module ).
64
+
59
65
% % @doc Register a list of configuration variables that are settable.
60
66
% % Clique disallows setting of all config variables by default. They must be in
61
67
% % whitelist to be settable.
@@ -75,43 +81,29 @@ register_usage(Cmd, Usage) ->
75
81
clique_usage :register (Cmd , Usage ).
76
82
77
83
% % @doc Take a list of status types and generate console output
78
- -spec print (err () | clique_status :status (), [string ()]) -> ok .
79
- print (usage , Cmd ) ->
84
+ -spec print (err () | clique_status :status () | {clique_status :status (), string ()}, [string ()]) -> ok .
85
+ print ({error , _ } = E , Cmd ) ->
86
+ print (E , Cmd , " human" );
87
+ print ({Status , Format }, Cmd ) ->
88
+ print (Status , Cmd , Format );
89
+ print (Status , Cmd ) ->
90
+ print (Status , Cmd , " human" ).
91
+
92
+ -spec print (usage | err () | clique_status :status (), [string ()], string ()) -> ok .
93
+ print (usage , Cmd , _Format ) ->
80
94
clique_usage :print (Cmd );
81
- print ({error , _ }= E , Cmd ) ->
95
+ print ({error , _ }= E , Cmd , Format ) ->
82
96
Alert = clique_error :format (hd (Cmd ), E ),
83
- print (Alert , Cmd );
84
- print (Status , _Cmd ) ->
85
- Output = clique_human_writer :write (Status ),
86
- io :format (" ~ts " , [Output ]),
87
- ok .
97
+ print (Alert , Cmd , Format );
98
+ print (Status , _Cmd , Format ) ->
99
+ Output = clique_writer :write (Status , Format ),
100
+ io :format (" ~ts " , [Output ]).
88
101
89
102
% % @doc Run a config operation or command
90
103
-spec run ([string ()]) -> ok | {error , term ()}.
91
- run ([_Script , " set" | Args ] = Cmd ) ->
92
- print (clique_config :set (Args ), Cmd );
93
- run ([_Script , " show" | Args ] = Cmd ) ->
94
- print (clique_config :show (Args ), Cmd );
95
- run ([_Script , " describe" | Args ] = Cmd ) ->
96
- print (clique_config :describe (Args ), Cmd );
97
- run (Cmd0 ) ->
98
- case is_help (Cmd0 ) of
99
- {ok , Cmd } ->
100
- clique_usage :print (Cmd );
101
- _ ->
102
- M0 = clique_command :match (Cmd0 ),
103
- M1 = clique_parser :parse (M0 ),
104
- M2 = clique_parser :validate (M1 ),
105
- print (clique_command :run (M2 ), Cmd0 )
106
- end .
107
-
108
- % % @doc Help flags always comes at the end of the command
109
- -spec is_help (iolist ()) -> {ok , iolist ()} | false .
110
- is_help (Str ) ->
111
- [H | T ] = lists :reverse (Str ),
112
- case H =:= " --help" orelse H =:= " -h" of
113
- true ->
114
- {ok , lists :reverse (T )};
115
- false ->
116
- false
117
- end .
104
+ run (Cmd ) ->
105
+ M0 = clique_command :match (Cmd ),
106
+ M1 = clique_parser :parse (M0 ),
107
+ M2 = clique_parser :extract_global_flags (M1 ),
108
+ M3 = clique_parser :validate (M2 ),
109
+ print (clique_command :run (M3 ), Cmd ).
0 commit comments