import subprocess, threading, sys, re, time
def run_cloudpub(command):
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=True,
bufsize=1,
)
extracted_url = [None]
def monitor(stream_in, stream_out, extract_url=False):
def reader():
with stream_in:
for line in stream_in:
stream_out.write(line)
if extract_url:
match = re.search(r'.+ -> (.+)', line)
if match:
extracted_url[0] = match.group(1)
return reader
threading.Thread(target=monitor(process.stdout, sys.stdout, True), daemon=True).start()
threading.Thread(target=monitor(process.stderr, sys.stderr), daemon=True).start()
while True:
if extracted_url[0]:
return extracted_url[0]
time.sleep(1)
url = run_cloudpub("./clo publish http 8080")
print(f"Published URL: {url}")
input("Press Enter to exit...")