Skip to content

Commit 2cffd13

Browse files
committed
added support for cmd specific env vars and auth
1 parent c481086 commit 2cffd13

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

pystackql/stackql.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sys, subprocess, json, os, asyncio, functools
1212
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
1313
import pandas as pd
14+
import tempfile
1415

1516
from io import StringIO
1617

@@ -207,41 +208,53 @@ def _run_query(self, query, custom_auth=None, env_vars=None):
207208
local_params = self.params.copy()
208209
local_params.insert(1, f'"{query}"')
209210

210-
# # Handle custom authentication if provided
211-
# if custom_auth:
212-
# if '--auth' in local_params:
213-
# auth_index = local_params.index('--auth')
214-
# local_params.pop(auth_index) # remove --auth
215-
# local_params.pop(auth_index) # remove the auth string
216-
# authstr = json.dumps(custom_auth)
217-
# local_params.extend(["--auth", f"'{authstr}'"])
218-
219211
# Handle custom authentication if provided
220212
if custom_auth:
221213
if '--auth' in local_params:
222214
auth_index = local_params.index('--auth')
223215
local_params.pop(auth_index) # remove --auth
224216
local_params.pop(auth_index) # remove the auth string
225217
authstr = json.dumps(custom_auth)
226-
227-
# For Windows PowerShell, transpose quotes: wrap with " and use ' inside the JSON
228-
if self.platform.startswith("Windows"):
229-
authstr = authstr.replace('"', "'") # Replace inner double quotes with single quotes
230-
authstr = f'"{authstr}"' # Wrap the entire string in double quotes
231-
else:
232-
authstr = f"'{authstr}'" # Use single quotes on non-Windows platforms
233-
234-
local_params.extend(["--auth", authstr])
218+
local_params.extend(["--auth", f"'{authstr}'"])
235219

236220
output = {}
237221
env_command_prefix = ""
238222

223+
# # Determine platform and set environment command prefix accordingly
224+
# if env_vars:
225+
# if self.platform.startswith("Windows"):
226+
# # For Windows, use PowerShell syntax
227+
# env_command_prefix = "& { " + " ".join([f'$env:{key} = "{value}";' for key, value in env_vars.items()]) + " "
228+
# full_command = f"{env_command_prefix}{self.bin_path} " + " ".join(local_params) + " }"
229+
# is_github_actions = os.environ.get('GITHUB_ACTIONS') == 'true'
230+
# # ok new approach, if we are in github actions, dump the command to a file and run it
231+
# # GO!
232+
# else:
233+
# # For Linux/Mac, use standard env variable syntax
234+
# env_command_prefix = "env " + " ".join([f'{key}="{value}"' for key, value in env_vars.items()]) + " "
235+
# full_command = env_command_prefix + " ".join([self.bin_path] + local_params)
236+
# else:
237+
# full_command = " ".join([self.bin_path] + local_params)
238+
239+
# print(full_command) # For debugging
240+
239241
# Determine platform and set environment command prefix accordingly
240242
if env_vars:
241243
if self.platform.startswith("Windows"):
242-
# For Windows, use PowerShell syntax
244+
# For Windows, use PowerShell syntax with GitHub Actions check
243245
env_command_prefix = "& { " + " ".join([f'$env:{key} = "{value}";' for key, value in env_vars.items()]) + " "
244246
full_command = f"{env_command_prefix}{self.bin_path} " + " ".join(local_params) + " }"
247+
is_github_actions = os.environ.get('GITHUB_ACTIONS') == 'true'
248+
249+
# If in GitHub Actions, write command to a PowerShell script file and execute it
250+
if is_github_actions:
251+
with tempfile.NamedTemporaryFile(delete=False, suffix=".ps1", mode="w") as script_file:
252+
# Write environment variable setup and command to script file
253+
for key, value in env_vars.items():
254+
script_file.write(f'$env:{key} = "{value}";\n')
255+
script_file.write(f"{self.bin_path} " + " ".join(local_params) + "\n")
256+
script_path = script_file.name
257+
full_command = f"powershell -File {script_path}"
245258
else:
246259
# For Linux/Mac, use standard env variable syntax
247260
env_command_prefix = "env " + " ".join([f'{key}="{value}"' for key, value in env_vars.items()]) + " "

0 commit comments

Comments
 (0)