Script to import and export layout group permissions in bulk [D: 1825] - #641
Script to import and export layout group permissions in bulk [D: 1825]#641Oliver-ctrlo wants to merge 2 commits into
Conversation
This script allows bulk importing and exporting group layout permissions.
pwlodarski-ctrlo
left a comment
There was a problem hiding this comment.
PR reviewed - couple comments added for consideration.
| } | ||
|
|
||
| sub Export_permissions { | ||
| my $export_file = './Group_permission_export.csv'; |
There was a problem hiding this comment.
Would it be beneficial for the filename to be unique? E.g. it could have the group IDs or the current datetime appended.
There was a problem hiding this comment.
Changed to a script argument instead so it would be the same for both import and export.
droberts-ctrlo
left a comment
There was a problem hiding this comment.
Various observations - the majority of which are from the use of die rather than error or fault - you can also use info rather than say, but that's a matter of personal preference and not an issue.
pwlodarski-ctrlo
left a comment
There was a problem hiding this comment.
PR re-reviewed - minor comments added.
| my ($action, $file); | ||
|
|
||
| GetOptions( | ||
| 'action=s' => \$action, |
There was a problem hiding this comment.
May be worth aligning the fat commas to keep the code neat.
| report ERROR => "Invalid CSV header/s. Import must consist of 3 columns: layout_id, group_id and permission" | ||
| unless $headers && join(',', sort @$headers) eq 'group_id,layout_id,permission'; | ||
|
|
||
| while (my $row = $csv->getline_hr($fh)) { |
There was a problem hiding this comment.
The indentation of some lines within this while loop should be reviewed.
| 'file=s' => \$file | ||
| ) or exit; | ||
|
|
||
| $action or report ERROR => "Please state if you want to import/export group permissions with --action"; |
There was a problem hiding this comment.
May be worth aligning the fat commas for clarity across the script - it's done in other places in the stack too.
| -f $file or report ERROR => "File '$file' does not exist"; | ||
|
|
||
| my $csv = Text::CSV->new({ binary => 1 }) | ||
| or report ERROR => "Cannot use CSV: ".Text::CSV->error_diag (); |
There was a problem hiding this comment.
For clarity, would be ideal to indent this line.
|
|
||
| sub Import_permissions { | ||
|
|
||
| -f $file or report ERROR => "File '$file' does not exist"; |
There was a problem hiding this comment.
Generally better to use parameters, e.g. ERROR => "File '{file}' does not exist", file => $file
|
|
||
| my $csv = Text::CSV->new({ binary => 1 }) | ||
| or report ERROR => "Cannot use CSV: ".Text::CSV->error_diag (); | ||
| open my $fh, "<:encoding(utf8)", $file or report FAULT => "$file"; |
There was a problem hiding this comment.
Better to write something like FAULT => "Unable to open file {file}", file => $file - the reason will be automatically appended when using FAULT
|
Looks good to me to go ahead and use. Various comments to tidy it up in due course. Ideally these sorts of scripts should use high-level functions in the library files (so that as the internal API changes the scripts don't need to be updated). However, this would be a bigger piece of work, as the libraries would also need to be updated for the purpose of this script. |
This script allows bulk importing and exporting group layout permissions.