From 8dda6a9241a20cc6e36ff6efd01e0ac84a7883db Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Tue, 13 Aug 2013 11:01:06 +0100 Subject: [PATCH 01/10] Package with distutils Created cheatsheets package to store the default sheets. --- cheat | 13 +++++++------ cheatsheets/__init__.py | 3 +++ {.cheat => cheatsheets}/apt-cache | 0 {.cheat => cheatsheets}/bash | 0 {.cheat => cheatsheets}/convert | 0 {.cheat => cheatsheets}/cut | 0 {.cheat => cheatsheets}/dhclient | 0 {.cheat => cheatsheets}/find | 0 {.cheat => cheatsheets}/git | 0 {.cheat => cheatsheets}/ln | 0 {.cheat => cheatsheets}/mysqldump | 0 {.cheat => cheatsheets}/netstat | 0 {.cheat => cheatsheets}/notify-send | 0 {.cheat => cheatsheets}/sed | 0 {.cheat => cheatsheets}/shred | 0 {.cheat => cheatsheets}/sockstat | 0 {.cheat => cheatsheets}/split | 0 {.cheat => cheatsheets}/ssh | 0 {.cheat => cheatsheets}/ssh-copy-id | 0 {.cheat => cheatsheets}/ssh-keygen | 0 {.cheat => cheatsheets}/stdout | 0 {.cheat => cheatsheets}/tar | 0 setup.py | 16 ++++++++++++++++ 23 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 cheatsheets/__init__.py rename {.cheat => cheatsheets}/apt-cache (100%) rename {.cheat => cheatsheets}/bash (100%) rename {.cheat => cheatsheets}/convert (100%) rename {.cheat => cheatsheets}/cut (100%) rename {.cheat => cheatsheets}/dhclient (100%) rename {.cheat => cheatsheets}/find (100%) rename {.cheat => cheatsheets}/git (100%) rename {.cheat => cheatsheets}/ln (100%) rename {.cheat => cheatsheets}/mysqldump (100%) rename {.cheat => cheatsheets}/netstat (100%) rename {.cheat => cheatsheets}/notify-send (100%) rename {.cheat => cheatsheets}/sed (100%) rename {.cheat => cheatsheets}/shred (100%) rename {.cheat => cheatsheets}/sockstat (100%) rename {.cheat => cheatsheets}/split (100%) rename {.cheat => cheatsheets}/ssh (100%) rename {.cheat => cheatsheets}/ssh-copy-id (100%) rename {.cheat => cheatsheets}/ssh-keygen (100%) rename {.cheat => cheatsheets}/stdout (100%) rename {.cheat => cheatsheets}/tar (100%) create mode 100644 setup.py diff --git a/cheat b/cheat index ad69b44..5cd91d4 100755 --- a/cheat +++ b/cheat @@ -1,18 +1,18 @@ #!/usr/bin/env python import os import sys +from cheatsheets import cheat_dir # assemble a keyphrase out of all params passed to the script keyphrase = ' '.join(sys.argv[1:]) -cheat_dir = os.path.expanduser('~') + '/.cheat/' -# verify that the ~/.cheat directory exists +# verify that the cheat directory exists if not os.path.isdir(cheat_dir): - print >> sys.stderr, 'The ~/.cheat directory does not exist.' + print >> sys.stderr, 'The cheat directory does not exist.' exit() -# list the files in the ~/.cheat directory -cheatsheets = os.listdir(cheat_dir) +# list the files in the cheat directory +cheatsheets = [f for f in os.listdir(cheat_dir) if '.' not in f] cheatsheets.sort() # print help if requested @@ -24,7 +24,8 @@ if keyphrase in ['', 'help', '--help', '-h']: # print the cheatsheet if it exists if keyphrase in cheatsheets: - with open (cheat_dir + keyphrase, 'r') as cheatsheet: + cheatsheet_filename = os.path.join(cheat_dir, keyphrase) + with open(cheatsheet_filename, 'r') as cheatsheet: print cheatsheet.read() # if it does not, say so diff --git a/cheatsheets/__init__.py b/cheatsheets/__init__.py new file mode 100644 index 0000000..8a9b797 --- /dev/null +++ b/cheatsheets/__init__.py @@ -0,0 +1,3 @@ +import os + +cheat_dir, __init = os.path.split(__file__) diff --git a/.cheat/apt-cache b/cheatsheets/apt-cache similarity index 100% rename from .cheat/apt-cache rename to cheatsheets/apt-cache diff --git a/.cheat/bash b/cheatsheets/bash similarity index 100% rename from .cheat/bash rename to cheatsheets/bash diff --git a/.cheat/convert b/cheatsheets/convert similarity index 100% rename from .cheat/convert rename to cheatsheets/convert diff --git a/.cheat/cut b/cheatsheets/cut similarity index 100% rename from .cheat/cut rename to cheatsheets/cut diff --git a/.cheat/dhclient b/cheatsheets/dhclient similarity index 100% rename from .cheat/dhclient rename to cheatsheets/dhclient diff --git a/.cheat/find b/cheatsheets/find similarity index 100% rename from .cheat/find rename to cheatsheets/find diff --git a/.cheat/git b/cheatsheets/git similarity index 100% rename from .cheat/git rename to cheatsheets/git diff --git a/.cheat/ln b/cheatsheets/ln similarity index 100% rename from .cheat/ln rename to cheatsheets/ln diff --git a/.cheat/mysqldump b/cheatsheets/mysqldump similarity index 100% rename from .cheat/mysqldump rename to cheatsheets/mysqldump diff --git a/.cheat/netstat b/cheatsheets/netstat similarity index 100% rename from .cheat/netstat rename to cheatsheets/netstat diff --git a/.cheat/notify-send b/cheatsheets/notify-send similarity index 100% rename from .cheat/notify-send rename to cheatsheets/notify-send diff --git a/.cheat/sed b/cheatsheets/sed similarity index 100% rename from .cheat/sed rename to cheatsheets/sed diff --git a/.cheat/shred b/cheatsheets/shred similarity index 100% rename from .cheat/shred rename to cheatsheets/shred diff --git a/.cheat/sockstat b/cheatsheets/sockstat similarity index 100% rename from .cheat/sockstat rename to cheatsheets/sockstat diff --git a/.cheat/split b/cheatsheets/split similarity index 100% rename from .cheat/split rename to cheatsheets/split diff --git a/.cheat/ssh b/cheatsheets/ssh similarity index 100% rename from .cheat/ssh rename to cheatsheets/ssh diff --git a/.cheat/ssh-copy-id b/cheatsheets/ssh-copy-id similarity index 100% rename from .cheat/ssh-copy-id rename to cheatsheets/ssh-copy-id diff --git a/.cheat/ssh-keygen b/cheatsheets/ssh-keygen similarity index 100% rename from .cheat/ssh-keygen rename to cheatsheets/ssh-keygen diff --git a/.cheat/stdout b/cheatsheets/stdout similarity index 100% rename from .cheat/stdout rename to cheatsheets/stdout diff --git a/.cheat/tar b/cheatsheets/tar similarity index 100% rename from .cheat/tar rename to cheatsheets/tar diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..af240f9 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +from distutils.core import setup +import os + +setup(name='cheat', + version='1.0', + description='Create and view interactive cheatsheets on the command-line', + author='Chris Lane', + author_email='chris@chris-allen-lane.com', + url='https://github.com/chrisallenlane/cheat', + packages=['cheatsheets'], + package_data={'cheatsheets': [f for f in os.listdir('cheatsheets') + if '.' not in f]}, + scripts=['cheat'] + ) From 53b93c00e1ead38c765e12281efacca7cc3bc4ab Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Tue, 13 Aug 2013 11:08:05 +0100 Subject: [PATCH 02/10] Update readme with setup.py --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4799788..04133d3 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Installing Do the following to install `cheat`: 1. Clone this repository and `cd` into it -2. Run `sudo ./install` +2. Run `$ sudo python setup.py install` The `install` script will copy a python file into `/usr/local/bin/`, and will also create a hidden `.cheat` folder (containing the cheatsheet content) in From 3c9136b476984bdf0a8287a0902a4d35b82a140f Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Tue, 13 Aug 2013 11:42:58 +0100 Subject: [PATCH 03/10] Replace support for user's .cheat directories --- cheat | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/cheat b/cheat index 5cd91d4..d0e63dc 100755 --- a/cheat +++ b/cheat @@ -6,28 +6,31 @@ from cheatsheets import cheat_dir # assemble a keyphrase out of all params passed to the script keyphrase = ' '.join(sys.argv[1:]) -# verify that the cheat directory exists -if not os.path.isdir(cheat_dir): - print >> sys.stderr, 'The cheat directory does not exist.' - exit() +user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat') # list the files in the cheat directory -cheatsheets = [f for f in os.listdir(cheat_dir) if '.' not in f] +cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f] +# add the user's cheat files if they have a ~/.cheat directory +if os.path.isdir(user_cheat_dir): + cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)] cheatsheets.sort() # print help if requested if keyphrase in ['', 'help', '--help', '-h']: print "Usage: cheat [keyphrase]\n" print "Available keyphrases:" - print "\n".join(cheatsheets) + print "\n".join([name[0] for name in cheatsheets]) exit() +sheet_found = False # print the cheatsheet if it exists -if keyphrase in cheatsheets: - cheatsheet_filename = os.path.join(cheat_dir, keyphrase) - with open(cheatsheet_filename, 'r') as cheatsheet: - print cheatsheet.read() +for sheet in cheatsheets: + if keyphrase == sheet[0]: + cheatsheet_filename = os.path.join(sheet[1], keyphrase) + with open(cheatsheet_filename, 'r') as cheatsheet: + print cheatsheet.read() + sheet_found = True # if it does not, say so -else: +if not sheet_found: print 'No cheatsheet found.' From 389d6d56719834fc020c59885c8e17c325eb5494 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Tue, 13 Aug 2013 11:49:37 +0100 Subject: [PATCH 04/10] Remove old install script --- install | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100755 install diff --git a/install b/install deleted file mode 100755 index cf64c1f..0000000 --- a/install +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python -from os.path import expanduser -import shutil -import sys - -try: - shutil.copy('./cheat', '/usr/local/bin/') - shutil.copytree('./.cheat', expanduser('~') + '/.cheat') - print "cheat has been installed successfully." -except IOError as e: - print >> sys.stderr, "This installer must be run as root." - sys.exit(1) From 63b0ffba141be7a40837bc6e3032f1c28893d624 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Fri, 16 Aug 2013 03:02:33 +0100 Subject: [PATCH 05/10] Don't require the package to be available. --- cheat | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cheat b/cheat index d0e63dc..2d9c0a6 100755 --- a/cheat +++ b/cheat @@ -1,20 +1,28 @@ #!/usr/bin/env python import os import sys -from cheatsheets import cheat_dir -# assemble a keyphrase out of all params passed to the script -keyphrase = ' '.join(sys.argv[1:]) +try: + # check to see if the cheat package is available + import cheatsheets + cheat_dir = cheatsheets.cheat_dir + cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f] +except ImportError: + cheatsheets = [] +# construct the path to the cheat directory user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat') # list the files in the cheat directory -cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f] # add the user's cheat files if they have a ~/.cheat directory if os.path.isdir(user_cheat_dir): - cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)] + cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir) + if '.' not in f] cheatsheets.sort() +# assemble a keyphrase out of all params passed to the script +keyphrase = ' '.join(sys.argv[1:]) + # print help if requested if keyphrase in ['', 'help', '--help', '-h']: print "Usage: cheat [keyphrase]\n" From 1944c1a351805918ef5e88a1985e6bd0910a28c3 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Fri, 16 Aug 2013 09:19:04 +0100 Subject: [PATCH 06/10] Add a non-root section to the installation guide. --- README.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 04133d3..507959a 100644 --- a/README.md +++ b/README.md @@ -47,15 +47,21 @@ to store notes on your favorite cookie recipes, feel free. Installing ---------- -Do the following to install `cheat`: -1. Clone this repository and `cd` into it -2. Run `$ sudo python setup.py install` +### Installing for all users (requires root) -The `install` script will copy a python file into `/usr/local/bin/`, and will -also create a hidden `.cheat` folder (containing the cheatsheet content) in -your home directory. +Clone this repository and `cd` into it, then run + sudo python setup.py install + +### Installing in your home directory + +Clone this repository and `cd` into it, then run + + mkdir -p ~/bin + cp cheat ~/bin + mkdir ~/.cheat + cp cheatsheets/* ~/.cheat Modifying Cheatsheets --------------------- From f10c7fd19bd54e5c5eb4cd81e660cdb7ee13d4c2 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Fri, 16 Aug 2013 09:26:18 +0100 Subject: [PATCH 07/10] Change most of the headers to h1's. Makes the subheadings in the installation section look less strange. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 507959a..3a260d6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ remember. Examples --------- +======== The next time you're forced to disarm a nuclear weapon without consulting Google, you may run: @@ -46,7 +46,7 @@ to store notes on your favorite cookie recipes, feel free. Installing ----------- +========== ### Installing for all users (requires root) @@ -64,7 +64,7 @@ Clone this repository and `cd` into it, then run cp cheatsheets/* ~/.cheat Modifying Cheatsheets ---------------------- +===================== The value of `cheat` is that it allows you to create your own cheatsheets - the defaults are meant to serve only as a starting point, and can and should be modified. @@ -84,7 +84,7 @@ with your [dotfiles][]. Contributing ------------- +============ If you would like to contribute additional cheatsheets for basic \*nix commands, please modify the `.cheat` file and send me a pull request. From b38bbceb8a7dfa6d3936c7b55537e789a1d95f52 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Fri, 16 Aug 2013 09:47:43 +0100 Subject: [PATCH 08/10] Change indent level from 2 spaces to 4. --- cheat | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cheat b/cheat index 2d9c0a6..d1968a8 100755 --- a/cheat +++ b/cheat @@ -25,20 +25,20 @@ keyphrase = ' '.join(sys.argv[1:]) # print help if requested if keyphrase in ['', 'help', '--help', '-h']: - print "Usage: cheat [keyphrase]\n" - print "Available keyphrases:" - print "\n".join([name[0] for name in cheatsheets]) - exit() + print "Usage: cheat [keyphrase]\n" + print "Available keyphrases:" + print "\n".join([name[0] for name in cheatsheets]) + exit() sheet_found = False # print the cheatsheet if it exists for sheet in cheatsheets: - if keyphrase == sheet[0]: + if keyphrase == sheet[0]: cheatsheet_filename = os.path.join(sheet[1], keyphrase) with open(cheatsheet_filename, 'r') as cheatsheet: - print cheatsheet.read() - sheet_found = True + print cheatsheet.read() + sheet_found = True # if it does not, say so if not sheet_found: - print 'No cheatsheet found.' + print 'No cheatsheet found.' From cdf0aa19264a1cbe1dc341fb82d8b9c4b3968a91 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Sat, 17 Aug 2013 03:16:44 +0100 Subject: [PATCH 09/10] Remove duplicates from the list of cheatsheets. --- cheat | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cheat b/cheat index b4a3249..72559f0 100755 --- a/cheat +++ b/cheat @@ -24,6 +24,16 @@ if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']: path = os.environ['CHEATPATH'].split(os.pathsep) if os.path.isdir(path): cheatsheets += [(f, path) for f in os.listdir(path) if '.' not in f] + +# remove any duplicates +def remove_duplicates(cheats): + sheets = [] + for i, sheet in enumerate(cheats): + if sheet[0] not in [c[0] for c in sheets]: + sheets.append(sheet) + return sheets + +cheatsheets = remove_duplicates(cheatsheets) cheatsheets.sort() # assemble a keyphrase out of all params passed to the script From 9946bab1a99a43decc18fde033c7e1d7f6279a35 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Sun, 18 Aug 2013 17:41:32 +0100 Subject: [PATCH 10/10] Don't split cheatpath for no reason --- cheat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cheat b/cheat index 72559f0..29a5add 100755 --- a/cheat +++ b/cheat @@ -21,7 +21,7 @@ if os.path.isdir(user_cheat_dir): # add the cheat files from the directory specified in $CHEATPATH if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']: - path = os.environ['CHEATPATH'].split(os.pathsep) + path = os.environ['CHEATPATH'] if os.path.isdir(path): cheatsheets += [(f, path) for f in os.listdir(path) if '.' not in f]