-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature_flags_utils.py
More file actions
150 lines (113 loc) Β· 4.33 KB
/
Copy pathfeature_flags_utils.py
File metadata and controls
150 lines (113 loc) Β· 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""
Feature flags management utilities.
Usage:
python feature_flags.py <command> [options]
"""
import sys
from pathlib import Path
try:
import codomyrmex
except ImportError:
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
import argparse
import json
import os
DEFAULT_FLAGS = {
"new_dashboard": {"enabled": False, "description": "New dashboard UI"},
"dark_mode": {"enabled": True, "description": "Dark mode support"},
"experimental_api": {"enabled": False, "description": "Experimental API endpoints"},
"cache_v2": {"enabled": True, "description": "New caching system"},
}
def load_flags(path: Path | None = None) -> dict:
"""Load feature flags from file or defaults."""
if path and path.exists():
return json.loads(path.read_text())
# Check environment
env_flags = {}
for key, val in os.environ.items():
if key.startswith(("FF_", "FEATURE_")):
flag_name = key.replace("FF_", "").replace("FEATURE_", "").lower()
env_flags[flag_name] = {"enabled": val.lower() in ["true", "1", "yes"]}
if env_flags:
return {**DEFAULT_FLAGS, **env_flags}
return DEFAULT_FLAGS
def save_flags(flags: dict, path: Path):
"""Save feature flags to file."""
path.write_text(json.dumps(flags, indent=2))
def is_enabled(flags: dict, name: str) -> bool:
"""Check if a flag is enabled."""
flag = flags.get(name, {})
return flag.get("enabled", False)
def main():
# Auto-injected: Load configuration
from pathlib import Path
import yaml
config_path = (
Path(__file__).resolve().parent.parent.parent
/ "config"
/ "feature_flags"
/ "config.yaml"
)
if config_path.exists():
with open(config_path) as f:
yaml.safe_load(f) or {}
print("Loaded config from config/feature_flags/config.yaml")
parser = argparse.ArgumentParser(description="Feature flags utilities")
subparsers = parser.add_subparsers(dest="command")
# List command
list_cmd = subparsers.add_parser("list", help="List all flags")
list_cmd.add_argument("--file", "-f", help="Flags file")
# Get command
get_cmd = subparsers.add_parser("get", help="Get flag status")
get_cmd.add_argument("name", help="Flag name")
# Set command
set_cmd = subparsers.add_parser("set", help="Set flag")
set_cmd.add_argument("name", help="Flag name")
set_cmd.add_argument("value", choices=["on", "off"])
set_cmd.add_argument("--file", "-f", default="feature_flags.json")
# Create command
create = subparsers.add_parser("create", help="Create flags file")
create.add_argument("--output", "-o", default="feature_flags.json")
args = parser.parse_args()
if not args.command:
print("π© Feature Flags Utilities\n")
print("Commands:")
print(" list - List all flags")
print(" get - Get flag status")
print(" set - Set flag on/off")
print(" create - Create flags file")
return 0
if args.command == "list":
path = Path(args.file) if args.file else None
flags = load_flags(path)
print("π© Feature Flags:\n")
for name, config in flags.items():
status = "β
ON " if config.get("enabled") else "βͺ OFF"
desc = config.get("description", "")
print(f" {status} {name}")
if desc:
print(f" {desc}")
elif args.command == "get":
flags = load_flags()
if args.name not in flags:
print(f"β Unknown flag: {args.name}")
return 1
enabled = is_enabled(flags, args.name)
status = "enabled" if enabled else "disabled"
print(f"π© {args.name}: {status}")
elif args.command == "set":
path = Path(args.file)
flags = load_flags(path if path.exists() else None)
if args.name not in flags:
flags[args.name] = {"description": ""}
flags[args.name]["enabled"] = args.value == "on"
save_flags(flags, path)
print(f"β
Set {args.name} = {args.value}")
elif args.command == "create":
save_flags(DEFAULT_FLAGS, Path(args.output))
print(f"β
Created: {args.output}")
return 0
if __name__ == "__main__":
sys.exit(main())