/ src / repositories.c
repositories.c
 1  /*-
 2   * Copyright(c) 2024 Baptiste Daroussin <bapt@FreeBSD.org>
 3   *
 4   * SPDX-License-Identifier: BSD-2-Clause
 5   */
 6  
 7  #include <getopt.h>
 8  #include <stdio.h>
 9  #include <stdbool.h>
10  #include <unistd.h>
11  
12  #include <pkg.h>
13  
14  #include "pkgcli.h"
15  
16  void
17  usage_repositories(void)
18  {
19          fprintf(stderr, "Usage: pkg repositories [-edl] [repository]\n\n");
20  }
21  
22  
23  typedef enum {
24  	REPO_SHOW_ALL = 0,
25  	REPO_SHOW_ENABLED = 1U << 0,
26  	REPO_SHOW_DISABLED = 1U << 1,
27  } repo_show_t;
28  
29  int
30  exec_repositories(int argc, char **argv)
31  {
32  	const char *r = NULL;
33  	struct pkg_repo *repo = NULL;
34  	bool list_only = false;
35  	repo_show_t rs = REPO_SHOW_ALL;
36  	int ch;
37  
38  	struct option longopts[] = {
39  		{ "list",	no_argument,	NULL,	'l' },
40  		{ "enabled",	no_argument,	NULL,	'e' },
41  		{ "disabled",	no_argument,	NULL,	'd' },
42  		{ NULL,		0,		NULL,	0   },
43  	};
44  
45  	while ((ch = getopt_long(argc, argv, "+led", longopts, NULL)) != -1) {
46                  switch (ch) {
47  		case 'l':
48  			list_only = true;
49  			break;
50  		case 'e':
51  			rs |= REPO_SHOW_ENABLED;
52  			break;
53  		case 'd':
54  			rs |= REPO_SHOW_DISABLED;
55  			break;
56  		default:
57  			usage_repositories();
58  			return (EXIT_FAILURE);
59  		}
60  	}
61  
62  	if (rs == REPO_SHOW_ALL)
63  		rs |= REPO_SHOW_DISABLED|REPO_SHOW_ENABLED;
64  
65  	argc -= optind;
66  	argv += optind;
67  
68  	if (argc == 1)
69  		r = argv[0];
70  
71  	while (pkg_repos(&repo) == EPKG_OK) {
72  		if (r && !STREQ(r, pkg_repo_name(repo)))
73  			continue;
74  		if (pkg_repo_enabled(repo)) {
75  			if ((rs & REPO_SHOW_ENABLED) == 0)
76  				continue;
77  		} else {
78  			if ((rs & REPO_SHOW_DISABLED) == 0)
79  				continue;
80  		}
81  		if (list_only) {
82  			printf("%s\n", pkg_repo_name(repo));
83  			continue;
84  		}
85  		print_repository(repo, false);
86  	}
87  
88  
89  	return (EXIT_SUCCESS);
90  }