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
|
#include "config.h"
#include "log.h"
#include "run.h"
#include "util/array.h"
#include "util/fs.h"
#include "util/string.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#if HAVE_STDBOOL_H
#include <stdbool.h>
#endif
#if HAVE_STDINT_H
#include <stdint.h>
#endif
struct sk_options {
const char *conf_root;
bool dry_run;
const char *init_manifest;
unsigned int jobs;
uint8_t verbosity;
const char *target_host;
};
static const char *EXECNAME = PACKAGE; /* default */
/* help */
void sk_print_usage(FILE *outstream) {
fprintf(
outstream,
"usage: %s [-h] [-n] [-v] [-V] [-j JOBS] [-i MANIFEST] target_host\n",
EXECNAME);
}
void sk_print_version(FILE *outstream) {
fprintf(outstream, "%s %s\n", PACKAGE, VERSION);
}
void sk_print_help(FILE *outstream) {
/* usage */
sk_print_usage(outstream);
fprintf(outstream, "\n");
/* version */
sk_print_version(outstream);
fprintf(outstream, "\n");
/* positional arguments */
fprintf(
outstream,
"positional arguments:\n"
" target_host host to configure\n"
"\n");
/* optional arguments */
fprintf(
outstream,
"optional arguments:\n"
" -i path to the manifest to be used or '-' to read from stdin\n"
" -j number of jobs to execute objects in parallel\n"
" -h show this help and exit\n"
" -n dry run: do not execute generated code\n"
" -v increase verbosity (repeat for more verbosity)\n"
" -V output version information and exit\n"
"\n");
}
/* argument parsing */
static int sk_get_global_opts(
int argc, char *argv[], struct sk_options *options) {
/**
* Parse skonfig options.
* Will update the `options` struct.
*
* Options defined to print some information and exit the program will
* already do so in this function, in which case the function will never
* return.
*
* @returns true if no errors occurred during parsing, false otherwise.
*/
extern char *optarg;
extern int opterr, optind, optopt;
opterr = 0;
int c;
unsigned int posind = 1, errors = 0;
bool eoo = false; /* end of options */
while (optind < argc) {
while (!eoo && (c = getopt(argc, argv, ":i:j:hnvV")) != -1) {
switch (c) {
case 'i':
options->init_manifest = optarg;
break;
case 'j': {
unsigned int j;
if ('-' == optarg[0] || 1 > (j = atoi(optarg))) {
fprintf(stderr, "-j must be at least 1. Assuming -j 1.\n");
j = 1;
}
options->jobs = j;
break;
}
case 'h':
sk_print_help(stdout);
exit(EXIT_SUCCESS);
case 'n':
options->dry_run = true;
break;
case 'v':
options->verbosity++;
break;
case 'V':
sk_print_version(stdout);
exit(EXIT_SUCCESS);
case ':':
fprintf(stderr, "%s: option requires an argument -- %c\n", EXECNAME, optopt);
errors++;
break;
case '?':
default:
fprintf(stderr, "%s: illegal option -- %c\n", EXECNAME, optopt);
errors++;
break;
}
}
if (0 == strcmp("--", argv[optind-1])) {
eoo = true;
}
/* handle positional argument */
switch (posind) {
case 1:
/* target host */
options->target_host = argv[optind]; /* XXX: strcpy() needed? */
break;
default:
fprintf(stderr, "%s: unrecognized positional argument: %s\n",
EXECNAME, argv[optind]);
errors++;
break;
}
posind++; optind++;
};
if (errors) {
sk_print_usage(stderr);
return false;
}
return true;
}
/* config */
SK_NONNULL(1)
void sk_init_config(struct sk_config *config, const char *config_file) {
FILE *fh;
if (NULL != config_file) {
/* Parse config file (as given by option) */
#if DEBUG
printf("processing config file (opt): %s\n", config_file);
#endif
fh = fopen(config_file, "r");
if (fh) {
sk_config_parse_file(config, fh);
fclose(fh);
} else {
sk_error("invalid config file: %s", config_file);
exit(EXIT_FAILURE);
}
} else if ((config_file = sk_config_find())) {
/* found a config file in default location */
#if DEBUG
printf("processing config file (found): %s\n", config_file);
#endif
fh = fopen(config_file, "r");
if (NULL == fh) {
sk_error("could not open config file (%s): %s",
config_file, strerror(errno));
} else {
sk_config_parse_file(config, fh);
fclose(fh);
}
free((char *)config_file);
}
sk_config_parse_env(config);
}
int main(int argc, char *argv[]) {
int exit_status = EXIT_SUCCESS;
EXECNAME = argv[0];
#if DEBUG
printf("Debugging enabled.\n");
#endif
/* determine absolute path to ~/.skonfig */
char default_conf_root[PATH_MAX+1] = "";
(void)strncpy(default_conf_root, myhome(), arraylen(default_conf_root)-1);
strncat(default_conf_root, "/.skonfig",
(arraylen(default_conf_root) - strlen(default_conf_root)));
struct sk_options options = {
/* defaults */
.conf_root = (const char *)default_conf_root,
.dry_run = false,
.init_manifest = "manifest/init",
.jobs = 1,
.verbosity = 0,
.target_host = NULL
};
sk_get_global_opts(argc, argv, &options);
/* check if conf_root exists */
if (!is_dir(options.conf_root)) {
sk_error("cannot find configuration directory.");
perror(options.conf_root); /* is_dir sets errno */
/* TODO: Add link to Getting Started guide */
return EXIT_FAILURE;
}
/* Read in skonfig config */
struct sk_config *config = sk_config_alloc();
if (NULL == config) {
sk_error("could not allocate memory to store config data");
return EXIT_FAILURE;
}
sk_init_config(config, NULL);
/* update config from command line arguments */
free(config->global.init_manifest);
config->global.init_manifest = strdup(options.init_manifest);
config->global.jobs = options.jobs;
config->global.verbosity_level += options.verbosity;
if (config->global.verbosity_level > SK_LOG_MAX)
config->global.verbosity_level = SK_LOG_MAX;
sk_log_level_set(config->global.verbosity_level);
switch (config->global.coloured_output) {
case SK_COLOUR_NEVER:
sk_log_font_set(SK_LOG_MONOCHROME);
break;
case SK_COLOUR_ALWAYS:
sk_log_font_set(SK_LOG_COLOURED);
break;
case SK_COLOUR_AUTO:
if (isatty(fileno(stdout))) {
sk_log_font_set(SK_LOG_COLOURED);
} else {
sk_log_font_set(SK_LOG_MONOCHROME);
}
break;
}
#if DEBUG
sk_config_print(config, stdout); /* DEBUG */
#endif
sk_debug("conf dir: %s", options.conf_root);
sk_debug("target host: %s", options.target_host);
exit_status = sk_run_start(
options.target_host, options.dry_run, config);
sk_config_free(config);
return exit_status;
}
|