Skip to content

Instantly share code, notes, and snippets.

@cluelessperson
Created May 5, 2019 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cluelessperson/deb889d0db78b37933571fbef3b8ac01 to your computer and use it in GitHub Desktop.
Save cluelessperson/deb889d0db78b37933571fbef3b8ac01 to your computer and use it in GitHub Desktop.
import sys, subprocess, os, pathlib, json
import fbf
def _get_cli_config_file():
if ".json" in sys.argv[-1]:
return sys.argv[-1]
def _get_cli_config():
config = {}
if any(["-" in a and "v" in a for a in sys.argv]):
config['log_level'] = "DEBUG"
return config
x
def _get_gcloud_project_config_file():
call = "gcloud config get-value project"
try:
project_name = subprocess.check_output(call.split(" "), stderr=open(os.devnull, 'wb')).decode('utf-8').strip()
potential = pathlib.Path("{project_name}.json".format(project_name=project_name))
if potential.is_file():
return str(potential.resolve())
except:
pass
def _get_default_config_file():
config_file = pathlib.Path("config.json")
if config_file.is_file():
return str(config_file.resolve())
def _get_default_config():
config = {
"log_level": "INFO"
}
return config
def get_config():
config = {"log_level": "INFO"}
config_file = _get_cli_config_file() or _get_gcloud_project_config_file() or _get_default_config_file()
if not config_file:
fbf.log.fatal("No config file found. Expects: <command> path.json ./<gcloud_project>.json ./config.json")
sys.exit()
with open(config_file, 'r') as f:
config.update(json.load(f))
config.update(_get_cli_config())
return config, config_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment