pkgman [un]install/update: Add non-interactive mode (-y)
This commit is contained in:
@@ -10,6 +10,13 @@
|
||||
#include "DecisionProvider.h"
|
||||
|
||||
|
||||
DecisionProvider::DecisionProvider(bool interactive)
|
||||
:
|
||||
fInteractive(interactive)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
DecisionProvider::YesNoDecisionNeeded(const BString& description,
|
||||
const BString& question, const BString& yes, const BString& no,
|
||||
@@ -25,6 +32,11 @@ DecisionProvider::YesNoDecisionNeeded(const BString& description,
|
||||
haveDefault
|
||||
? (BString(" (") << defaultChoice << ") ").String() : "");
|
||||
|
||||
if (!fInteractive) {
|
||||
printf("%s\n", yes.String());
|
||||
return true;
|
||||
}
|
||||
|
||||
char buffer[32];
|
||||
if (fgets(buffer, 32, stdin)) {
|
||||
if (haveDefault && (buffer[0] == '\n' || buffer[0] == '\0'))
|
||||
|
||||
@@ -9,10 +9,20 @@
|
||||
#include <package/Context.h>
|
||||
|
||||
|
||||
struct DecisionProvider : public BPackageKit::BDecisionProvider {
|
||||
virtual bool YesNoDecisionNeeded(const BString& description,
|
||||
const BString& question, const BString& yes, const BString& no,
|
||||
const BString& defaultChoice);
|
||||
class DecisionProvider : public BPackageKit::BDecisionProvider {
|
||||
public:
|
||||
DecisionProvider(bool interactive = true);
|
||||
|
||||
void SetInteractive(bool interactive)
|
||||
{ fInteractive = interactive; }
|
||||
|
||||
virtual bool YesNoDecisionNeeded(const BString& description,
|
||||
const BString& question, const BString& yes,
|
||||
const BString& no,
|
||||
const BString& defaultChoice);
|
||||
|
||||
private:
|
||||
bool fInteractive;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -22,13 +22,15 @@
|
||||
using namespace BPackageKit::BPrivate;
|
||||
|
||||
|
||||
PackageManager::PackageManager(BPackageInstallationLocation location)
|
||||
PackageManager::PackageManager(BPackageInstallationLocation location,
|
||||
bool interactive)
|
||||
:
|
||||
BPackageManager(location, &fClientInstallationInterface, this),
|
||||
BPackageManager::UserInteractionHandler(),
|
||||
fDecisionProvider(),
|
||||
fDecisionProvider(interactive),
|
||||
fClientInstallationInterface(),
|
||||
fPreviousDownloadPercentage(0)
|
||||
fPreviousDownloadPercentage(0),
|
||||
fInteractive(interactive)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -38,6 +40,14 @@ PackageManager::~PackageManager()
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PackageManager::SetInteractive(bool interactive)
|
||||
{
|
||||
fInteractive = interactive;
|
||||
fDecisionProvider.SetInteractive(interactive);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PackageManager::JobFailed(BJob* job)
|
||||
{
|
||||
@@ -80,6 +90,9 @@ PackageManager::HandleProblems()
|
||||
}
|
||||
}
|
||||
|
||||
if (!fInteractive)
|
||||
continue;
|
||||
|
||||
// let the user choose a solution
|
||||
printf("Please select a solution, skip the problem for now or quit.\n");
|
||||
for (;;) {
|
||||
@@ -112,6 +125,9 @@ PackageManager::HandleProblems()
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (problemCount > 0 && !fInteractive)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,9 +25,12 @@ class PackageManager : public BPackageManager,
|
||||
private BPackageManager::UserInteractionHandler {
|
||||
public:
|
||||
PackageManager(
|
||||
BPackageInstallationLocation location);
|
||||
BPackageInstallationLocation location,
|
||||
bool interactive = true);
|
||||
~PackageManager();
|
||||
|
||||
void SetInteractive(bool interactive);
|
||||
|
||||
virtual void JobFailed(BJob* job);
|
||||
virtual void JobAborted(BJob* job);
|
||||
|
||||
@@ -68,6 +71,7 @@ private:
|
||||
BPackageManager::ClientInstallationInterface
|
||||
fClientInstallationInterface;
|
||||
int32 fPreviousDownloadPercentage;
|
||||
bool fInteractive;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -35,6 +35,9 @@ static const char* const kLongUsage =
|
||||
" -H, --home\n"
|
||||
" Install the packages in the user's home directory. Default is to\n"
|
||||
" install in the system directory.\n"
|
||||
" -y\n"
|
||||
" Non-interactive mode. Automatically confirm changes, but fail when\n"
|
||||
" encountering problems.\n"
|
||||
"\n";
|
||||
|
||||
|
||||
@@ -47,6 +50,7 @@ InstallCommand::Execute(int argc, const char* const* argv)
|
||||
{
|
||||
BPackageInstallationLocation location
|
||||
= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
|
||||
bool interactive = true;
|
||||
|
||||
while (true) {
|
||||
static struct option sLongOptions[] = {
|
||||
@@ -56,7 +60,7 @@ InstallCommand::Execute(int argc, const char* const* argv)
|
||||
};
|
||||
|
||||
opterr = 0; // don't print errors
|
||||
int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL);
|
||||
int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
@@ -69,6 +73,10 @@ InstallCommand::Execute(int argc, const char* const* argv)
|
||||
location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
interactive = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
PrintUsageAndExit(true);
|
||||
break;
|
||||
@@ -83,7 +91,7 @@ InstallCommand::Execute(int argc, const char* const* argv)
|
||||
const char* const* packages = argv + optind;
|
||||
|
||||
// perform the installation
|
||||
PackageManager packageManager(location);
|
||||
PackageManager packageManager(location, interactive);
|
||||
packageManager.Install(packages, packageCount);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -35,6 +35,9 @@ static const char* const kLongUsage =
|
||||
" -H, --home\n"
|
||||
" Uninstall the packages from the user's home directory. Default is to\n"
|
||||
" uninstall from the system directory.\n"
|
||||
" -y\n"
|
||||
" Non-interactive mode. Automatically confirm changes, but fail when\n"
|
||||
" encountering problems.\n"
|
||||
"\n";
|
||||
|
||||
|
||||
@@ -47,6 +50,7 @@ UninstallCommand::Execute(int argc, const char* const* argv)
|
||||
{
|
||||
BPackageInstallationLocation location
|
||||
= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
|
||||
bool interactive = true;
|
||||
|
||||
while (true) {
|
||||
static struct option sLongOptions[] = {
|
||||
@@ -56,7 +60,7 @@ UninstallCommand::Execute(int argc, const char* const* argv)
|
||||
};
|
||||
|
||||
opterr = 0; // don't print errors
|
||||
int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL);
|
||||
int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
@@ -69,6 +73,10 @@ UninstallCommand::Execute(int argc, const char* const* argv)
|
||||
location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
interactive = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
PrintUsageAndExit(true);
|
||||
break;
|
||||
@@ -83,7 +91,7 @@ UninstallCommand::Execute(int argc, const char* const* argv)
|
||||
const char* const* packages = argv + optind;
|
||||
|
||||
// perform the installation
|
||||
PackageManager packageManager(location);
|
||||
PackageManager packageManager(location, interactive);
|
||||
packageManager.Uninstall(packages, packageCount);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -36,6 +36,9 @@ static const char* const kLongUsage =
|
||||
" -H, --home\n"
|
||||
" Update the packages in the user's home directory. Default is to\n"
|
||||
" update in the system directory.\n"
|
||||
" -y\n"
|
||||
" Non-interactive mode. Automatically confirm changes, but fail when\n"
|
||||
" encountering problems.\n"
|
||||
"\n";
|
||||
|
||||
|
||||
@@ -48,6 +51,7 @@ UpdateCommand::Execute(int argc, const char* const* argv)
|
||||
{
|
||||
BPackageInstallationLocation location
|
||||
= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
|
||||
bool interactive = true;
|
||||
|
||||
while (true) {
|
||||
static struct option sLongOptions[] = {
|
||||
@@ -57,7 +61,7 @@ UpdateCommand::Execute(int argc, const char* const* argv)
|
||||
};
|
||||
|
||||
opterr = 0; // don't print errors
|
||||
int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL);
|
||||
int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
@@ -70,6 +74,10 @@ UpdateCommand::Execute(int argc, const char* const* argv)
|
||||
location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
interactive = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
PrintUsageAndExit(true);
|
||||
break;
|
||||
@@ -81,7 +89,7 @@ UpdateCommand::Execute(int argc, const char* const* argv)
|
||||
const char* const* packages = argv + optind;
|
||||
|
||||
// perform the update
|
||||
PackageManager packageManager(location);
|
||||
PackageManager packageManager(location, interactive);
|
||||
packageManager.Update(packages, packageCount);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user