cli.c 2.7 KB

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