cli.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Original idea: https://github.com/br0ziliy
  2. #include "cli.h"
  3. #include <lib/toolbox/args.h>
  4. #include "cli_helpers.h"
  5. #include "commands/list/list.h"
  6. #include "commands/add/add.h"
  7. #include "commands/delete/delete.h"
  8. #include "commands/timezone/timezone.h"
  9. #include "commands/help/help.h"
  10. #include "commands/move/move.h"
  11. #include "commands/pin/pin.h"
  12. #include "commands/notification/notification.h"
  13. static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
  14. TOTP_CLI_PRINTF(
  15. "Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
  16. "\" command to get list of available commands.",
  17. furi_string_get_cstr(unknown_command));
  18. }
  19. static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
  20. PluginState* plugin_state = (PluginState*)context;
  21. FuriString* cmd = furi_string_alloc();
  22. args_read_string_and_trim(args, cmd);
  23. if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 ||
  24. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT) == 0 ||
  25. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT2) == 0 || furi_string_empty(cmd)) {
  26. totp_cli_command_help_handle();
  27. } else if(
  28. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0 ||
  29. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT) == 0 ||
  30. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT2) == 0) {
  31. totp_cli_command_add_handle(plugin_state, args, cli);
  32. } else if(
  33. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0 ||
  34. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST_ALT) == 0) {
  35. totp_cli_command_list_handle(plugin_state, cli);
  36. } else if(
  37. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0 ||
  38. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE_ALT) == 0) {
  39. totp_cli_command_delete_handle(plugin_state, args, cli);
  40. } else if(
  41. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
  42. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
  43. totp_cli_command_timezone_handle(plugin_state, args, cli);
  44. } else if(
  45. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
  46. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
  47. totp_cli_command_move_handle(plugin_state, args, cli);
  48. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
  49. totp_cli_command_pin_handle(plugin_state, args, cli);
  50. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
  51. totp_cli_command_notification_handle(plugin_state, args, cli);
  52. } else {
  53. totp_cli_print_unknown_command(cmd);
  54. }
  55. furi_string_free(cmd);
  56. }
  57. void totp_cli_register_command_handler(PluginState* plugin_state) {
  58. Cli* cli = furi_record_open(RECORD_CLI);
  59. cli_add_command(
  60. cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, plugin_state);
  61. furi_record_close(RECORD_CLI);
  62. }
  63. void totp_cli_unregister_command_handler() {
  64. Cli* cli = furi_record_open(RECORD_CLI);
  65. cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  66. furi_record_close(RECORD_CLI);
  67. }