Code Monkey home page Code Monkey logo

getgist's Introduction

Travis CI Coveralls PyPI Version Python Version

GetGist

Easily download any file from a GitHub Gist, with one single command.

Why?

Because of reasons I do not have a dotfiles repository. I prefer to store my init.vim, .gitconfig, .bashrc etc. as Gists.

I wrote this CLI so I could update my dotfiles with one single command: getmy vim.init, for example β€” and it's done.

Install

$ pip install getgist

GetGist works with Python 3.6+.

To update it just run $ pip install --upgrade getgist.

Usage

Getting Gists from GitHub

Just run getgist <username> <filename>. For example:

$ getgist cuducos .vimrc
  Fetching https://api.github.com/users/cuducos/gists
  Reading https://gist.githubusercontent.com/cuducos/409fac6ac23bf515f495/raw/666d7d01a0058e4fd898ff752db66160f10a60bb/.vimrc
  Saving .vimrc
  Done!

GetGist asks you what to do when a local file (with the same name) exists. If you decide not to delete your local copy of the file, it will be renamed with extensions such as .bkp, .bkp1, .bkp2 etc.

Updating Gists at GitHub

Just run putgist <username> <filename> to update the remote Gist with the contents of the local file. It requires an OAuth token (see Using OAuth authentication below). For example:

$ putgist cuducos .vimrc
  User cuducos authenticated
  Fetching https://api.github.com/gists
  Sending contents of .vimrc to https://api.github.com/gists/409fac6ac23bf515f495
  Done!
  The URL to this Gist is: https://gist.github.com/cuducos/409fac6ac23bf515f495

GetGist asks you what to do when it finds the different files with the same name in different Gists.

Listing Gist files from GitHub

Just run lsgists <username>. For example:

$ lsgists cuducos
  Gist           File               URL
  -------------  ------------------ -------------------------
  First Gist     file.md            https://gist.github.com/…
  My Gist #2     another_file.md    https://gist.github.com/…
  My Gist #2     README.md          https://gist.github.com/…

Secret Gists (when user is authenticated) are listed with [Secret Gist] tag next to their names.

Using OAuth authentication

Why?

Add your personal access token as as environment variable to allow:

  1. downloading private gists
  2. updating existing gists
  3. listing private gists

How?

  1. Get a personal access token with permission to manage your gists from GitHub settings
  2. Set an environment variable called GETGIST_TOKEN with your personal access token

This article might help you create an environment variable in a Unix-based operational system with Bash, but feel free to search alternatives for other systems and shells.

Example

$ export GETGIST_TOKEN=whatever1234
$ getgist cuducos .vimrc
  User cuducos authenticated
  Fetching https://api.github.com/gists
  Reading https://gist.githubusercontent.com/cuducos/409fac6ac23bf515f495/raw/666d7d01a0058e4fd898ff752db66160f10a60bb/.vimrc
  Saving .vimrc
  Done!
  The URL to this Gist is: https://gist.github.com/cuducos/409fac6ac23bf515f495

This will work even if the file you are trying to download is a private gist (surely the user name has to match the GETGIST_TOKEN account).

Setting a default user

Why?

Set a default user to avoid typing your GitHub user name all the time.

How?

  1. Set an environment variable called GETGIST_USER with your GitHub user name
  2. Use the shortcut getmy <filename>, putmy <filename> or mygists

Example

$ export GETGIST_USER=cuducos
$ getmy .vimrc
  Fetching https://api.github.com/users/cuducos/gists
  Reading https://gist.githubusercontent.com/cuducos/409fac6ac23bf515f495/raw/666d7d01a0058e4fd898ff752db66160f10a60bb/.vimrc
  Saving .vimrc
  Done!
  The URL to this Gist is: https://gist.github.com/cuducos/409fac6ac23bf515f495

Contributing

We use Poetry to manage our development environment:

  1. poetry install will get you a virtualenv with all the dependencies for you
  2. poetry shell will activate this virtualenv
  3. exit deactivates this virtualenv

Feel free to report an issue, open a pull request, or drop a line.

Don't forget to format your code with Black, and to write and run tests:

$ tox

getgist's People

Contributors

cojad avatar cuducos avatar kmmelcher avatar sanarisan avatar vmesel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

getgist's Issues

Refactor: better abstractions needed for GetGist internals

This package has a poor design.

Both getgist.github and getgist.local (the core modules of the package) abstracts two things, making then coupled: they act as an adapter to systems (GitHub and local file system, respectively) and as a manager output information (formating, asking for confirmation, etc.). We need to decouple these responsibilities.

I suggest a huge refactor in which we end up with:

  • GitHub class with methods to handle GitHub related actions:

    • .read(name: str) returns the contents of the gist
    • .write(name: str, contents: str, private: bool) writes the Gist and returns its URL
    • list() return a list of all the Gist files for a given user
    • plus β€œprivate” methods needed for the internals
  • Local class with methods to handle local file system actions, but… to be honest, we might just inherit from Python's native pathlib.Path adding one or other method or wrapper to better fit our use case (and, yes, we can drop support for Python 2.7):

    • pathlib.Path.read_text() to read the contents of a local file
    • pathlib.Path.write_text(…) to write contents to a local file
    • plus β€œprivate” methods needed for the internals
  • A logger/output object to replace existing GetGistCommons that would be able to:

    • write to stdout or stderr without raising errors when running as CLI
    • raise custom GetGist errors when using in a script (as in #14)

List gists/files available

Something like a ls for gists. For example:

$ getgist cuducos
  .bash_profie
  .vimrc (from Gist: MacVim setup)
  .vimrc (from Gist: NeoVim setup)

It fails to create a gist when the path incudes directories

For example:

$ putmy Downloads/colors.py                                                                      cuducos@thomas
  User cuducos authenticated
  Fetching https://api.github.com/gists
  Sending contents of Downloads/colors.py to https://api.github.com/gists
  Could not create Downloads/colors.py
  POST request returned 422

(One might try the same for other commands, such as getmy ~/.vimrc for example)

Add documentatin for API usage

In case people want to use it as part of their file scripts.

For example, to read the contents of a Gist:

getgist = GetGist("init.vimn")
content = getgist.github.read_gist_file(self.gist)

We can refactor the internals for a better API β€” I mean, it would make more sense to:

getgist = GetGist("init.vim")  # throws an error if (a) not found or (b) multiple files found with the same name
contents = getgist.contents

Differentiate private gists in lsgists command

For example:

  Gist               File           URL
-----------------  -------------  -------------------------
README.md           My Gist #2      https://gist.github.com/…
anothe_file.md      My Gist #2      https://gist.github.com/…
file.md πŸ”’          First Gist πŸ”’    https://gist.github.com/…

Maybe πŸ”’ is not a good idea because it makes it more difficult to use withe grep (user has to input the emoji in the command, e.g.: lsgists cuducos | grep πŸ”’).

Improve lsgists command

Awesome tool!

The lsgists command lists all gists including those in subfolders. Like unix ls, it would be better if it only listed the top level gists, That is also the way the gists.github.com site works.
Look at my gists at https://gists.github.com/osiristerje . You can see I have a history folder (that comes from Visual Studio Code sync settings, so it is automatically updated). Using lsgists the content of this folder is listed along with the few top level (16) gists I do have.

image

One could additionally add an option to list content in subfolders, using the same syntax as the UNIX ls.

Add mygists command

Shortcut for lsgists $GETGUST_USERNAME (just as getmy file is a shortcur for getgist $GETGIST_USERNAME file).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.