summaryrefslogtreecommitdiff
path: root/smount.py
blob: 9a0a3120ac5c3a13b7398b656c101b7ac3a49347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python

import json
import os
import subprocess
import sys


def main():
    """Main entry point"""
    if len(sys.argv) != 2:
        sys.exit(f'Invalid parameter. Usage: {sys.argv[0]} <mapping>\n')

    cstm_error = ''

    try:
        cstm_error = 'Could not open config (~/.smount.json)'

        with open(os.path.expanduser('~/.smount.json')) as fp:
            config_dict = json.load(fp)

        cstm_error = f'Invalid / nonexistent mapping: {sys.argv[1]}'
        data = config_dict[sys.argv[1]]
        cstm_error = 'subprocess error'
        args = ['/usr/bin/sshfs']
        if data.get('options'):
            for opt in data.get('options'):
                args.extend(['-o', opt])
        args.extend([data['src'], data['dest']])
        subprocess.check_call(args)
    except subprocess.CalledProcessError as err:
        print(f'Execution error: {err}', file=sys.stderr, flush=True)
    except Exception:
        print(f'Error: {cstm_error}', file=sys.stderr, flush=True)


if __name__ == '__main__':
    main()