Compare commits
2 commits
709d99f729
...
c3e61db909
Author | SHA1 | Date | |
---|---|---|---|
c3e61db909 | |||
aff77c5cc2 |
2 changed files with 68 additions and 23 deletions
|
@ -15,6 +15,7 @@ classifiers = [
|
||||||
"Environment :: Console",
|
"Environment :: Console",
|
||||||
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
||||||
]
|
]
|
||||||
|
requires-python = ">=3.11"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pyclip",
|
"pyclip",
|
||||||
]
|
]
|
||||||
|
|
|
@ -14,15 +14,65 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import getpass
|
import getpass
|
||||||
import importlib.util
|
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import pkg_resources
|
|
||||||
import pyclip
|
import pyclip
|
||||||
import time
|
import time
|
||||||
|
import tomllib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import _version
|
from _version import version
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
def __init__(self):
|
||||||
|
config_directory = os.environ.get("XDG_CONFIG_DIR")
|
||||||
|
if config_directory == None:
|
||||||
|
config_directory = f"{os.environ["HOME"]}/.config"
|
||||||
|
_config = self._load_from_file(f"{config_directory}/sshare/config.toml")
|
||||||
|
|
||||||
|
self.source_directory = _config.get("source_directory")
|
||||||
|
|
||||||
|
host = _config.get("host")
|
||||||
|
if host == None:
|
||||||
|
print("Error: 'host' cannot be 'None'")
|
||||||
|
sys.exit(1)
|
||||||
|
self.host_protocol = host.get("protocol")
|
||||||
|
self.host_name = host.get("name")
|
||||||
|
self.host_port = host.get("port")
|
||||||
|
self.host_path = host.get("path")
|
||||||
|
if self.host_protocol == None:
|
||||||
|
self.host_protocol = "https"
|
||||||
|
if self.host_name == None:
|
||||||
|
print("Error: 'host.name' cannot be 'None'")
|
||||||
|
sys.exit(1)
|
||||||
|
if self.host_port == None:
|
||||||
|
self.host_port = ""
|
||||||
|
else:
|
||||||
|
self.host_port = f":{self.host_port}"
|
||||||
|
if self.host_path == None:
|
||||||
|
self.host_path = ""
|
||||||
|
|
||||||
|
ssh = _config.get("ssh")
|
||||||
|
if ssh == None:
|
||||||
|
print("Error: 'ssh' cannot be 'None'")
|
||||||
|
sys.exit(1)
|
||||||
|
self.ssh_port = ssh.get("port")
|
||||||
|
self.ssh_user = ssh.get("user")
|
||||||
|
self.ssh_host = ssh.get("target")
|
||||||
|
self.ssh_directory = ssh.get("directory")
|
||||||
|
if self.ssh_port == None:
|
||||||
|
self.ssh_port = 22
|
||||||
|
if self.ssh_user == None:
|
||||||
|
self.ssh_user = getpass.getuser()
|
||||||
|
if self.ssh_host == None:
|
||||||
|
self.ssh_host = self.host_name;
|
||||||
|
if self.ssh_directory == None:
|
||||||
|
print("Error: 'ssh.directory' cannot be 'None'")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
def _load_from_file(self, config_path):
|
||||||
|
with open(config_path, mode="rb") as file:
|
||||||
|
return tomllib.load(file)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
arguments = parse_arguments()
|
arguments = parse_arguments()
|
||||||
|
@ -34,28 +84,31 @@ def main():
|
||||||
if arguments.latest or arguments.file != None:
|
if arguments.latest or arguments.file != None:
|
||||||
file_path = ""
|
file_path = ""
|
||||||
if arguments.latest:
|
if arguments.latest:
|
||||||
|
if config.source_directory == "":
|
||||||
|
print("Option 'latest' requires source directory to be specified")
|
||||||
|
sys.exit(1)
|
||||||
file_path = _latest(config.source_directory)
|
file_path = _latest(config.source_directory)
|
||||||
else:
|
else:
|
||||||
file_path = arguments.file
|
file_path = arguments.file
|
||||||
print(f"=> Uploading file '{file_path}'")
|
print(f"Uploading file '{file_path}'")
|
||||||
|
|
||||||
with open(file_path, mode="rb") as file:
|
with open(file_path, mode="rb") as file:
|
||||||
contents = file.read()
|
contents = file.read()
|
||||||
|
|
||||||
(_, target_file_extension) = os.path.splitext(file_path)
|
(_, target_file_extension) = os.path.splitext(file_path)
|
||||||
elif arguments.paste:
|
elif arguments.paste:
|
||||||
print("=> Uploading contents of clipboard")
|
print("Uploading contents of clipboard")
|
||||||
contents = pyclip.paste()
|
contents = pyclip.paste()
|
||||||
target_file_extension = ".txt"
|
target_file_extension = ".txt"
|
||||||
else:
|
else:
|
||||||
print("=> Error: must specify one of -f FILE, -l, -p")
|
print("Error: must specify one of -f FILE, -l, -p")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
target_id = time.time_ns()
|
target_id = time.time_ns()
|
||||||
target_file_name = f"{target_id}{target_file_extension}"
|
target_file_name = f"{target_id}{target_file_extension}"
|
||||||
target_file = f"{config.ssh_dir}/{target_file_name}"
|
target_file = f"{config.ssh_directory}/{target_file_name}"
|
||||||
target_destination = f"{config.ssh_user}@{config.ssh_target}"
|
target_destination = f"{config.ssh_user}@{config.ssh_host}"
|
||||||
print(f"=> Uploading to '{target_destination}:{target_file}'")
|
print(f"Uploading to host: {target_destination}, port: {config.ssh_port}, file: {target_file}")
|
||||||
process = subprocess.run([
|
process = subprocess.run([
|
||||||
"ssh",
|
"ssh",
|
||||||
f"-p {config.ssh_port}",
|
f"-p {config.ssh_port}",
|
||||||
|
@ -66,14 +119,14 @@ def main():
|
||||||
input = contents,
|
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"{config.host_protocol}://{config.host_name}{config.host_port}{config.host_path}/{target_file_name}"
|
||||||
print(f"=> File available at '{target_url}'")
|
print(f"File available at '{target_url}'")
|
||||||
if arguments.copy:
|
if arguments.copy:
|
||||||
pyclip.copy(target_url)
|
pyclip.copy(target_url)
|
||||||
print("=> URL copied to clipboard")
|
print("URL copied to clipboard")
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
@ -88,7 +141,7 @@ def parse_arguments():
|
||||||
"-v",
|
"-v",
|
||||||
"--version",
|
"--version",
|
||||||
action="version",
|
action="version",
|
||||||
version=f"%(prog)s version {_version.version}",
|
version=f"%(prog)s version {version}",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-l",
|
"-l",
|
||||||
|
@ -131,12 +184,3 @@ def _latest(directory, key=os.path.getmtime):
|
||||||
selection = file
|
selection = file
|
||||||
selection_key = key
|
selection_key = key
|
||||||
return selection
|
return selection
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
def __init__(self):
|
|
||||||
self.source_directory = "/home/gnarwhal/Pictures/Screenshots"
|
|
||||||
self.ssh_port = 2222
|
|
||||||
self.ssh_user = getpass.getuser()
|
|
||||||
self.ssh_target = "monodon.me"
|
|
||||||
self.ssh_dir = "/srv/gnarwhal/sshare"
|
|
||||||
|
|
Loading…
Reference in a new issue