cli.c 2.4 KB

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