Class: Sym::App::Commands::ShowExamples

Inherits:
BaseCommand show all
Defined in:
lib/sym/app/commands/show_examples.rb

Constant Summary

Constants included from Sym

BASH_COMPLETION, COMPLETION_FILE, COMPLETION_PATH, DESCRIPTION, ENV_ARGS_VARIABLE_NAME, LOGGER, NIL_LOGGER, VERSION

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Sym::App::Commands::BaseCommand

Instance Method Details

#example(comment: nil, command: nil, echo: nil, result: nil) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/sym/app/commands/show_examples.rb', line 65

def example(comment: nil, command: nil, echo: nil, result: nil)
  out = []
  out << "# #{comment}".white.dark.italic if comment
  out << command if command
  out << echo if echo
  out << result if result
  out << ''*80
end

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sym/app/commands/show_examples.rb', line 10

def execute
  output = []

  output << example(comment: 'generate a new private key into an environment variable:',
                    command: 'export mykey=$(sym -g)',
                    echo:    'echo $mykey',
                    result:  '75ngenJpB6zL47/8Wo7Ne6JN1pnOsqNEcIqblItpfg4='.green)

  output << example(comment: 'generate a new password-protected key & save to a file',
                    command: 'sym -gp -o ~/.key',
                    echo:    'New Password     : ' + '••••••••••'.green,
                    result:  'Confirm Password : ' + '••••••••••'.green)

  output << example(comment: 'encrypt a plain text string with a key, and save the output to a file',
                    command: 'sym -e -s ' + '"secret string"'.bold.yellow + ' -k $mykey -o file.enc',
                    echo:    'cat file.enc',
                    result:  'Y09MNDUyczU1S0UvelgrLzV0RTYxZz09CkBDMEw4Q0R0TmpnTm9md1QwNUNy%T013PT0K'.green)

  output << example(comment: 'decrypt a previously encrypted string:',
                    command: 'sym -d -s $(cat file.enc) -k $mykey',
                    result:  'secret string'.green)

  output << example(comment: 'encrypt sym.yml and save it to sym.enc:',
                    command: 'sym -e -f sym.yml -o sym.enc -k $mykey')

  output << example(comment: 'decrypt an encrypted file and print it to STDOUT:',
                    command: 'sym -df sym.enc -k $mykey')

  output << example(comment: 'edit an encrypted file in $EDITOR, ask for key, create file backup',
                    command: 'sym -tibf ecrets.enc',
                    result:  '
Private Key: ••••••••••••••••••••••••••••••••••••••••••••
Saved encrypted content to sym.enc.

Diff:
3c3
'.white.dark + '# (c) 2015 Konstantin Gredeskoul.  All rights reserved.'.red.bold  + '
---' + '
# (c) 2016 Konstantin Gredeskoul.  All rights reserved.'.green.bold)


  if Sym::App.is_osx?
  output << example(comment: 'generate a new password-encrypted key, save it to your Keychain:',
                    command: 'sym -gpx mykey -o ~/.key')

  output << example(comment: 'use the new key to encrypt a file:',
                    command: 'sym -x mykey -e -f password.txt -o passwords.enc')

  output << example(comment: 'use the new key to inline-edit the encrypted file:',
                    command: 'sym -x mykey -t -f sym.yml')
  end

  output.flatten.compact.join("\n")
end