main.c
author Kristian H?gsberg <krh@redhat.com>
Sun Sep 30 00:18:20 2007 -0400 (2007-09-30)
changeset 43 d37d57c99cac
child 44 3d1a1517fa1d
permissions -rw-r--r--
Split command line interface out into main.c.
     1 #include <stdlib.h>
     2 #include <stddef.h>
     3 #include <stdio.h>
     4 #include <string.h>
     5 #include <unistd.h>
     6 
     7 #include "razor.h"
     8 
     9 static const char *repo_filename = "system.repo";
    10 static const char rawhide_repo_filename[] = "rawhide.repo";
    11 
    12 static int
    13 command_list(int argc, const char *argv[])
    14 {
    15 	struct razor_set *set;
    16 
    17 	set = razor_set_open(repo_filename);
    18 	razor_set_list(set);
    19 	razor_set_destroy(set);
    20 
    21 	return 0;
    22 }
    23 
    24 static int
    25 command_list_requires(int argc, const char *argv[])
    26 {
    27 	struct razor_set *set;
    28 
    29 	set = razor_set_open(repo_filename);
    30 	razor_set_list_requires(set, argv[2]);
    31 	razor_set_destroy(set);
    32 
    33 	return 0;
    34 }
    35 
    36 static int
    37 command_list_provides(int argc, const char *argv[])
    38 {
    39 	struct razor_set *set;
    40 
    41 	set = razor_set_open(repo_filename);
    42 	razor_set_list_provides(set, argv[2]);
    43 	razor_set_destroy(set);
    44 
    45 	return 0;
    46 }
    47 
    48 static int
    49 command_what_requires(int argc, const char *argv[])
    50 {
    51 	struct razor_set *set;
    52 
    53 	set = razor_set_open(repo_filename);
    54 	razor_set_list_requires_packages(set, argv[2], argv[3]);
    55 	razor_set_destroy(set);
    56 
    57 	return 0;
    58 }
    59 
    60 static int
    61 command_what_provides(int argc, const char *argv[])
    62 {
    63 	struct razor_set *set;
    64 
    65 	set = razor_set_open(repo_filename);
    66 	razor_set_list_provides_packages(set, argv[2], argv[3]);
    67 	razor_set_destroy(set);
    68 
    69 	return 0;
    70 }
    71 
    72 static int
    73 command_import_yum(int argc, const char *argv[])
    74 {
    75 	struct razor_set *set;
    76 
    77 	set = razor_set_create_from_yum_filelist(STDIN_FILENO);
    78 	if (set == NULL)
    79 		return 1;
    80 	razor_set_write(set, rawhide_repo_filename);
    81 	razor_set_destroy(set);
    82 	printf("wrote %s\n", rawhide_repo_filename);
    83 
    84 	return 0;
    85 }
    86 
    87 static int
    88 command_import_rpmdb(int argc, const char *argv[])
    89 {
    90 	struct razor_set *set;
    91 
    92 	set = razor_set_create_from_rpmdb();
    93 	if (set == NULL)
    94 		return 1;
    95 	razor_set_write(set, repo_filename);
    96 	razor_set_destroy(set);
    97 	printf("wrote %s\n", repo_filename);
    98 
    99 	return 0;
   100 }
   101 
   102 static int
   103 command_validate(int argc, const char *argv[])
   104 {
   105 	struct razor_set *set;
   106 
   107 	set = razor_set_open(repo_filename);
   108 	if (set == NULL)
   109 		return 1;
   110 	razor_set_list_unsatisfied(set);
   111 	razor_set_destroy(set);
   112 
   113 	return 0;
   114 }
   115 
   116 static int
   117 command_update(int argc, const char *argv[])
   118 {
   119 	struct razor_set *set, *upstream;
   120 
   121 	set = razor_set_open(repo_filename);
   122 	upstream = razor_set_open(rawhide_repo_filename);
   123 	if (set == NULL || upstream == NULL)
   124 		return 1;
   125 	set = razor_set_update(set, upstream, argc - 2, argv + 2);
   126 	razor_set_write(set, "system-updated.repo");
   127 	razor_set_destroy(set);
   128 	razor_set_destroy(upstream);
   129 	printf("wrote system-updated.repo\n");
   130 
   131 	return 0;
   132 }
   133 
   134 static struct {
   135 	const char *name;
   136 	const char *description;
   137 	int (*func)(int argc, const char *argv[]);
   138 } razor_commands[] = {
   139 	{ "list", "list all packages", command_list },
   140 	{ "list-requires", "list all requires or requires for the given package", command_list_requires },
   141 	{ "list-provides", "list all provides or provides for the give package", command_list_provides },
   142 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   143 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   144 	{ "import-yum", "import yum filelist.xml on stdin", command_import_yum },
   145 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   146 	{ "validate", "validate a package set", command_validate },
   147 	{ "update", "update all or specified packages", command_update }
   148 };
   149 
   150 static int
   151 usage(void)
   152 {
   153 	int i;
   154 
   155 	printf("usage:\n");
   156 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   157 		printf("  %-20s%s\n",
   158 		       razor_commands[i].name, razor_commands[i].description);
   159 
   160 	return 1;
   161 }
   162 
   163 int
   164 main(int argc, const char *argv[])
   165 {
   166 	char *repo;
   167 	int i;
   168 
   169 	repo = getenv("RAZOR_REPO");
   170 	if (repo != NULL)
   171 		repo_filename = repo;
   172 
   173 	if (argc < 2)
   174 		return usage();
   175 
   176 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   177 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   178 			return razor_commands[i].func(argc - 2, argv + 2);
   179 
   180 	return usage();
   181 }