Skip to content

Commit

Permalink
feat: improve helps
Browse files Browse the repository at this point in the history
  • Loading branch information
Rirusha committed Dec 14, 2024
1 parent 950ffaf commit 7d799fb
Show file tree
Hide file tree
Showing 58 changed files with 1,902 additions and 1,288 deletions.
1 change: 1 addition & 0 deletions data/defaults
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[settings]
auto-update=true
use-fuzzy-search=true
auto-upgrade-kernel=false
209 changes: 132 additions & 77 deletions src/apa.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,107 +17,167 @@

namespace Apa {

const string CONFIG_COMMAND = "config";
const string KERNEL_COMMAND = "kernel";
const string LIST_COMMAND = "list";
const string INFO_COMMAND = "info";
const string MOO_COMMAND = "moo";
const string TASK_COMMAND = "task";
const string HELP_COMMAND = "help";
const string VERSION_COMMAND = "version";
const string SEARCH_FILE_COMMAND = "search-file";
const string REPO_COMMAND = "repo";
const string REINSTALL_COMMAND = "reinstall";

const CommandDescriptionEntity[] VISIBLE_COMMANDS = {
{ AptGet.INSTALL, Commands.Descriptions.install, true },
{ REINSTALL_COMMAND, Commands.Descriptions.reinstall, true },
{ AptGet.REMOVE, Commands.Descriptions.remove, true },
{ AptGet.DO, Commands.Descriptions.do, true },
{ AptGet.UPDATE, Commands.Descriptions.update, true },
{ AptGet.UPGRADE, Commands.Descriptions.upgrade, true },
{ AptCache.SEARCH, Commands.Descriptions.search, false },
{ KERNEL_COMMAND, Commands.Descriptions.kernel, false },
{ CONFIG_COMMAND, Commands.Descriptions.config, false },
{ TASK_COMMAND, Commands.Descriptions.task, false },
{ AptGet.AUTOREMOVE, Commands.Descriptions.autoremove, true },
// { AptGet.SOURCE, Commands.Descriptions.source, true },
{ LIST_COMMAND, Commands.Descriptions.list, false },
{ INFO_COMMAND, Commands.Descriptions.info, false },
{ SEARCH_FILE_COMMAND, Commands.Descriptions.search_file, false },
{ HELP_COMMAND, Commands.Descriptions.help, false },
{ VERSION_COMMAND, Commands.Descriptions.version, false },
};

public async int run (owned string[] argv) {

string? command = cut_of_command (ref argv);
string? subcommand = null;

if (command == null && argv.length == 0) {
if (command == null) {
Help.print_apa ();
return ExitCode.SUCCESS;
}

} else if (command == null && argv.length > 0) {
if ("-h" in argv || "--help" in argv) {
Help.print_help (command);
return ExitCode.SUCCESS;
var entity = CommandEntity.find (Commands.Data.all_commands (), command);

if (entity == null) {
// print (command);
print_error (_("Unknown command `%s'").printf (command));
return ExitCode.BASE_ERROR;
}

CommandEntity? subcommand_entity = null;
if (entity.subcommands != null) {
subcommand = cut_of_command (ref argv);

subcommand_entity = CommandEntity.find (entity.subcommands.to_array (), subcommand);

if (subcommand_entity == null) {
print_error (_("Unknown subcommand `%s' of `%s' command").printf (subcommand, command));
return ExitCode.BASE_ERROR;
}

if ("-v" in argv || "--version" in argv) {
print (get_version ());
} else if (entity.subcommands != null && subcommand_entity == null) {
print_error (_("No subcommand. Try `apa %s --help'").printf (command));
return ExitCode.BASE_ERROR;
}

if ("-h" in argv || "--help" in argv) {
Help.print_command_help (command, subcommand);
return ExitCode.SUCCESS;
}

if ("-v" in argv || "--version" in argv) {
print_version ();
return ExitCode.SUCCESS;
}

if (command == null) {
if (argv.length == 0) {
Help.print_apa ();
return ExitCode.SUCCESS;

} else {
print_error (_("No command. Try `apa help'"));
return ExitCode.BASE_ERROR;
}

}

var args_handler = new ArgsHandler (argv);
CommandEntity current_entity = subcommand_entity ?? entity;

if (current_entity.need_root) {
check_is_root (command);
}
if (current_entity.need_no_packagekit) {
check_pk_is_not_running ();
}

try {
var args_handler = new ArgsHandler (argv);
args_handler.init_options (
current_entity.options.to_array (),
current_entity.arg_options.to_array ()
);

switch (command) {
case KERNEL_COMMAND:
return yield kernel (argv);
case Commands.Data.KERNEL_COMMAND:
switch (subcommand) {
case Commands.Data.KERNEL_UPGRADE_SUBCOMMAND:
return yield Kernel.upgrade (args_handler);

case Commands.Data.KERNEL_LIST_SUBCOMMAND:
return yield Kernel.list (args_handler);

default:
assert_not_reached ();
}

case Commands.Data.TASK_COMMAND:
switch (subcommand) {
case Commands.Data.TASK_SEARCH_SUBCOMMAND:
return yield Task.search (args_handler);

case Commands.Data.TASK_SHOW_SUBCOMMAND:
return yield Task.show (args_handler);

case Commands.Data.TASK_INSTALL_SUBCOMMAND:
return yield Task.install (args_handler);

case Commands.Data.TASK_LIST_SUBCOMMAND:
return yield Task.list (args_handler);

default:
assert_not_reached ();
}

case Commands.Data.CONFIG_COMMAND:
switch (subcommand) {
case Commands.Data.CONFIG_RESET_SUBCOMMAND:
return yield Config.reset (args_handler);

case Commands.Data.CONFIG_LIST_SUBCOMMAND:
return yield Config.list (args_handler);

case TASK_COMMAND:
return yield task (argv);
case Commands.Data.CONFIG_GET_SUBCOMMAND:
return yield Config.get (args_handler);

case CONFIG_COMMAND:
return yield config (argv);
case Commands.Data.CONFIG_SET_SUBCOMMAND:
return yield Config.set (args_handler);

case REPO_COMMAND:
return yield repo (argv);
default:
assert_not_reached ();
}

case Commands.Data.REPO_COMMAND:
switch (subcommand) {
case Commands.Data.REPO_LIST_SUBCOMMAND:
return yield Repo.list (args_handler);

case Commands.Data.REPO_ADD_SUBCOMMAND:
return yield Repo.add (args_handler);

case Commands.Data.ADD_AIDES_SUBCOMMAND:
return yield Repo.add_aides (args_handler);

case Commands.Data.REMOVE_AIDES_SUBCOMMAND:
return yield Repo.remove_aides (args_handler);

case Commands.Data.REPO_REMOVE_SUBCOMMAND:
return yield Repo.remove (args_handler);

default:
assert_not_reached ();
}

case AptGet.AUTOREMOVE:
check_pk_is_not_running ();
check_is_root (command);
return yield autoremove (args_handler);

case AptGet.DO:
check_pk_is_not_running ();
check_is_root (command);
return yield @do (args_handler);

case AptGet.UPDATE:
check_pk_is_not_running ();
check_is_root (command);
return yield update (args_handler);

case AptGet.UPGRADE:
check_pk_is_not_running ();
check_is_root (command);
return yield upgrade (args_handler);

case AptGet.INSTALL:
check_pk_is_not_running ();
check_is_root (command);
return yield install (args_handler);

case REINSTALL_COMMAND:
check_pk_is_not_running ();
check_is_root (command);
case Commands.Data.REINSTALL_COMMAND:
return yield reinstall (args_handler);

case AptGet.REMOVE:
check_pk_is_not_running ();
check_is_root (command);
return yield remove (args_handler);

case AptGet.SOURCE:
Expand All @@ -126,33 +186,28 @@ namespace Apa {
case AptCache.SEARCH:
return yield search (args_handler);

case LIST_COMMAND:
return yield Rpm.list (args_handler);
case Commands.Data.LIST_COMMAND:
return yield list (args_handler);

case INFO_COMMAND:
case Commands.Data.INFO_COMMAND:
return yield info (args_handler);

case SEARCH_FILE_COMMAND:
case Commands.Data.SEARCH_FILE_COMMAND:
return yield search_file (args_handler);

case MOO_COMMAND:
case Commands.Data.MOO_COMMAND:
return moo (args_handler);

case VERSION_COMMAND:
print (get_version ());
case Commands.Data.VERSION_COMMAND:
print_version ();
return ExitCode.SUCCESS;

case HELP_COMMAND:
case Commands.Data.HELP_COMMAND:
Help.print_apa ();
return ExitCode.SUCCESS;

case null:
Help.print_apa ();
return ExitCode.BASE_ERROR;

default:
print_error (_("Unknown command `%s'").printf (command));
return ExitCode.BASE_ERROR;
assert_not_reached ();
}

} catch (CommandError e) {
Expand Down
6 changes: 0 additions & 6 deletions src/apa/autoremove.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ namespace Apa {
) throws CommandError, OptionsError {
var error = new Gee.ArrayList<string> ();

args_handler.init_options (
OptionData.concat (AptGet.Data.COMMON_OPTIONS_DATA, AptGet.Data.AUTOREMOVE_OPTIONS_DATA),
OptionData.concat (AptGet.Data.COMMON_ARG_OPTIONS_DATA, AptGet.Data.AUTOREMOVE_ARG_OPTIONS_DATA),
skip_unknown_options
);

while (true) {
error.clear ();
var status = yield AptGet.autoremove (args_handler, error);
Expand Down
58 changes: 0 additions & 58 deletions src/apa/config/config.vala

This file was deleted.

8 changes: 1 addition & 7 deletions src/apa/config/get.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@ namespace Apa.Config {
owned ArgsHandler args_handler,
bool skip_unknown_options = false
) throws CommandError, OptionsError {
args_handler.init_options (
OptionData.concat (Data.COMMON_OPTIONS_DATA, Data.GET_OPTIONS_DATA),
OptionData.concat (Data.COMMON_ARG_OPTIONS_DATA, Data.GET_ARG_OPTIONS_DATA),
skip_unknown_options
);

if (args_handler.args.size == 0) {
throw new CommandError.COMMON (_("Nothing to get"));
}

if (args_handler.args.size > 1) {
throw new CommandError.TOO_MANY_ARGS (null);
throw new CommandError.TOO_MANY_ARGS ("");
}

var value = ConfigManager.get_default ().get_value (args_handler.args[0]);
Expand Down
10 changes: 2 additions & 8 deletions src/apa/config/list.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ namespace Apa.Config {
owned ArgsHandler args_handler,
bool skip_unknown_options = false
) throws OptionsError {
args_handler.init_options (
OptionData.concat (Data.COMMON_OPTIONS_DATA, Data.LIST_OPTIONS_DATA),
OptionData.concat (Data.COMMON_ARG_OPTIONS_DATA, Data.LIST_ARG_OPTIONS_DATA),
skip_unknown_options
);

foreach (var possible_key in Data.POSSIBLE_CONFIG_KEYS) {
print ("%s\t- %s".printf (possible_key.name, possible_key.description_getter ()));
foreach (var possible_key in Data.possible_config_keys ()) {
print ("%s\t- %s".printf (possible_key.name, possible_key.description));
}

return 0;
Expand Down
Loading

0 comments on commit 7d799fb

Please sign in to comment.