Skip to content

Commit

Permalink
Merge pull request #21 from sofusalbertsen/sal/config-file-option
Browse files Browse the repository at this point in the history
 #20 adding dynamic config file specification [deploy]
  • Loading branch information
naesheim committed Dec 8, 2020
2 parents b06b29e + a339186 commit dfa5fcd
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions functionhub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

# global class with all global data
class Global(object):
def __init__(self, endpoint, debug, token):
def __init__(self, endpoint, debug, token,configfile):
self.endpoint = endpoint
self.debug = debug
self.token = token
self.configfile = configfile

def read_config(config_file):
try:
Expand All @@ -29,24 +30,25 @@ def read_config(config_file):
@click.option('--endpoint', '-e', envvar='FH_ENDPOINT', default='https://cloudstash.io/artifact', help='Endpoint URL for your FunctionHub')
@click.option('--debug', '-d', envvar='FH_DEBUG', default=False, help='Enable debug output')
@click.option('--token', '-at', envvar='ACCESS_TOKEN', default="", help='Set access token for publishing to private repositories')
@click.option('--configfile','-f',envvar='FH_CONFIG', default="config.ini", help='FunctionHub config file')
# enabling the built in --version option. Uses the version from setup.py
@click.version_option()
# pass the main command context to other subcommands
@click.pass_context
def fuhub(ctx, endpoint, debug, token):
def fuhub(ctx, endpoint, debug, token,configfile):
# add a Global object to the context that will be passed to all subcommands
ctx.obj = Global(endpoint, debug, token)
ctx.obj = Global(endpoint, debug, token, configfile)

# subcommnad for upload operation
@fuhub.command(name='upload')
@click.argument('zip_file', type=click.Path(exists=True,resolve_path=True))
@click.pass_obj
def upload_function(global_config, zip_file):
pass
if not os.path.exists('config.ini'):
if not os.path.exists(global_config.configfile):
raise click.ClickException(f"Config file could not be found")

config = read_config('config.ini')
config = read_config(global_config.configfile)
payload = {}
try:
payload['artifact_name'] = config.get('FUNCTION','name')
Expand All @@ -69,6 +71,7 @@ def upload_function(global_config, zip_file):
json=payload,
headers={'content-type':'application/json', 'Authorization': global_config.token} if global_config.token else {'content-type':'application/json'}
)
click.echo(r)
click.echo(r.status_code)

except KeyError as ke:
Expand All @@ -86,9 +89,9 @@ def create_function(global_config, package_name, desired_dir):
try:
target_dir=os.path.join(desired_dir,package_name)
os.mkdir(target_dir)
config_file_path = os.path.join(os.path.dirname(__file__), 'config.ini')
config_file_path = os.path.join(os.path.dirname(__file__), global_config.configfile)
config_file = open(config_file_path, 'r')
project_config = open(f"{target_dir}/config.ini", 'w')
project_config = open(f"{target_dir}/{global_config.configfile}", 'w')
for line in config_file.readlines():
project_config.write(line)

Expand Down

0 comments on commit dfa5fcd

Please sign in to comment.