cli.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
  13. TOTP_CLI_PRINTF(
  14. "Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
  15. "\" command to get list of available commands.",
  16. furi_string_get_cstr(unknown_command));
  17. }
  18. static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
  19. PluginState* plugin_state = (PluginState*)context;
  20. FuriString* cmd = furi_string_alloc();
  21. args_read_string_and_trim(args, cmd);
  22. if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 ||
  23. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT) == 0 ||
  24. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT2) == 0 || furi_string_empty(cmd)) {
  25. totp_cli_command_help_handle();
  26. } else if(
  27. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0 ||
  28. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT) == 0 ||
  29. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT2) == 0) {
  30. totp_cli_command_add_handle(plugin_state, args, cli);
  31. } else if(
  32. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0 ||
  33. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST_ALT) == 0) {
  34. totp_cli_command_list_handle(plugin_state, cli);
  35. } else if(
  36. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0 ||
  37. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE_ALT) == 0) {
  38. totp_cli_command_delete_handle(plugin_state, args, cli);
  39. } else if(
  40. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
  41. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
  42. totp_cli_command_timezone_handle(plugin_state, args, cli);
  43. } else if(
  44. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
  45. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
  46. totp_cli_command_move_handle(plugin_state, args, cli);
  47. } else if(
  48. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
  49. totp_cli_command_pin_handle(plugin_state, args, cli);
  50. } else {
  51. totp_cli_print_unknown_command(cmd);
  52. }
  53. furi_string_free(cmd);
  54. }
  55. void totp_cli_register_command_handler(PluginState* plugin_state) {
  56. Cli* cli = furi_record_open(RECORD_CLI);
  57. cli_add_command(
  58. cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, plugin_state);
  59. furi_record_close(RECORD_CLI);
  60. }
  61. void totp_cli_unregister_command_handler() {
  62. Cli* cli = furi_record_open(RECORD_CLI);
  63. cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  64. furi_record_close(RECORD_CLI);
  65. }