cli.c 4.3 KB

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