aboutsummaryrefslogtreecommitdiff
path: root/snag/main.c
blob: 4f6aa44c614d12ae82a7a4856834b43d349e45d1 (plain)
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
#include <stdio.h>

#include "util.h"
#include "libargs/args.h"
#include "pkg_db.h"

int cmd_install(char* cmd_name, ArgParser* parser);

int main(int argc, char* argv[]) {

	// create argument parser
    ArgParser* parser = ap_new_parser();
	if (parser == NULL) error_out("failed to initialize cli parsing");
    ap_set_helptext(parser, "Usage: snag <command>");
    ap_set_version(parser, "0.0");

	// add install subcommand
	ArgParser* install = ap_new_cmd(parser, "install inst i");
	if (install == NULL) error_out("failed to initialize install cli subcommand");
    ap_set_helptext(install, "Usage: snag install [packages]");
    ap_set_cmd_callback(install, cmd_install);

	// parse the arguments (automatically executes subcommands)
    if (!ap_parse(parser, argc, argv)) error_out("failed to parse arguments");

	// if no subcommand was specified, parse and execute install as the root command
	if (!ap_found_cmd(parser)) {
		if (!ap_parse(install, argc, argv)) error_out("failed to parse arguments");
		cmd_install("install", install);
	}

    ap_free(parser);
	return 0;
}

int cmd_install(char* cmd_name, ArgParser* parser) {

	// install each package specified
	for (int i = 0; i < ap_count_args(parser); i++) {
		if (i != 0) printf("\n");

		// load package description
		char* pkgid = ap_get_arg_at_index(parser, i);
		package_info_t info;
		if (load_package_info(pkgid, &info)) {
			printf("Installing %s - %s\n", info.attrs.name, info.attrs.desc);
			run_package_install(&info);
		}
	}

	return 0;
}