def main(args=None): args = parser.parse_args(args) try: _main(args) except GroupNotFound: parser.error('%s at service %s: Group does not exist.' % (args.group, args.service)) except GroupExists as e: parser.error("%s: Group already exists." % e.args[0])
def _main(args): # Actions that do not act on an existing group: if args.action == 'add': try: backend.create_group(group=args.group, service=args.service) except GroupExists: parser.error('Group already exists.') elif args.action in ['list', 'ls']: groups = backend.list_groups(service=args.service) for name in sorted(groups): print(name) elif args.action == 'view': explicit_users = sorted(backend.members(group=args.group, service=args.service, depth=0)) effective_users = sorted(backend.members(group=args.group, service=args.service)) if explicit_users: print('* Explicit members: %s' % ', '.join(explicit_users)) else: print('* No explicit members') if backend.SUPPORTS_SUBGROUPS: if effective_users: print('* Effective members: %s' % ', '.join(effective_users)) else: print('* No effective members') if backend.SUPPORTS_SUBGROUPS is True: parents = backend.parents(group=args.group, service=args.service) sub_groups = backend.subgroups(group=args.group, service=args.service, filter=False) if parents: if backend.SUPPORTS_GROUP_VISIBILITY: print('* Parent groups:') parents = sorted(parents, key=lambda g: g[1].username if g[1] else '') print_by_service(parents, ' ') else: print('* Parent groups: %s' % ', '.join([g[0] for g in parents])) else: print('* No parent groups') if sub_groups: if backend.SUPPORTS_GROUP_VISIBILITY: print('* Subgroups:') sub_groups = sorted(sub_groups, key=lambda g: g[1].username if g[1] else '') print_by_service(sub_groups, ' ') else: print('* Subgroups: %s' % ', '.join([g[0] for g in sub_groups])) else: print('* No subgroups') elif args.action == 'set-service': if backend.SUPPORTS_GROUP_VISIBILITY is False: parser.error('Backend does not support group visiblity.') backend.set_service(group=args.group, service=args.service, new_service=args.new_service) elif args.action == 'add-user': backend.add_member(group=args.group, service=args.service, user=args.user) elif args.action == 'add-group': if backend.SUPPORTS_SUBGROUPS is False: parser.error('Backend does not support subgroups.') backend.add_subgroup(group=args.group, service=args.service, subgroup=args.subgroup, subservice=args.sub_service) elif args.action in ['delete', 'del', 'rm']: backend.remove_group(group=args.group, service=args.service) elif args.action in ['remove-user', 'rm-user', 'del-user']: try: backend.remove_member(group=args.group, service=args.service, user=args.user) except UserNotFound: parser.error('User "%s" not member of group "%s".' % (args.user, args.group)) elif args.action == 'rename': backend.rename_group(args.group, args.name, service=args.service) elif args.action in ['remove-group', 'rm-group', 'del-group']: # pragma: no branch if backend.SUPPORTS_SUBGROUPS is False: parser.error('Backend does not support subgroups.') try: backend.remove_subgroup(group=args.group, service=args.service, subgroup=args.subgroup, subservice=args.sub_service) except GroupNotFound: parser.error('Group "%s" is not a subgroup of "%s".' % (args.subgroup, args.group))
def main(args=None): # parse arguments args = parser.parse_args(args=args) # Actions that do not act on an existing group: if args.action == 'add': try: group_backend.create(name=args.group, service=args.service) except GroupExists: parser.error('Group already exists.') elif args.action in ['list', 'ls']: groups = group_backend.list(service=args.service) for name in sorted(groups): print(name) elif args.action == 'view': group = get_group(parser, args.group, args.service) explicit_users = sorted(group_backend.members(group, depth=0)) effective_users = sorted(group_backend.members(group)) parent_groups = sorted(group_backend.parents(group)) sub_groups = sorted(group_backend.subgroups(group, filter=False)) if explicit_users: print('* Explicit members: %s' % ', '.join(explicit_users)) else: print('* No explicit members') if effective_users: print('* Effective members: %s' % ', '.join(effective_users)) else: print('* No effective members') if parent_groups: print('* Parent groups:') print_by_service(parent_groups, ' ') else: print('* No parent groups') if sub_groups: print('* Subgroups:') print_by_service(sub_groups, ' ') else: print('* No subgroups') elif args.action == 'set-service': group = get_group(parser, args.group, args.service) group_backend.set_service(group, args.new_service) elif args.action == 'add-user': group = get_group(parser, args.group, args.service) group_backend.add_user(group, args.user) elif args.action == 'add-group': group = get_group(parser, args.group, args.service) subgroup = get_group(parser, args.subgroup, args.sub_service) group_backend.add_subgroup(group, subgroup) elif args.action in ['delete', 'del', 'rm']: group = get_group(parser, args.group, args.service) group_backend.remove(group) elif args.action in ['remove-user', 'rm-user', 'del-user']: group = get_group(parser, args.group, args.service) try: group_backend.rm_user(group, args.user) except UserNotFound: parser.error('User "%s" not member of group "%s".' % (args.user.username, group.name)) elif args.action == 'rename': group = get_group(parser, args.group, args.service) try: group_backend.rename(group, args.name) except GroupExists as e: parser.error("%s: %s" % (_d(args.name), e)) elif args.action in ['remove-group', 'rm-group', 'del-group']: # pragma: no branch group = get_group(parser, args.group, args.service) subgroup = get_group(parser, args.subgroup, args.sub_service) try: group_backend.rm_subgroup(group, subgroup) except GroupNotFound: parser.error('Group "%s" is not a subgroup of "%s".' % (subgroup.name, group.name))