2014-04-27 05:31:13 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""cheat
|
|
|
|
|
2017-02-28 05:09:51 +01:00
|
|
|
Create and view cheatsheets on the command line.
|
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
Usage:
|
|
|
|
cheat <cheatsheet>
|
|
|
|
cheat -e <cheatsheet>
|
|
|
|
cheat -s <keyword>
|
|
|
|
cheat -l
|
|
|
|
cheat -d
|
|
|
|
cheat -v
|
|
|
|
|
|
|
|
Options:
|
2019-01-31 23:53:34 +01:00
|
|
|
-d --directories List directories on $CHEAT_PATH
|
2014-04-27 05:31:13 +02:00
|
|
|
-e --edit Edit cheatsheet
|
|
|
|
-l --list List cheatsheets
|
|
|
|
-s --search Search cheatsheets for <keyword>
|
|
|
|
-v --version Print the version number
|
2017-02-28 05:09:51 +01:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
To view the `tar` cheatsheet:
|
|
|
|
cheat tar
|
|
|
|
|
|
|
|
To edit (or create) the `foo` cheatsheet:
|
|
|
|
cheat -e foo
|
|
|
|
|
|
|
|
To list all available cheatsheets:
|
|
|
|
cheat -l
|
|
|
|
|
|
|
|
To search for "ssh" among all cheatsheets:
|
|
|
|
cheat -s ssh
|
2014-04-27 05:31:13 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# require the dependencies
|
2019-01-11 23:00:39 +01:00
|
|
|
from __future__ import print_function
|
2019-01-31 23:43:21 +01:00
|
|
|
from cheat.colorize import Colorize
|
2019-01-31 23:01:46 +01:00
|
|
|
from cheat.configuration import Configuration
|
2018-05-30 08:49:52 +02:00
|
|
|
from cheat.sheet import Sheet
|
2019-01-31 23:01:46 +01:00
|
|
|
from cheat.sheets import Sheets
|
2019-02-01 20:42:10 +01:00
|
|
|
from cheat.utils import Utils
|
2014-04-27 05:31:13 +02:00
|
|
|
from docopt import docopt
|
2019-02-01 02:03:10 +01:00
|
|
|
import os
|
2014-04-27 05:31:13 +02:00
|
|
|
|
2015-10-16 00:56:34 +02:00
|
|
|
if __name__ == '__main__':
|
2019-01-11 22:26:57 +01:00
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
# parse the command-line options
|
2019-02-13 18:42:58 +01:00
|
|
|
options = docopt(__doc__, version='cheat 2.5.1')
|
2014-04-27 05:31:13 +02:00
|
|
|
|
2019-01-31 22:45:28 +01:00
|
|
|
# initialize and validate configs
|
2018-05-30 08:49:52 +02:00
|
|
|
config = Configuration()
|
2019-01-31 22:45:28 +01:00
|
|
|
config.validate()
|
|
|
|
|
2019-02-01 21:10:03 +01:00
|
|
|
# create the CHEAT_USER_DIR if it does not exist
|
|
|
|
if not os.path.isdir(config.cheat_user_dir):
|
2019-02-01 02:03:10 +01:00
|
|
|
try:
|
2019-02-01 21:10:03 +01:00
|
|
|
os.mkdir(config.cheat_user_dir)
|
2019-02-01 02:03:10 +01:00
|
|
|
|
|
|
|
except OSError:
|
|
|
|
Utils.die("%s %s %s" % (
|
2019-02-01 21:10:03 +01:00
|
|
|
'Could not create CHEAT_USER_DIR (',
|
|
|
|
config.cheat_user_dir,
|
2019-02-01 02:03:10 +01:00
|
|
|
')')
|
|
|
|
)
|
|
|
|
|
2019-02-01 21:10:03 +01:00
|
|
|
# assert that the CHEAT_USER_DIR is readable and writable
|
|
|
|
if not os.access(config.cheat_user_dir, os.R_OK):
|
2019-02-01 02:03:10 +01:00
|
|
|
Utils.die("%s %s %s" % (
|
2019-02-01 21:10:03 +01:00
|
|
|
'The CHEAT_USER_DIR (',
|
|
|
|
config.cheat_user_dir,
|
2019-02-01 02:03:10 +01:00
|
|
|
') is not readable')
|
|
|
|
)
|
2019-02-01 21:10:03 +01:00
|
|
|
if not os.access(config.cheat_user_dir, os.W_OK):
|
2019-02-01 02:03:10 +01:00
|
|
|
Utils.die("%s %s %s" % (
|
2019-02-01 21:10:03 +01:00
|
|
|
'The CHEAT_USER_DIR (',
|
|
|
|
config.cheat_user_dir,
|
2019-02-01 02:03:10 +01:00
|
|
|
') is not writeable')
|
|
|
|
)
|
|
|
|
|
2019-01-31 22:45:28 +01:00
|
|
|
# bootsrap
|
2019-02-01 00:08:19 +01:00
|
|
|
sheets = Sheets(config)
|
|
|
|
sheet = Sheet(config, sheets)
|
2019-01-31 23:43:21 +01:00
|
|
|
colorize = Colorize(config)
|
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
# list directories
|
|
|
|
if options['--directories']:
|
2019-02-01 02:03:10 +01:00
|
|
|
print("\n".join(sheets.directories()))
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
# list cheatsheets
|
|
|
|
elif options['--list']:
|
2018-06-13 12:56:54 +02:00
|
|
|
print(sheets.list(), end="")
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
# create/edit cheatsheet
|
|
|
|
elif options['--edit']:
|
2019-02-01 17:23:38 +01:00
|
|
|
sheet.edit(options['<cheatsheet>'])
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
# search among the cheatsheets
|
|
|
|
elif options['--search']:
|
2019-01-31 23:43:21 +01:00
|
|
|
print(colorize.syntax(sheets.search(options['<keyword>'])), end="")
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
# print the cheatsheet
|
|
|
|
else:
|
2019-01-31 23:43:21 +01:00
|
|
|
print(colorize.syntax(sheet.read(options['<cheatsheet>'])), end="")
|