summaryrefslogtreecommitdiff
path: root/src/main.c
blob: db11d5a822b1e9e63891d277fcbeccbbca4f34d3 (plain) (blame)
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
#include <getopt.h>
#include <string.h>
#include <unistd.h>

#include <curl/curl.h>

#include "config.h"
#include "git.h"
#include "github.h"
#include "github_types.h"
#include "precheck.h"

static int load_config(int argc, char **argv, struct config **cfg_out)
{
	int opt, opt_idx = 0;
	size_t i;
	char *cfg_path = NULL;

	static struct option long_options[] = {
			{"config", required_argument, 0, 'c'},
			{"help", no_argument, 0, 'h'},
			{0, 0, 0, 0}};

	while ((opt = getopt_long(argc, argv, "C:c:h", long_options,
				  &opt_idx)) != -1) {
		switch (opt) {
		case 'C':
		case 'c':
			cfg_path = optarg;
			break;
		case 'h':
			fprintf(stderr,
				"Usage: %s [--config <file>] [--help]\n",
				argv[0]);
			return 0;
		default:
			fprintf(stderr, "Unknown option: %c\n", opt);
			fprintf(stderr,
				"Usage: %s [--config <file>] [--help]\n",
				argv[0]);
			return 1;
		}
	}

	// Config file given, use it
	if (cfg_path) {
		*cfg_out = config_read(cfg_path);
		return *cfg_out == NULL;
	}

	// No config file given, try the default locations
	for (i = 0; config_locations[i]; i++) {
		fprintf(stderr, "Trying config file: %s\n",
			config_locations[i]);
		*cfg_out = config_read(config_locations[i]);
		if (*cfg_out) {
			fprintf(stderr, "Using config file: %s\n",
				config_locations[i]);
			return 0;
		}
	}
	fprintf(stderr, "Failed to read config file\n");
	return 1;
}

int main(int argc, char **argv)
{
	curl_global_init(CURL_GLOBAL_DEFAULT);

	struct config *cfg = NULL;
	const int ret = load_config(argc, argv, &cfg);
	if (ret != 0 || !cfg)
		return ret;

	if (precheck_self(cfg)) {
		fprintf(stderr, "Precheck failed\n");
		config_free(cfg);
		return 1;
	}

	const struct github_ctx ctx = {
			.endpoint = cfg->endpoint,
			.token = cfg->token,
			.user_agent = cfg->user_agent,
	};
	github_client *client = github_client_new(ctx);
	if (!client) {
		fprintf(stderr, "Failed to create GitHub client\n");
		return 1;
	}

	// Get identity
	char *login = github_client_identity(client);

	struct list_repos_res res;
	char *end_cursor = NULL;
	int status = 0;
	do {
		if (github_client_list_user_repos(client, cfg->owner,
						  end_cursor, &res))
			return 1;

		for (size_t i = 0; i < res.repos_len; i++) {
			printf("Repo: %s\t%s\n", res.repos[i].name,
			       res.repos[i].url);

			const struct repo_ctx repo = {
					.cfg = cfg,
					.name = res.repos[i].name,
					.url = res.repos[i].url,
					.username = login,
			};
			if (git_mirror_repo(&repo) != 0) {
				fprintf(stderr, "Failed to mirror repo\n");
				status = 1;
				break;
			}
		}

		free(end_cursor);
		end_cursor = strdup(res.end_cursor);

		list_repos_res_free(res);
	} while (res.has_next_page);

	free(end_cursor);
	github_client_free(client);
	free(login);
	config_free(cfg);
	curl_global_cleanup();
	return status;
}