Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

125 linhas
3.6 KiB

  1. #!/usr/bin/env node
  2. 'use strict';
  3. const clipboardy = require('clipboardy');
  4. const meow = require('meow');
  5. const LessPass = require('lesspass');
  6. const read = require('read');
  7. const chalk = require('chalk');
  8. const helpMessage = `
  9. Usage
  10. $ lesspass <site> <login> [masterPassword] [options]
  11. Options
  12. -l add lowercase in password
  13. -u add uppercase in password
  14. -d add digits in password
  15. -s add symbols in password
  16. --no-lowercase remove lowercase from password
  17. --no-uppercase remove uppercase from password
  18. --no-digits remove digits from password
  19. --no-symbols remove symbols from password
  20. --length, -L int (default 16)
  21. --counter, -c int (default 1)
  22. --clipboard, -C copy generated password to clipboard rather than displaying it.
  23. Need pbcopy (OSX), xsel (Linux) or clip (Windows).
  24. Examples
  25. # no symbols
  26. $ lesspass lesspass.com contact@lesspass.com password --no-symbols
  27. OlfK63bmUhqrGODR
  28. # no symbols shortcut
  29. $ lesspass lesspass.com contact@lesspass.com password -lud
  30. OlfK63bmUhqrGODR
  31. # only digits and length of 8
  32. $ lesspass lesspass.com contact@lesspass.com -d -L8
  33. master password:
  34. 75837019`;
  35. const cli = meow(helpMessage, {
  36. alias: {L: 'length', c: 'counter', C: 'clipboard'},
  37. boolean: ['l', 'u', 'd', 's', 'C', 'clipboard']
  38. });
  39. function calcPassword(site, login, masterPassword, passwordProfile) {
  40. LessPass.generatePassword(site, login, masterPassword, passwordProfile)
  41. .then(function(generatedPassword) {
  42. if (passwordProfile.clipboard) {
  43. clipboardy.write(generatedPassword)
  44. .then(() => {
  45. console.log('Copied to clipboard');
  46. process.exit();
  47. }).catch(err => {
  48. console.error(chalk.red('Copy failed.'));
  49. console.error(err.message);
  50. process.exit(1);
  51. });
  52. } else {
  53. console.log(generatedPassword);
  54. process.exit();
  55. }
  56. });
  57. }
  58. function hasNoShortOption(options) {
  59. let hasShortOption = false;
  60. ['l', 'u', 'd', 's'].forEach(function(shortOption) {
  61. if (typeof options[shortOption] !== 'undefined' && options[shortOption]) {
  62. hasShortOption = true;
  63. }
  64. });
  65. return !hasShortOption;
  66. }
  67. function getOptionBoolean(options, optionString) {
  68. let shortOption = optionString.substring(0, 1);
  69. if (options[shortOption]) {
  70. return true;
  71. }
  72. if (typeof options[optionString] === 'undefined') {
  73. return hasNoShortOption(options);
  74. }
  75. return options[optionString]
  76. }
  77. const lowercase = getOptionBoolean(cli.flags, 'lowercase');
  78. const uppercase = getOptionBoolean(cli.flags, 'uppercase');
  79. const symbols = getOptionBoolean(cli.flags, 'symbols');
  80. const digits = getOptionBoolean(cli.flags, 'digits');
  81. const passwordProfile = {
  82. lowercase: lowercase,
  83. uppercase: uppercase,
  84. symbols: symbols,
  85. numbers: digits,
  86. clipboard: cli.flags.clipboard || false,
  87. length: cli.flags.length || 16,
  88. counter: cli.flags.counter || 1
  89. };
  90. const site = cli.input[0];
  91. const login = cli.input[1];
  92. if (typeof site === 'undefined' && typeof login === 'undefined') {
  93. console.log(chalk.red('site or login cannot be empty'));
  94. console.log('type lesspass --help');
  95. process.exit(-1);
  96. }
  97. if (cli.input.length === 3) {
  98. const masterPassword = cli.input[2];
  99. calcPassword(site, login, masterPassword, passwordProfile)
  100. } else {
  101. read({prompt: 'master password: ', silent: true}, function(er, password) {
  102. calcPassword(site, login, password, passwordProfile)
  103. });
  104. }