cli.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/update/update.h"
  8. #include "commands/delete/delete.h"
  9. #include "commands/timezone/timezone.h"
  10. #include "commands/help/help.h"
  11. #include "commands/move/move.h"
  12. #include "commands/pin/pin.h"
  13. #include "commands/notification/notification.h"
  14. #include "commands/reset/reset.h"
  15. #include "commands/automation/automation.h"
  16. #include "commands/details/details.h"
  17. static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
  18. TOTP_CLI_PRINTF_ERROR(
  19. "Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
  20. "\" command to get list of available commands.",
  21. furi_string_get_cstr(unknown_command));
  22. }
  23. static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
  24. TotpCliContext* cli_context = context;
  25. PluginState* plugin_state = cli_context->plugin_state;
  26. FuriString* cmd = furi_string_alloc();
  27. args_read_string_and_trim(args, cmd);
  28. if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 ||
  29. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT) == 0 ||
  30. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT2) == 0 || furi_string_empty(cmd)) {
  31. totp_cli_command_help_handle();
  32. } else if(
  33. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0 ||
  34. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT) == 0 ||
  35. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT2) == 0) {
  36. totp_cli_command_add_handle(plugin_state, args, cli);
  37. } else if(
  38. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0 ||
  39. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST_ALT) == 0) {
  40. totp_cli_command_list_handle(plugin_state, cli);
  41. } else if(
  42. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0 ||
  43. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE_ALT) == 0) {
  44. totp_cli_command_delete_handle(plugin_state, args, cli);
  45. } else if(
  46. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
  47. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
  48. totp_cli_command_timezone_handle(plugin_state, args, cli);
  49. } else if(
  50. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
  51. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
  52. totp_cli_command_move_handle(plugin_state, args, cli);
  53. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
  54. totp_cli_command_pin_handle(plugin_state, args, cli);
  55. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
  56. totp_cli_command_notification_handle(plugin_state, args, cli);
  57. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_AUTOMATION) == 0) {
  58. totp_cli_command_automation_handle(plugin_state, args, cli);
  59. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_RESET) == 0) {
  60. totp_cli_command_reset_handle(plugin_state, cli);
  61. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_UPDATE) == 0) {
  62. totp_cli_command_update_handle(plugin_state, args, cli);
  63. } else if(
  64. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DETAILS) == 0 ||
  65. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DETAILS_ALT) == 0) {
  66. totp_cli_command_details_handle(plugin_state, args, cli);
  67. } else {
  68. totp_cli_print_unknown_command(cmd);
  69. }
  70. furi_string_free(cmd);
  71. }
  72. TotpCliContext* totp_cli_register_command_handler(PluginState* plugin_state) {
  73. Cli* cli = furi_record_open(RECORD_CLI);
  74. TotpCliContext* context = malloc(sizeof(TotpCliContext));
  75. furi_check(context != NULL);
  76. context->plugin_state = plugin_state;
  77. cli_add_command(
  78. cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, context);
  79. furi_record_close(RECORD_CLI);
  80. return context;
  81. }
  82. void totp_cli_unregister_command_handler(TotpCliContext* context) {
  83. Cli* cli = furi_record_open(RECORD_CLI);
  84. cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  85. furi_record_close(RECORD_CLI);
  86. free(context);
  87. }