Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
297 lines (265 loc) · 9.7 KB
/
Copy pathmain.cpp
File metadata and controls
297 lines (265 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "base.h"
#include "cli.h"
#include "configs.h"
#include "files.h"
#include "help_text.h"
#define QUICK_SCRIPT_VERSION "1.1.0"
static void
print_version()
{
fprintf(stdout, "%s\n", QUICK_SCRIPT_VERSION);
}
static void
print_usage(const char* exec_name)
{
printf("Usage:\n");
printf("%s [options] <action name> [--help] [<action arguments>, ...]\n", exec_name);
printf("%s [options] --template <template string> [<action arguments>, ...]\n", exec_name);
printf(" --help to see more help.\n");
printf(" --actions to see a list of available actions.\n");
}
static void
print_available_actions(StringList* config_filepaths)
{
StringList* config_path_item = config_filepaths;
StringList* seen = 0;
bool did_print_header = false;
while (config_path_item) {
String config_path = config_path_item->string;
StringList* action_names = 0;
if (config_get_action_names(config_path, &action_names)) {
if (!did_print_header) {
fprintf(stdout, "Available actions:\n");
did_print_header = true;
}
StringList* action_name_item = action_names;
while (action_name_item) {
String action_name = action_name_item->string;
bool shadowed = string_list_contains(seen, action_name);
if (!shadowed) {
fprintf(stdout, " - %-35s (%s)\n", action_name, config_path);
seen = string_list_add_front_dup(seen, action_name);
}
action_name_item = action_name_item->next;
}
string_list_free(action_names);
}
config_path_item = config_path_item->next;
}
string_list_free(seen);
}
/**
* Break a filepath at the last forward slash ('/').
* If there are no '/' characters in the provided filepath, a '.' character is returned.
* The trailing '/' is not included after calling this function.
*/
static void dirname(String filepath)
{
u32 offset = string_len(filepath);
while (offset && filepath[offset] != '/')
offset--;
if (!offset) {
filepath[0] = '.';
filepath[1] = '\0';
} else {
filepath[offset] = '\0';
}
}
static void
exec_with_options(CommandLineOptions options, String shell_command, char* cwd)
{
String cmd = string_new("cd ");
cmd = string_append(cmd, cwd ? cwd : ".");
cmd = string_append(cmd, "; QS_RUN_DIR=");
char rundir[PATH_MAX] = { 0 };
if (realpath(".", rundir)) {
cmd = string_append(cmd, rundir);
} else {
fprintf(stderr, "Error: Failed to resolve current directory\n");
string_free(cmd);
return;
}
cmd = string_append(cmd, "; ");
cmd = string_append(cmd, shell_command);
if (options.dry_run) {
fprintf(stdout, "Would run: %s\n", cmd);
} else {
if (options.verbose) {
fprintf(stdout, "Running: %s\n", cmd);
}
system(cmd);
}
string_free(cmd);
}
static void
populate_options_with_default_config_files(CommandLineOptions* options)
{
StringList* default_config_files = resolve_default_config_files();
if (options->config_files) {
// Add the default configs files after the user provided ones
StringList* end = options->config_files;
while (end && end->next)
end = end->next;
end->next = default_config_files;
} else {
// Set the config files to just the default ones
options->config_files = default_config_files;
}
if (options->verbose && options->config_files) {
fprintf(stdout, "Searching the following configuration files:\n");
StringList* node = options->config_files;
while (node) {
fprintf(stdout, " - %s\n", node->string);
node = node->next;
}
}
}
enum ErrorType {
ErrorType_None = 0,
ErrorType_Error = 1,
ErrorType_User = 2,
};
static ErrorType
process_options(CommandLineOptions* options, const char* program_name)
{
// Handle no argument invokation
if (options->no_arguments_given) {
print_usage(program_name);
return ErrorType_User;
}
// Handle --help
if (options->print_help) {
print_help();
return ErrorType_None;
}
// Handle --version
if (options->print_version) {
print_version();
return ErrorType_None;
}
if (options->print_available_actions) {
populate_options_with_default_config_files(options);
print_available_actions(options->config_files);
return ErrorType_None;
}
if (options->action_name && options->action_template) {
fprintf(stdout, "Error: Must provide either an action name or a template string (--template), not both.\n");
return ErrorType_User;
}
if (!options->action_name && !options->action_template) {
fprintf(stdout, "Error: Must provide either an action name or a --template\n\n");
print_usage(program_name);
return ErrorType_User;
}
if (options->action_template) {
if (options->verbose) {
fprintf(stdout, "Resolved template: %s\n", options->action_template);
}
String command = template_render(options->action_template, options->variables);
if (command) {
exec_with_options(*options, command, 0);
string_free(command);
return ErrorType_None;
}
return ErrorType_User;
}
if (options->action_name) {
// User gave an action name. Dig into the config files and try to resolve it.
populate_options_with_default_config_files(options);
// Search through the configuration files for an action with the given name in order to find
// the template.
char* action_name = options->action_name;
char* config_path = 0;
// Loop through the configuration files and look for the first declaration of the
// sought action.
ResolvedTemplateResult template_res;
StringList* config_file = options->config_files;
while (config_file) {
config_path = config_file->string;
template_res = resolve_template_for_action(config_path, action_name);
if (template_res.parse_error) {
return ErrorType_Error;
} else if (template_res.action_template) {
// Found the action template, stop looking
break;
}
config_file = config_file->next;
}
if (template_res.action_template) {
// Successfully resolved a valid template for the action
if (options->verbose) {
fprintf(stdout, "Resolved template: %s\nFrom: %s\n", template_res.action_template, config_path ? config_path : "--template");
if (template_res.vars) {
fprintf(stdout, "with predefined variable values:\n");
VarList* vars = template_res.vars;
while (vars) {
fprintf(stdout, " - ${%s} => %s\n", vars->name, vars->value);
vars = vars->next;
}
}
}
ErrorType error;
if (options->print_action_help) {
String usage = template_generate_usage(template_res.action_template, options->action_name);
if (usage) {
fprintf(stdout, "%s", usage);
string_free(usage);
error = ErrorType_None;
} else {
fprintf(stderr, "Invalid action template: %s\n", template_res.action_template);
error = ErrorType_Error;
}
} else {
// Set the config path for the template render function to use when setting the
// command QS_RUN_DIR varibable
if (config_path)
dirname(config_path);
// Merge the user defined variables into the config file provided variables
VarList* merged_vars = template_merge(template_res.vars, options->variables);
String command = template_render(template_res.action_template, merged_vars);
template_free(merged_vars);
if (command) {
exec_with_options(*options, command, config_path);
string_free(command);
error = ErrorType_None;
} else {
fprintf(stderr, "Invalid action template: %s\n", template_res.action_template);
error = ErrorType_Error;
}
}
// Free the template result resources
string_free(template_res.action_template);
template_free(template_res.vars);
return error;
} else {
// Failed to find an template for the action
fprintf(stdout, "Could not find action with name: %s\n", action_name);
return ErrorType_User;
}
}
assert(false); // All possible combinations should have been exhausted at this point
}
int main(int argc, char** argv)
{
// parse command line arguments
CommandLineOptions options = {};
ParseResult parse_result = parse_cli_args(&options, argc, argv);
switch (parse_result) {
case ParseResult_Error:
free_cli_options_resources(options);
exit(ErrorType_Error);
case ParseResult_Invalid:
free_cli_options_resources(options);
exit(ErrorType_User);
case ParseResult_Ok:
/* fallthrough */;
}
ErrorType error = process_options(&options, argv[0]);
free_cli_options_resources(options);
exit(error);
}