Print full version, not just pkg->version.
[oweals/opkg-lede.git] / tests / libopkg_test.c
1 #include <opkg.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <libgen.h>
6
7 pkg_t *find_pkg = NULL;
8
9
10 #define TEST_PACKAGE "aspell"
11
12 void
13 progress_callback (const opkg_progress_data_t *progress, void *data)
14 {
15   printf ("\r%s %3d%%\n", (char*) data, progress->percentage);
16   fflush (stdout);
17 }
18
19 static void list_pkg(pkg_t *pkg)
20 {
21   char *v = pkg_version_str_alloc(pkg);
22   printf ("%s - %s\n", pkg->name, v);
23   free(v);
24 }
25
26 void
27 package_list_installed_callback (pkg_t *pkg, void *data)
28 {
29   if (pkg->state_status == SS_INSTALLED)
30     list_pkg(pkg);
31 }
32
33 void
34 package_list_callback (pkg_t *pkg, void *data)
35 {
36   static int install_count = 0;
37   static int total_count = 0;
38
39   if (pkg->state_status == SS_INSTALLED)
40     install_count++;
41
42   total_count++;
43
44   printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
45   fflush (stdout);
46
47   if (!find_pkg)
48   {
49     /* store the first package to print out later */
50     find_pkg = pkg;
51   }
52 }
53
54 void
55 package_list_upgradable_callback (pkg_t *pkg, void *data)
56 {
57   list_pkg(pkg);
58 }
59
60 void
61 print_package (pkg_t *pkg)
62 {
63   char *v = pkg_version_str_alloc(pkg);
64   printf (
65       "Name:         %s\n"
66       "Version:      %s\n"
67       "Repository:   %s\n"
68       "Architecture: %s\n"
69       "Description:  %s\n"
70       "Tags:         %s\n"
71       "Size:         %ld\n"
72       "Status:       %d\n",
73       pkg->name,
74       v,
75       pkg->src->name,
76       pkg->architecture,
77       pkg->description,
78       pkg->tags? pkg->tags : "",
79       pkg->size,
80       pkg->state_status);
81   free(v);
82 }
83
84
85 void
86 opkg_test (void)
87 {
88   int err;
89   pkg_t *pkg;
90
91   err = opkg_update_package_lists (progress_callback, "Updating...");
92   printf ("\nopkg_update_package_lists returned %d\n", err);
93
94   opkg_list_packages (package_list_callback, NULL);
95   printf ("\n");
96
97   if (find_pkg)
98   {
99     printf ("Finding package \"%s\"\n", find_pkg->name);
100     pkg = opkg_find_package (find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->src->name);
101     if (pkg)
102     {
103       print_package (pkg);
104     }
105     else
106       printf ("Package \"%s\" not found!\n", find_pkg->name);
107   }
108   else
109     printf ("No package available to test find_package.\n");
110
111   err = opkg_install_package (TEST_PACKAGE, progress_callback, "Installing...");
112   printf ("\nopkg_install_package returned %d\n", err);
113
114   err = opkg_upgrade_package (TEST_PACKAGE, progress_callback, "Upgrading...");
115   printf ("\nopkg_upgrade_package returned %d\n", err);
116
117   err = opkg_remove_package (TEST_PACKAGE, progress_callback, "Removing...");
118   printf ("\nopkg_remove_package returned %d\n", err);
119
120   printf ("Listing upgradable packages...\n");
121   opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
122
123   err = opkg_upgrade_all (progress_callback, "Upgrading all...");
124   printf ("\nopkg_upgrade_all returned %d\n", err);
125
126 }
127
128 int
129 main (int argc, char **argv)
130 {
131   pkg_t *pkg;
132   int err;
133
134   if (argc < 2)
135   {
136     printf ("Usage: %s command\n"
137             "\nCommands:\n"
138             "\tupdate - Update package lists\n"
139             "\tfind [package] - Print details of the specified package\n"
140             "\tinstall [package] - Install the specified package\n"
141             "\tupgrade [package] - Upgrade the specified package\n"
142             "\tlist upgrades - List the available upgrades\n"
143             "\tlist all - List all available packages\n"
144             "\tlist installed - List all the installed packages\n"
145             "\tremove [package] - Remove the specified package\n"
146             "\trping - Reposiroties ping, check the accessibility of repositories\n"
147             "\ttest - Run test script\n"
148     , basename (argv[0]));
149     exit (0);
150   }
151
152   setenv("OFFLINE_ROOT", "/tmp", 0);
153  
154   if (opkg_new ()) {
155           printf("opkg_new() failed. This sucks.\n");
156           print_error_list();
157           return 1;
158   }
159
160   switch (argv[1][0])
161   {
162     case 'f':
163       pkg = opkg_find_package (argv[2], NULL, NULL, NULL);
164       if (pkg)
165       {
166         print_package (pkg);
167       }
168       else
169         printf ("Package \"%s\" not found!\n", find_pkg->name);
170       break;
171     case 'i':
172       err = opkg_install_package (argv[2], progress_callback, "Installing...");
173       printf ("\nopkg_install_package returned %d\n", err);
174       break;
175
176     case 'u':
177       if (argv[1][2] == 'd')
178       {
179         err = opkg_update_package_lists (progress_callback, "Updating...");
180         printf ("\nopkg_update_package_lists returned %d\n", err);
181         break;
182       }
183       else
184       {
185         if (argc < 3)
186         {
187           err = opkg_upgrade_all (progress_callback, "Upgrading all...");
188           printf ("\nopkg_upgrade_all returned %d\n", err);
189         }
190         else
191         {
192           err = opkg_upgrade_package (argv[2], progress_callback, "Upgrading...");
193           printf ("\nopkg_upgrade_package returned %d\n", err);
194         }
195       }
196       break;
197
198     case 'l':
199       if (argc < 3)
200       {
201         printf ("Please specify one either all, installed or upgrades\n");
202       }
203       else
204       {
205         switch (argv[2][0])
206         {
207           case 'u':
208             printf ("Listing upgradable packages...\n");
209             opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
210             break;
211           case 'a':
212             printf ("Listing all packages...\n");
213             opkg_list_packages (package_list_callback, NULL);
214             printf ("\n");
215             break;
216           case 'i':
217             printf ("Listing installed packages...\n");
218             opkg_list_packages (package_list_installed_callback, NULL);
219             break;
220           default:
221             printf ("Unknown list option \"%s\"\n", argv[2]);
222         }
223       }
224       break;
225           
226     case 'r':
227       if (argv[1][1] == 'e')
228       {
229         err = opkg_remove_package (argv[2], progress_callback, "Removing...");
230         printf ("\nopkg_remove_package returned %d\n", err);
231         break;
232       }else if (argv[1][1] == 'p')
233       {
234         err = opkg_repository_accessibility_check();
235         printf("\nopkg_repository_accessibility_check returned (%d)\n", err);
236         break;
237       }
238
239     default:
240       printf ("Unknown command \"%s\"\n", argv[1]);
241   }
242
243
244   opkg_free ();
245
246   return 0;
247 }