#!python

import click
from requests import get

url = 'https://raw.githubusercontent.com'

class styles:
    end = '\033[0m'
    red = '\033[31m'

@click.group()
def ghg():
    'Get single files from github onto your computer'

@ghg.command()
@click.option('-r', '--repo', required=True, help='Target repository')
@click.option('-b', '--branch', required=False, default='master', help='Target branch (default: master)')
@click.option('-f', '--file', required=True, help='Path of target file')
@click.option('-o', '--output', required=False, help='Output to local file')
def scrape(repo, branch, file, output):
    target = '{}/{}/{}/{}'.format(url, repo, branch, file)
    res = get(target).text
    if res == '404: Not Found':
        click.echo('{}File doesn\'t exist{}'.format(styles.red, styles.end))
        exit()
    if output:
        open(output, '+w').write(res)
    else:
        click.echo(res)

if __name__ == '__main__':
    ghg(prog_name='ghg')