More config

This commit is contained in:
Gnarwhal 2024-08-01 01:27:12 +00:00
parent aff77c5cc2
commit c3e61db909
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174

View file

@ -31,22 +31,41 @@ class Config:
_config = self._load_from_file(f"{config_directory}/sshare/config.toml") _config = self._load_from_file(f"{config_directory}/sshare/config.toml")
self.source_directory = _config.get("source_directory") 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") ssh = _config.get("ssh")
if ssh == None: if ssh == None:
print("Error: 'ssh' cannot be 'None'") print("Error: 'ssh' cannot be 'None'")
sys.exit(1) sys.exit(1)
self.ssh_port = ssh.get("port") self.ssh_port = ssh.get("port")
self.ssh_user = ssh.get("user") self.ssh_user = ssh.get("user")
self.ssh_target = ssh.get("target") self.ssh_host = ssh.get("target")
self.ssh_directory = ssh.get("directory") self.ssh_directory = ssh.get("directory")
if self.ssh_port == None: if self.ssh_port == None:
self.ssh_port = 22 self.ssh_port = 22
if self.ssh_user == None: if self.ssh_user == None:
self.ssh_user = getpass.getuser() self.ssh_user = getpass.getuser()
if self.ssh_target == None: if self.ssh_host == None:
print("Error: 'ssh.target' cannot be 'None'") self.ssh_host = self.host_name;
sys.exit(1)
if self.ssh_directory == None: if self.ssh_directory == None:
print("Error: 'ssh.directory' cannot be 'None'") print("Error: 'ssh.directory' cannot be 'None'")
sys.exit(1) sys.exit(1)
@ -88,7 +107,7 @@ def main():
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_directory}/{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 host: {target_destination}, port: {config.ssh_port}, file: {target_file}") print(f"Uploading to host: {target_destination}, port: {config.ssh_port}, file: {target_file}")
process = subprocess.run([ process = subprocess.run([
"ssh", "ssh",
@ -103,7 +122,7 @@ def main():
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)
@ -165,5 +184,3 @@ def _latest(directory, key=os.path.getmtime):
selection = file selection = file
selection_key = key selection_key = key
return selection return selection