149 lines
4.9 KiB
Plaintext
149 lines
4.9 KiB
Plaintext
"""{{CLI_NAME}} - {{CLI_DESCRIPTION}}
|
|
|
|
Nested Fire CLI template with command groups.
|
|
"""
|
|
import fire
|
|
from rich.console import Console
|
|
from pathlib import Path
|
|
import json
|
|
|
|
console = Console()
|
|
|
|
|
|
class {{CLASS_NAME}}:
|
|
"""{{CLI_DESCRIPTION}}"""
|
|
|
|
def __init__(self):
|
|
self.version = "{{VERSION}}"
|
|
self.config_file = Path.home() / ".{{CLI_NAME_LOWER}}" / "config.json"
|
|
|
|
class Config:
|
|
"""Configuration management commands"""
|
|
|
|
def __init__(self, parent):
|
|
self.parent = parent
|
|
|
|
def get(self, key):
|
|
"""Get configuration value
|
|
|
|
Args:
|
|
key: Configuration key to retrieve
|
|
"""
|
|
config = self._load_config()
|
|
value = config.get(key)
|
|
if value is None:
|
|
console.print(f"[yellow]Key '{key}' not found[/yellow]")
|
|
else:
|
|
console.print(f"[blue]{key}[/blue]: {value}")
|
|
return value
|
|
|
|
def set(self, key, value):
|
|
"""Set configuration value
|
|
|
|
Args:
|
|
key: Configuration key to set
|
|
value: Configuration value
|
|
"""
|
|
config = self._load_config()
|
|
config[key] = value
|
|
self._save_config(config)
|
|
console.print(f"[green]✓[/green] Set {key} = {value}")
|
|
|
|
def list(self):
|
|
"""List all configuration values"""
|
|
config = self._load_config()
|
|
if not config:
|
|
console.print("[yellow]No configuration values set[/yellow]")
|
|
return
|
|
|
|
console.print("[bold]Configuration:[/bold]")
|
|
for key, value in config.items():
|
|
console.print(f" [blue]{key}[/blue]: {value}")
|
|
return config
|
|
|
|
def reset(self):
|
|
"""Reset configuration to defaults"""
|
|
self._save_config({})
|
|
console.print("[green]✓[/green] Configuration reset")
|
|
|
|
def _load_config(self):
|
|
"""Load configuration from file"""
|
|
if not self.parent.config_file.exists():
|
|
return {}
|
|
try:
|
|
return json.loads(self.parent.config_file.read_text())
|
|
except Exception as e:
|
|
console.print(f"[red]Error loading config: {e}[/red]")
|
|
return {}
|
|
|
|
def _save_config(self, config):
|
|
"""Save configuration to file"""
|
|
self.parent.config_file.parent.mkdir(parents=True, exist_ok=True)
|
|
self.parent.config_file.write_text(json.dumps(config, indent=2))
|
|
|
|
class {{SUBCOMMAND_GROUP_NAME}}:
|
|
"""{{SUBCOMMAND_GROUP_DESCRIPTION}}"""
|
|
|
|
def create(self, name, template='default'):
|
|
"""Create new {{RESOURCE_NAME}}
|
|
|
|
Args:
|
|
name: {{RESOURCE_NAME}} name
|
|
template: Template to use (default: default)
|
|
"""
|
|
console.print(f"[cyan]Creating {{RESOURCE_NAME}}: {name}[/cyan]")
|
|
console.print(f"[dim]Using template: {template}[/dim]")
|
|
console.print("[green]✓[/green] {{RESOURCE_NAME}} created successfully")
|
|
|
|
def delete(self, name, confirm=False):
|
|
"""Delete {{RESOURCE_NAME}}
|
|
|
|
Args:
|
|
name: {{RESOURCE_NAME}} name
|
|
confirm: Confirm deletion (default: False)
|
|
"""
|
|
if not confirm:
|
|
console.print("[yellow]⚠ Use --confirm to delete {{RESOURCE_NAME}}[/yellow]")
|
|
return
|
|
|
|
console.print(f"[red]Deleting {{RESOURCE_NAME}}: {name}[/red]")
|
|
console.print("[green]✓[/green] {{RESOURCE_NAME}} deleted")
|
|
|
|
def list(self):
|
|
"""List all {{RESOURCE_NAME}}s"""
|
|
console.print("[bold]{{RESOURCE_NAME}}s:[/bold]")
|
|
# Add list logic here
|
|
items = ["item1", "item2", "item3"]
|
|
for item in items:
|
|
console.print(f" • {item}")
|
|
return items
|
|
|
|
def __init__(self):
|
|
self.version = "{{VERSION}}"
|
|
self.config_file = Path.home() / ".{{CLI_NAME_LOWER}}" / "config.json"
|
|
self.config = self.Config(self)
|
|
self.{{SUBCOMMAND_GROUP_NAME_LOWER}} = self.{{SUBCOMMAND_GROUP_NAME}}()
|
|
|
|
def info(self):
|
|
"""Display CLI information"""
|
|
console.print(f"[bold]{{CLI_NAME}}[/bold] v{self.version}")
|
|
console.print(f"Config file: {self.config_file}")
|
|
return {"version": self.version, "config_file": str(self.config_file)}
|
|
|
|
|
|
def main():
|
|
fire.Fire({{CLASS_NAME}})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
# Usage examples:
|
|
# python {{CLI_NAME_LOWER}}.py config get api_key
|
|
# python {{CLI_NAME_LOWER}}.py config set api_key abc123
|
|
# python {{CLI_NAME_LOWER}}.py config list
|
|
# python {{CLI_NAME_LOWER}}.py {{SUBCOMMAND_GROUP_NAME_LOWER}} create my-item
|
|
# python {{CLI_NAME_LOWER}}.py {{SUBCOMMAND_GROUP_NAME_LOWER}} delete my-item --confirm
|
|
# python {{CLI_NAME_LOWER}}.py {{SUBCOMMAND_GROUP_NAME_LOWER}} list
|