Reworked arguments and then actually implemented them
This commit is contained in:
parent
cde4bffb0d
commit
709d99f729
1 changed files with 60 additions and 46 deletions
|
@ -29,27 +29,51 @@ def main():
|
||||||
|
|
||||||
config = Config()
|
config = Config()
|
||||||
|
|
||||||
file = _latest("/home/gnarwhal/Pictures/Screenshots")
|
contents = b''
|
||||||
print(f"=> Selected file '{file}'")
|
target_file_extension = ""
|
||||||
|
if arguments.latest or arguments.file != None:
|
||||||
|
file_path = ""
|
||||||
|
if arguments.latest:
|
||||||
|
file_path = _latest(config.source_directory)
|
||||||
|
else:
|
||||||
|
file_path = arguments.file
|
||||||
|
print(f"=> Uploading file '{file_path}'")
|
||||||
|
|
||||||
id = time.time_ns()
|
with open(file_path, mode="rb") as file:
|
||||||
(_, extension) = os.path.splitext(file)
|
contents = file.read()
|
||||||
target_file_name = f"{id}{extension}"
|
|
||||||
target_destination = f"{config.ssh_user}@{config.ssh_target}:{config.ssh_dir}/{target_file_name}"
|
(_, target_file_extension) = os.path.splitext(file_path)
|
||||||
print(f"=> Uploading to '{target_destination}'")
|
elif arguments.paste:
|
||||||
|
print("=> Uploading contents of clipboard")
|
||||||
|
contents = pyclip.paste()
|
||||||
|
target_file_extension = ".txt"
|
||||||
|
else:
|
||||||
|
print("=> Error: must specify one of -f FILE, -l, -p")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
target_id = time.time_ns()
|
||||||
|
target_file_name = f"{target_id}{target_file_extension}"
|
||||||
|
target_file = f"{config.ssh_dir}/{target_file_name}"
|
||||||
|
target_destination = f"{config.ssh_user}@{config.ssh_target}"
|
||||||
|
print(f"=> Uploading to '{target_destination}:{target_file}'")
|
||||||
process = subprocess.run([
|
process = subprocess.run([
|
||||||
"scp",
|
"ssh",
|
||||||
f"-P {config.ssh_port}",
|
f"-p {config.ssh_port}",
|
||||||
file,
|
|
||||||
target_destination,
|
target_destination,
|
||||||
])
|
"-T",
|
||||||
|
f"cat - > {target_file}"
|
||||||
|
],
|
||||||
|
input = contents,
|
||||||
|
)
|
||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
print("=> Error: failed to upload file")
|
print("=> Error: failed to upload file")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
target_url = f"https://{config.ssh_target}/sshare/{target_file_name}"
|
target_url = f"https://{config.ssh_target}/sshare/{target_file_name}"
|
||||||
pyclip.copy(target_url)
|
|
||||||
print(f"=> File available at '{target_url}'")
|
print(f"=> File available at '{target_url}'")
|
||||||
|
if arguments.copy:
|
||||||
|
pyclip.copy(target_url)
|
||||||
|
print("=> URL copied to clipboard")
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
@ -58,19 +82,6 @@ def parse_arguments():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog = "SSHare",
|
prog = "SSHare",
|
||||||
description = "Upload files to a server via ssh",
|
description = "Upload files to a server via ssh",
|
||||||
formatter_class = argparse.RawDescriptionHelpFormatter,
|
|
||||||
epilog =
|
|
||||||
"""
|
|
||||||
Configuration Files:
|
|
||||||
|
|
||||||
Configuration files will be looked for in
|
|
||||||
'$XDG_CONFIG_DIR/sshare/'. If XDG_DATA_DIR is not set,
|
|
||||||
SSHare will fallback to '$HOME/.config/sshare/'.
|
|
||||||
|
|
||||||
If it exists, `default.py' will be considered as config[0].
|
|
||||||
Otherwise, all other configuration files will be ordered
|
|
||||||
alphabetically.
|
|
||||||
"""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
@ -81,27 +92,29 @@ Configuration Files:
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-l",
|
"-l",
|
||||||
"--list",
|
"--latest",
|
||||||
action="store_const",
|
action="store_const",
|
||||||
help="List available configuration files",
|
const=True,
|
||||||
|
help="Upload the latest image from the source directory",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--paste",
|
||||||
|
action="store_const",
|
||||||
|
const=True,
|
||||||
|
help="Upload the contents of the clipboard as a txt",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--file",
|
||||||
|
help="Upload a file",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-c",
|
"-c",
|
||||||
"--config",
|
"--copy",
|
||||||
help="Use the config at location CONFIG",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-n",
|
|
||||||
"--number",
|
|
||||||
type=int,
|
|
||||||
default=0,
|
|
||||||
help="Use config number n (Default: 0)",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-i",
|
|
||||||
"--interactive",
|
|
||||||
action="store_const",
|
action="store_const",
|
||||||
help="Run interactively",
|
const=True,
|
||||||
|
help="Copy the resultant url to the clipboard",
|
||||||
)
|
)
|
||||||
arguments = parser.parse_args()
|
arguments = parser.parse_args()
|
||||||
|
|
||||||
|
@ -122,6 +135,7 @@ def _latest(directory, key=os.path.getmtime):
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.source_directory = "/home/gnarwhal/Pictures/Screenshots"
|
||||||
self.ssh_port = 2222
|
self.ssh_port = 2222
|
||||||
self.ssh_user = getpass.getuser()
|
self.ssh_user = getpass.getuser()
|
||||||
self.ssh_target = "monodon.me"
|
self.ssh_target = "monodon.me"
|
||||||
|
|
Loading…
Reference in a new issue