#!/usr/bin/env python3
# ./dev --help

import argparse
import os
from subprocess import run
import sys

parser = argparse.ArgumentParser(prog='./dev')
subparsers = parser.add_subparsers(metavar='<command>', title='commands')
command = [
    'docker', 'run', '-it', '--rm', '-v', os.getcwd() + ':/src:cached',
    '-w=/src', 'rust:1.21.0'
]

def cargo(args, remaining):
    sys.exit(run(command + ['cargo'] + remaining or []).returncode)

parser_cargo = subparsers.add_parser('cargo', help='run a cargo command')
parser_cargo.set_defaults(func=cargo)

def sh(args, remaining):
    sys.exit(run(command + ['bash']).returncode)

parser_sh = subparsers.add_parser('sh', help='bring up a shell')
parser_sh.set_defaults(func=sh)

def test(args, remaining):
    sys.exit(run(command + ['cargo', 'test']).returncode)

parser_test = subparsers.add_parser('test', help='run tests')
parser_test.set_defaults(func=test)

if len(sys.argv) > 1:
    args, remaining = parser.parse_known_args()
    try:
        args.func(args, remaining)
    except FileNotFoundError:
        sys.exit('Please install Docker.')
else:
    parser.print_help()
