Skip to content

Commit 646756b

Browse files
committed
feat: make logging WARNING by default, controllable by flags. Inform user about progress
1 parent 36bcfdc commit 646756b

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

gh_org_mgr/__init__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,6 @@
44

55
"""Global init file"""
66

7-
import logging
87
from importlib.metadata import version
98

109
__version__ = version("github-org-manager")
11-
12-
13-
def configure_logger(debug: bool = False) -> logging.Logger:
14-
"""Set logging options"""
15-
log = logging.getLogger()
16-
logging.basicConfig(
17-
encoding="utf-8",
18-
format="[%(asctime)s] %(levelname)s: %(message)s",
19-
datefmt="%Y-%m-%d %H:%M:%S",
20-
)
21-
if debug:
22-
log.setLevel(logging.DEBUG)
23-
else:
24-
log.setLevel(logging.INFO)
25-
26-
return log

gh_org_mgr/_helpers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,40 @@
44

55
"""Helper functions"""
66

7+
import logging
8+
import sys
79
from dataclasses import asdict
810

911

12+
def configure_logger(verbose: bool = False, debug: bool = False) -> logging.Logger:
13+
"""Set logging options"""
14+
log = logging.getLogger()
15+
logging.basicConfig(
16+
encoding="utf-8",
17+
format="[%(asctime)s] %(levelname)s: %(message)s",
18+
datefmt="%Y-%m-%d %H:%M:%S",
19+
)
20+
if debug:
21+
log.setLevel(logging.DEBUG)
22+
elif verbose:
23+
log.setLevel(logging.INFO)
24+
else:
25+
log.setLevel(logging.WARNING)
26+
27+
return log
28+
29+
30+
def log_progress(message: str) -> None:
31+
"""Log progress messages to stderr"""
32+
# Clear line if no message is given
33+
if not message:
34+
sys.stderr.write("\r\033[K")
35+
sys.stderr.flush()
36+
else:
37+
sys.stderr.write(f"\r\033[K⏳ {message}")
38+
sys.stderr.flush()
39+
40+
1041
def sluggify_teamname(team: str) -> str:
1142
"""Slugify a GitHub team name"""
1243
# TODO: this is very naive, no other special chars are

gh_org_mgr/manage.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import logging
99
import sys
1010

11-
from . import __version__, configure_logger
11+
from . import __version__
1212
from ._config import parse_config_files
1313
from ._gh_org import GHorg
14+
from ._helpers import configure_logger, log_progress
1415
from ._setup_team import setup_team
1516

1617
# Main parser with root-level flags
@@ -26,7 +27,8 @@
2627

2728
# Common flags, usable for all effective subcommands
2829
common_flags = argparse.ArgumentParser(add_help=False) # No automatic help to avoid duplication
29-
common_flags.add_argument("--debug", action="store_true", help="Get verbose logging output")
30+
common_flags.add_argument("-v", "--verbose", action="store_true", help="Get INFO logging output")
31+
common_flags.add_argument("-vv", "--debug", action="store_true", help="Get DEBUG logging output")
3032

3133
# Sync commands
3234
parser_sync = subparsers.add_parser(
@@ -95,14 +97,15 @@ def main():
9597
# Process arguments
9698
args = parser.parse_args()
9799

98-
configure_logger(args.debug)
100+
configure_logger(verbose=args.verbose, debug=args.debug)
99101

100102
# Sync command
101103
if args.command == "sync":
104+
log_progress("Preparing...")
102105
if args.dry:
103106
logging.info("Dry-run mode activated, will not make any changes at GitHub")
104107
if args.force:
105-
logging.info("Force mode activated, will make potentially dangerous actions")
108+
logging.warning("Force mode activated, will make potentially dangerous actions")
106109

107110
org = GHorg()
108111

@@ -129,33 +132,43 @@ def main():
129132
org.ratelimit()
130133

131134
# Synchronise organisation owners
135+
log_progress("Synchronising organisation owners...")
132136
org.sync_org_owners(dry=args.dry, force=args.force)
133137
# Create teams that aren't present at Github yet
138+
log_progress("Creating missing teams...")
134139
org.create_missing_teams(dry=args.dry)
135140
# Configure general settings of teams
141+
log_progress("Configuring team settings...")
136142
org.sync_current_teams_settings(dry=args.dry)
137143
# Synchronise the team memberships
144+
log_progress("Synchronising team memberships...")
138145
org.sync_teams_members(dry=args.dry)
139146
# Report and act on teams that are not configured locally
147+
log_progress("Checking for unconfigured teams...")
140148
org.get_unconfigured_teams(
141149
dry=args.dry,
142150
delete_unconfigured_teams=cfg_app.get("delete_unconfigured_teams", False),
143151
)
144152
# Report and act on organisation members that do not belong to any team
153+
log_progress("Checking for members without team...")
145154
org.get_members_without_team(
146155
dry=args.dry,
147156
remove_members_without_team=cfg_app.get("remove_members_without_team", False),
148157
)
149158
# Synchronise the permissions of teams for all repositories
159+
log_progress("Synchronising team permissions...")
150160
org.sync_repo_permissions(dry=args.dry, ignore_archived=args.ignore_archived)
151161
# Remove individual collaborator permissions if they are higher than the one
152162
# from team membership (or if they are in no configured team at all)
163+
log_progress("Synchronising individual collaborator permissions...")
153164
org.sync_repo_collaborator_permissions(dry=args.dry)
154165

155166
# Debug output
167+
log_progress("") # clear progress
156168
logging.debug("Final dataclass:\n%s", org.pretty_print_dataclass())
157169
org.ratelimit()
158170

171+
# Print changes
159172
org.stats.print_changes(
160173
orgname=cfg_org.get("org_name", ""), output=args.output, dry=args.dry
161174
)

0 commit comments

Comments
 (0)