feat: implement role-based configuration system for CLI with automatic API key management
- Add role detection to command groups (admin, client, miner, blockchain)
- Load role-specific config files (~/.aitbc/{role}-config.yaml)
- Add role field to Config class with environment variable support
- Implement automatic role detection from invoked subcommand
- Add development mode API key bypass for testing (APP_ENV=dev)
- Update CLI checklist with role-based configuration documentation
- Add configuration override priority and
This commit is contained in:
@@ -8,9 +8,12 @@ from ..utils import output, error, success
|
||||
|
||||
|
||||
@click.group()
|
||||
def admin():
|
||||
@click.pass_context
|
||||
def admin(ctx):
|
||||
"""System administration commands"""
|
||||
pass
|
||||
# Set role for admin commands
|
||||
ctx.ensure_object(dict)
|
||||
ctx.parent.detected_role = 'admin'
|
||||
|
||||
|
||||
@admin.command()
|
||||
|
||||
@@ -19,9 +19,12 @@ from ..utils import output, error
|
||||
|
||||
|
||||
@click.group()
|
||||
def blockchain():
|
||||
@click.pass_context
|
||||
def blockchain(ctx):
|
||||
"""Query blockchain information and status"""
|
||||
pass
|
||||
# Set role for blockchain commands
|
||||
ctx.ensure_object(dict)
|
||||
ctx.parent.detected_role = 'blockchain'
|
||||
|
||||
|
||||
@blockchain.command()
|
||||
|
||||
@@ -9,9 +9,12 @@ from ..utils import output, error, success
|
||||
|
||||
|
||||
@click.group()
|
||||
def client():
|
||||
@click.pass_context
|
||||
def client(ctx):
|
||||
"""Submit and manage jobs"""
|
||||
pass
|
||||
# Set role for client commands
|
||||
ctx.ensure_object(dict)
|
||||
ctx.parent.detected_role = 'client'
|
||||
|
||||
|
||||
@client.command()
|
||||
|
||||
@@ -9,10 +9,18 @@ from typing import Optional, Dict, Any, List
|
||||
from ..utils import output, error, success
|
||||
|
||||
|
||||
@click.group()
|
||||
def miner():
|
||||
@click.group(invoke_without_command=True)
|
||||
@click.pass_context
|
||||
def miner(ctx):
|
||||
"""Register as miner and process jobs"""
|
||||
pass
|
||||
# Set role for miner commands - this will be used by parent context
|
||||
ctx.ensure_object(dict)
|
||||
# Set role at the highest level context (CLI root)
|
||||
ctx.find_root().detected_role = 'miner'
|
||||
|
||||
# If no subcommand was invoked, show help
|
||||
if ctx.invoked_subcommand is None:
|
||||
click.echo(ctx.get_help())
|
||||
|
||||
|
||||
@miner.command()
|
||||
|
||||
@@ -13,6 +13,7 @@ class Config:
|
||||
"""Configuration object for AITBC CLI"""
|
||||
coordinator_url: str = "http://127.0.0.1:8000"
|
||||
api_key: Optional[str] = None
|
||||
role: Optional[str] = None # admin, client, miner, etc.
|
||||
config_dir: Path = field(default_factory=lambda: Path.home() / ".aitbc")
|
||||
config_file: Optional[str] = None
|
||||
|
||||
@@ -21,9 +22,12 @@ class Config:
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Set default config file if not specified
|
||||
# Set default config file based on role if not specified
|
||||
if not self.config_file:
|
||||
self.config_file = str(self.config_dir / "config.yaml")
|
||||
if self.role:
|
||||
self.config_file = str(self.config_dir / f"{self.role}-config.yaml")
|
||||
else:
|
||||
self.config_file = str(self.config_dir / "config.yaml")
|
||||
|
||||
# Load config from file if it exists
|
||||
self.load_from_file()
|
||||
@@ -33,6 +37,8 @@ class Config:
|
||||
self.coordinator_url = os.getenv("AITBC_URL")
|
||||
if os.getenv("AITBC_API_KEY"):
|
||||
self.api_key = os.getenv("AITBC_API_KEY")
|
||||
if os.getenv("AITBC_ROLE"):
|
||||
self.role = os.getenv("AITBC_ROLE")
|
||||
|
||||
def load_from_file(self):
|
||||
"""Load configuration from YAML file"""
|
||||
@@ -43,6 +49,7 @@ class Config:
|
||||
|
||||
self.coordinator_url = data.get('coordinator_url', self.coordinator_url)
|
||||
self.api_key = data.get('api_key', self.api_key)
|
||||
self.role = data.get('role', self.role)
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not load config file: {e}")
|
||||
|
||||
@@ -59,10 +66,13 @@ class Config:
|
||||
'api_key': self.api_key
|
||||
}
|
||||
|
||||
if self.role:
|
||||
data['role'] = self.role
|
||||
|
||||
with open(self.config_file, 'w') as f:
|
||||
yaml.dump(data, f, default_flow_style=False)
|
||||
|
||||
|
||||
def get_config(config_file: Optional[str] = None) -> Config:
|
||||
"""Get configuration instance"""
|
||||
return Config(config_file=config_file)
|
||||
def get_config(config_file: Optional[str] = None, role: Optional[str] = None) -> Config:
|
||||
"""Get configuration instance with optional role"""
|
||||
return Config(config_file=config_file, role=role)
|
||||
|
||||
@@ -9,6 +9,17 @@ from typing import Optional
|
||||
|
||||
from . import __version__
|
||||
from .config import get_config
|
||||
|
||||
|
||||
def with_role(role: str):
|
||||
"""Decorator to set role for command groups"""
|
||||
def decorator(func):
|
||||
@click.pass_context
|
||||
def wrapper(ctx, *args, **kwargs):
|
||||
ctx.parent.detected_role = role
|
||||
return func(ctx, *args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
from .utils import output, setup_logging
|
||||
from .commands.client import client
|
||||
from .commands.miner import miner
|
||||
@@ -107,8 +118,26 @@ def cli(ctx, url: Optional[str], api_key: Optional[str], output: str,
|
||||
# Setup logging based on verbosity
|
||||
log_level = setup_logging(verbose, debug)
|
||||
|
||||
# Load configuration
|
||||
config = get_config(config_file)
|
||||
# Detect role from command name (before config is loaded)
|
||||
role = None
|
||||
|
||||
# Check invoked_subcommand first
|
||||
if ctx.invoked_subcommand:
|
||||
if ctx.invoked_subcommand == 'client':
|
||||
role = 'client'
|
||||
elif ctx.invoked_subcommand == 'miner':
|
||||
role = 'miner'
|
||||
elif ctx.invoked_subcommand == 'blockchain':
|
||||
role = 'blockchain'
|
||||
elif ctx.invoked_subcommand == 'admin':
|
||||
role = 'admin'
|
||||
|
||||
# Also check if role was already set by command group
|
||||
if not role:
|
||||
role = getattr(ctx, 'detected_role', None)
|
||||
|
||||
# Load configuration with role
|
||||
config = get_config(config_file, role=role)
|
||||
|
||||
# Override config with command line options
|
||||
if url:
|
||||
|
||||
Reference in New Issue
Block a user