package: Add "info" command

It allows to print individual information from the package meta data.
This commit is contained in:
Ingo Weinhold 2013-10-09 03:43:08 +02:00
parent 40c9cb7b47
commit 16c8373026
5 changed files with 156 additions and 2 deletions

View File

@ -7,11 +7,11 @@ BinCommand package :
command_create.cpp
command_dump.cpp
command_extract.cpp
command_info.cpp
command_list.cpp
package.cpp
PackageWriterListener.cpp
PackageWritingUtils.cpp
:
package be
$(TARGET_LIBSUPC++)

View File

@ -0,0 +1,136 @@
/*
* Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <Entry.h>
#include <package/PackageInfo.h>
#include "package.h"
using namespace BPackageKit;
int
command_info(int argc, const char* const* argv)
{
const char* format = "name: %name% version: %version%\n";
while (true) {
static struct option sLongOptions[] = {
{ "format", required_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
opterr = 0; // don't print errors
int c = getopt_long(argc, (char**)argv, "f:h", sLongOptions, NULL);
if (c == -1)
break;
switch (c) {
case 'f':
format = optarg;
break;
default:
print_usage_and_exit(true);
break;
}
}
// One argument should remain -- the package file name.
if (optind + 1 != argc)
print_usage_and_exit(true);
const char* fileName = argv[optind++];
// Read the package info from the package file. If it doesn't look like a
// package file, assume it is a package info file.
BPackageInfo info;
if (BString(fileName).EndsWith(".hpkg")) {
status_t error = info.ReadFromPackageFile(fileName);
if (error != B_OK) {
fprintf(stderr, "Error: Failed to read package file \"%s\": %s\n",
fileName, strerror(error));
return 1;
}
} else {
status_t error = info.ReadFromConfigFile(BEntry(fileName));
if (error != B_OK) {
fprintf(stderr, "Error: Failed to read package info file \"%s\": "
"%s\n", fileName, strerror(error));
return 1;
}
}
// parse format string and produce output
BString output;
while (*format != '\0') {
char c = *format++;
switch (c) {
case '%':
{
const char* start = format;
while (*format != '\0' && *format != '%')
format++;
if (*format != '%') {
fprintf(stderr, "Error: Unexpected at end of the format "
"string. Expected \"%%\".\n");
return 1;
}
if (format == start) {
output << '%';
} else {
BString variable(start, format - start);
if (variable == "fileName") {
output << info.CanonicalFileName();
} else if (variable == "name") {
output << info.Name();
} else if (variable == "version") {
output << info.Version().ToString();
} else {
fprintf(stderr, "Error: Unsupported placeholder \"%s\" "
"in format string.\n", variable.String());
return 1;
}
}
format++;
break;
}
case '\\':
c = *format++;
if (c == '\0') {
fprintf(stderr, "Error: \"\\\" at the end of the format "
"string.\n");
return 1;
}
switch (c) {
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
}
// fall through
default:
output << c;
break;
}
}
fputs(output.String(), stdout);
return 0;
}

View File

@ -76,6 +76,19 @@ static const char* kUsage =
" of the archive.\n"
" -i <info> - Extract the .PackageInfo file to <info> instead.\n"
"\n"
" info [ <options> ] <package>\n"
" Prints individual meta information of package file <package>.\n"
"\n"
" -f <format>, --format <format>\n"
" - Print the given format string, performing the following\n"
" replacements:\n"
" %fileName% - the package's canonical file name\n"
" %name% - the package name\n"
" %version% - the package version\n"
" %% - %\n"
" \n - new line\n"
" \t - tab\n"
"\n"
" list [ <options> ] <package>\n"
" Lists the contents of package file <package>.\n"
"\n"
@ -118,6 +131,9 @@ main(int argc, const char* const* argv)
if (strcmp(command, "list") == 0)
return command_list(argc - 1, argv + 1);
if (strcmp(command, "info") == 0)
return command_info(argc - 1, argv + 1);
if (strcmp(command, "help") == 0)
print_usage_and_exit(false);
else

View File

@ -1,5 +1,5 @@
/*
* Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#ifndef PACKAGE_H
@ -12,6 +12,7 @@ int command_add(int argc, const char* const* argv);
int command_create(int argc, const char* const* argv);
int command_dump(int argc, const char* const* argv);
int command_extract(int argc, const char* const* argv);
int command_info(int argc, const char* const* argv);
int command_list(int argc, const char* const* argv);

View File

@ -11,6 +11,7 @@ BuildPlatformMain <build>package :
command_create.cpp
command_dump.cpp
command_extract.cpp
command_info.cpp
command_list.cpp
package.cpp
PackageWriterListener.cpp