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