-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoneoff.py
More file actions
48 lines (39 loc) · 1.46 KB
/
Copy pathoneoff.py
File metadata and controls
48 lines (39 loc) · 1.46 KB
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
39
40
41
42
43
44
45
46
47
48
import click
import subprocess
import random
import string
import os
@click.group()
def cli(): pass
@cli.command()
@click.argument("runner")
@click.option("--editor", default="nano")
def run(runner, editor):
file_name = None
realrunner = runner
if runner == "!":
prevfile = open(f"{os.environ['HOME']}/.config/oneoff-previousfile", "r")
file_name = prevfile.read().split('\n')[1]
realrunner = prevfile.read().split('\n')[0]
prevfile.close()
elif runner == "EXPORT":
prevfile = open(f"{os.environ['HOME']}/.config/oneoff-previousfile", "r")
file_name = prevfile.read().split('\n')[1]
prevfile.close()
save_loc = click.prompt("Where do you want to save this? ", type=click.Path())
if save_loc[0] == "~":
save_loc = os.environ["HOME"] + save_loc[1:]
old_file = open(file_name, "r")
new_file = open(save_loc, "w")
new_file.write(old_file.read())
click.echo(f"Saved to {save_loc}")
return
else:
file_name = f"/tmp/oneoff-{''.join(random.choice(string.ascii_letters + string.digits) for i in range(8))}"
edit = subprocess.run([f"{editor}", f"{file_name}"])
if (edit.returncode != 0):
click.echo("Editor returned non-zero error code, quitting")
return
subprocess.run([f"{realrunner}", f"{file_name}"])
f = open(f"{os.environ['HOME']}/.config/oneoff-previousfile", "w")
f.write(f"{runner}\n{file_name}")