Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from RedHatProductSecurity/add-rcfile
Browse files Browse the repository at this point in the history
GRIF-30, add ~/.griffonrc file
  • Loading branch information
JimFuller-RedHat authored Feb 15, 2023
2 parents 6685fac + 9d43a00 commit a578f57
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
19 changes: 19 additions & 0 deletions griffon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import configparser
import logging
import os
from configparser import ConfigParser
from functools import partial, wraps

import corgi_bindings
Expand All @@ -13,6 +15,9 @@
CORGI_API_URL = os.environ["CORGI_API_URL"]
OSIDB_API_URL = os.environ["OSIDB_API_URL"]

GRIFFON_CONFIG_DIR = os.getenv("GRIFFON_API_URL", "~/.griffon")
GRIFFON_RC_FILE = "~/.griffonrc"

logger = logging.getLogger("rich")


Expand All @@ -22,6 +27,20 @@ def get_logging(level="INFO"):
return logging.getLogger("rich")


def get_config():
"""read ~/.griffonrc ini file, if it does not exist then return some default config"""
if not os.path.exists(os.path.expanduser(GRIFFON_RC_FILE)):
config = configparser.ConfigParser(allow_no_value=True)
config.optionxform = str
config.add_section("default")
config["default"]["format"] = "text"
config.add_section("exclude")
return config
config = ConfigParser()
config.read(os.path.expanduser(GRIFFON_RC_FILE))
return config


class CorgiService:
name = "component-registry"
description = "Red Hat component registry"
Expand Down
6 changes: 4 additions & 2 deletions griffon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import click
import click_completion

from griffon import get_logging
from griffon import get_config, get_logging

from .commands.configure import configure_grp
from .commands.docs import docs_grp
Expand All @@ -22,6 +22,8 @@

click_completion.init()

griffon_config = get_config()


@click.group()
@click.pass_context
Expand Down Expand Up @@ -90,7 +92,7 @@ def docs(ctx):
@click.option(
"--format",
type=click.Choice([el.value for el in OUTPUT_FORMAT]),
default="json",
default=griffon_config["default"]["format"],
)
@click.pass_context
def cli(ctx, debug, format):
Expand Down
22 changes: 19 additions & 3 deletions griffon/commands/configure.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""
"""
import configparser
import logging
import os

import click

from griffon import GRIFFON_CONFIG_DIR, GRIFFON_RC_FILE

logger = logging.getLogger("rich")


Expand All @@ -15,7 +19,19 @@ def configure_grp(ctx):
pass


@configure_grp.command(name="stub")
def stub():
@configure_grp.command(name="setup", help="Create ~/.griffon and .griffonrc config file")
def setup():
"""stub"""
click.echo("generate ~/.griffonrc configuration file")
if not os.path.exists(os.path.expanduser(GRIFFON_CONFIG_DIR)):
os.makedirs(os.path.expanduser(GRIFFON_CONFIG_DIR))
logger.warning(f"{GRIFFON_CONFIG_DIR} created")
else:
logger.warning(f"{GRIFFON_CONFIG_DIR} already exists")

config = configparser.ConfigParser(allow_no_value=True)
config.optionxform = str
config.add_section("default")
config["default"]["format"] = "text"
config.add_section("exclude")
with open(os.path.expanduser(GRIFFON_RC_FILE), "w") as configfile:
config.write(configfile)

0 comments on commit a578f57

Please sign in to comment.