cli.c 4.1 KB

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