main.c
changeset 43 d37d57c99cac
child 44 3d1a1517fa1d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/main.c	Sun Sep 30 00:18:20 2007 -0400
     1.3 @@ -0,0 +1,181 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <stddef.h>
     1.6 +#include <stdio.h>
     1.7 +#include <string.h>
     1.8 +#include <unistd.h>
     1.9 +
    1.10 +#include "razor.h"
    1.11 +
    1.12 +static const char *repo_filename = "system.repo";
    1.13 +static const char rawhide_repo_filename[] = "rawhide.repo";
    1.14 +
    1.15 +static int
    1.16 +command_list(int argc, const char *argv[])
    1.17 +{
    1.18 +	struct razor_set *set;
    1.19 +
    1.20 +	set = razor_set_open(repo_filename);
    1.21 +	razor_set_list(set);
    1.22 +	razor_set_destroy(set);
    1.23 +
    1.24 +	return 0;
    1.25 +}
    1.26 +
    1.27 +static int
    1.28 +command_list_requires(int argc, const char *argv[])
    1.29 +{
    1.30 +	struct razor_set *set;
    1.31 +
    1.32 +	set = razor_set_open(repo_filename);
    1.33 +	razor_set_list_requires(set, argv[2]);
    1.34 +	razor_set_destroy(set);
    1.35 +
    1.36 +	return 0;
    1.37 +}
    1.38 +
    1.39 +static int
    1.40 +command_list_provides(int argc, const char *argv[])
    1.41 +{
    1.42 +	struct razor_set *set;
    1.43 +
    1.44 +	set = razor_set_open(repo_filename);
    1.45 +	razor_set_list_provides(set, argv[2]);
    1.46 +	razor_set_destroy(set);
    1.47 +
    1.48 +	return 0;
    1.49 +}
    1.50 +
    1.51 +static int
    1.52 +command_what_requires(int argc, const char *argv[])
    1.53 +{
    1.54 +	struct razor_set *set;
    1.55 +
    1.56 +	set = razor_set_open(repo_filename);
    1.57 +	razor_set_list_requires_packages(set, argv[2], argv[3]);
    1.58 +	razor_set_destroy(set);
    1.59 +
    1.60 +	return 0;
    1.61 +}
    1.62 +
    1.63 +static int
    1.64 +command_what_provides(int argc, const char *argv[])
    1.65 +{
    1.66 +	struct razor_set *set;
    1.67 +
    1.68 +	set = razor_set_open(repo_filename);
    1.69 +	razor_set_list_provides_packages(set, argv[2], argv[3]);
    1.70 +	razor_set_destroy(set);
    1.71 +
    1.72 +	return 0;
    1.73 +}
    1.74 +
    1.75 +static int
    1.76 +command_import_yum(int argc, const char *argv[])
    1.77 +{
    1.78 +	struct razor_set *set;
    1.79 +
    1.80 +	set = razor_set_create_from_yum_filelist(STDIN_FILENO);
    1.81 +	if (set == NULL)
    1.82 +		return 1;
    1.83 +	razor_set_write(set, rawhide_repo_filename);
    1.84 +	razor_set_destroy(set);
    1.85 +	printf("wrote %s\n", rawhide_repo_filename);
    1.86 +
    1.87 +	return 0;
    1.88 +}
    1.89 +
    1.90 +static int
    1.91 +command_import_rpmdb(int argc, const char *argv[])
    1.92 +{
    1.93 +	struct razor_set *set;
    1.94 +
    1.95 +	set = razor_set_create_from_rpmdb();
    1.96 +	if (set == NULL)
    1.97 +		return 1;
    1.98 +	razor_set_write(set, repo_filename);
    1.99 +	razor_set_destroy(set);
   1.100 +	printf("wrote %s\n", repo_filename);
   1.101 +
   1.102 +	return 0;
   1.103 +}
   1.104 +
   1.105 +static int
   1.106 +command_validate(int argc, const char *argv[])
   1.107 +{
   1.108 +	struct razor_set *set;
   1.109 +
   1.110 +	set = razor_set_open(repo_filename);
   1.111 +	if (set == NULL)
   1.112 +		return 1;
   1.113 +	razor_set_list_unsatisfied(set);
   1.114 +	razor_set_destroy(set);
   1.115 +
   1.116 +	return 0;
   1.117 +}
   1.118 +
   1.119 +static int
   1.120 +command_update(int argc, const char *argv[])
   1.121 +{
   1.122 +	struct razor_set *set, *upstream;
   1.123 +
   1.124 +	set = razor_set_open(repo_filename);
   1.125 +	upstream = razor_set_open(rawhide_repo_filename);
   1.126 +	if (set == NULL || upstream == NULL)
   1.127 +		return 1;
   1.128 +	set = razor_set_update(set, upstream, argc - 2, argv + 2);
   1.129 +	razor_set_write(set, "system-updated.repo");
   1.130 +	razor_set_destroy(set);
   1.131 +	razor_set_destroy(upstream);
   1.132 +	printf("wrote system-updated.repo\n");
   1.133 +
   1.134 +	return 0;
   1.135 +}
   1.136 +
   1.137 +static struct {
   1.138 +	const char *name;
   1.139 +	const char *description;
   1.140 +	int (*func)(int argc, const char *argv[]);
   1.141 +} razor_commands[] = {
   1.142 +	{ "list", "list all packages", command_list },
   1.143 +	{ "list-requires", "list all requires or requires for the given package", command_list_requires },
   1.144 +	{ "list-provides", "list all provides or provides for the give package", command_list_provides },
   1.145 +	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   1.146 +	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   1.147 +	{ "import-yum", "import yum filelist.xml on stdin", command_import_yum },
   1.148 +	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   1.149 +	{ "validate", "validate a package set", command_validate },
   1.150 +	{ "update", "update all or specified packages", command_update }
   1.151 +};
   1.152 +
   1.153 +static int
   1.154 +usage(void)
   1.155 +{
   1.156 +	int i;
   1.157 +
   1.158 +	printf("usage:\n");
   1.159 +	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   1.160 +		printf("  %-20s%s\n",
   1.161 +		       razor_commands[i].name, razor_commands[i].description);
   1.162 +
   1.163 +	return 1;
   1.164 +}
   1.165 +
   1.166 +int
   1.167 +main(int argc, const char *argv[])
   1.168 +{
   1.169 +	char *repo;
   1.170 +	int i;
   1.171 +
   1.172 +	repo = getenv("RAZOR_REPO");
   1.173 +	if (repo != NULL)
   1.174 +		repo_filename = repo;
   1.175 +
   1.176 +	if (argc < 2)
   1.177 +		return usage();
   1.178 +
   1.179 +	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   1.180 +		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   1.181 +			return razor_commands[i].func(argc - 2, argv + 2);
   1.182 +
   1.183 +	return usage();
   1.184 +}