I'm working with azure CLI to build out some automation around repo creation, etc. I'm using python as a sort of wrapper around various CLI commands to bundle up the automation. I want to write in a simple check to see if a repo name has been used and exists or not.
repoName comes from a system input and would be whatever the user wants to name their fresh repository.
So far I have this:
azRepoListCmd = "az repos list --query \"[?contains(name, \'" + repoName + "\')].[name]\" --organization https://myOrganizationHere.visualstudio.com/ --project myProject -o tsv"
azRepoList = os.system(azRepoListCmd)
print(azRepoList)
what the above returns is :
test-project-2
0
What is this "0" and where does it come from? Expected result would just be the name or an empty array if it didn't find anything.
The 0
is the resultcode of running os.system
, which doesn't capture output.
https://docs.python.org/3/library/os.html#os.system
So your azRepoList = ...
line is actually what's outputting the repo name, then the next line is outputting the result code.
What you want instead is subprocess.
import subprocess as sp
output = sp.getoutput("az repos list --query \"[?contains(name, 'PartsUnlimited')].[name]\" --project \"Parts Unlimited\" -o tsv")
print (output)
PartsUnlimited