@@ -16,9 +16,11 @@ use Path::Tiny;
1616use OMOP::CSV::Validator;
1717use JSON::XS;
1818use Pod::Usage;
19- use Data::Dumper;
19+ # use Data::Dumper;
2020use Term::ANSIColor; # For colored output
21+ use File::Basename;
2122binmode STDOUT , ' :encoding(UTF-8)' ;
23+ binmode STDERR , ' :encoding(UTF-8)' ;
2224
2325# Module version
2426my $VERSION = $OMOP::CSV::Validator::VERSION ;
@@ -29,13 +31,15 @@ my $csv_file;
2931my $sep = ' ,' ;
3032my $table_name ; # Optional parameter to override table name
3133my $schemas_file ; # Optional: file to save schemas
34+ my $nocolor = 0;
3235
3336GetOptions(
3437 ' ddl=s' => \$ddl_file , # Path to the DDL file (PostgreSQL)
3538 ' input=s' => \$csv_file , # Path to the input CSV file
3639 ' sep=s' => \$sep , # Field separator (default: comma)
3740 ' table|t=s' => \$table_name , # Optional: override table name
3841 ' save-schemas=s' => \$schemas_file , # Optional: file to save schemas
42+ ' no-color|nc' => \$nocolor , # Optional: Turn off STDOUT color
3943 ' help|h' => \my $help , # Show help message
4044 ' version|V' => sub {
4145 say color(" cyan" ), " $0 Version $VERSION " , color(" reset" );
@@ -45,12 +49,24 @@ GetOptions(
4549
4650pod2usage(1) if $help ;
4751
52+ # Turning color off if argument <--no-color>
53+ $ENV {' ANSI_COLORS_DISABLED' } = 1 if $nocolor ;
54+
4855unless ( $ddl_file && $csv_file ) {
4956 warn color(" red" ), " [ERROR] --ddl and --input are required parameters.\n " ,
5057 color(" reset" );
5158 pod2usage(1);
5259}
5360
61+ # Define the hash with emoji values
62+ my %msg_emoji = (
63+ error => ' ❌' ,
64+ warning => ' ⚠️ ' ,
65+ dot => ' ✖' ,
66+ success => ' ✅' ,
67+ save => ' 💾'
68+ );
69+
5470# Read the DDL file (UTF-8)
5571my $ddl_text = path($ddl_file )-> slurp_utf8;
5672
@@ -64,40 +80,42 @@ my $schemas = $validator->load_schemas_from_ddl($ddl_text);
6480if ($schemas_file ) {
6581 my $json = JSON::XS-> new-> utf8-> pretty-> encode($schemas );
6682 path($schemas_file )-> spew_utf8($json );
67- say color(" green" ), " 💾 Schemas saved to '$schemas_file '" , color(" reset" );
83+ say color(" green" ), " $msg_emoji {save} Schemas saved to '$schemas_file '" ,
84+ color(" reset" );
6885}
6986
7087# Determine the schema to use (either from --table or derived from CSV filename)
71- my $schema ;
88+ my ( $schema , $schema_name ) ;
7289if ($table_name ) {
73- $schema = $schemas -> { lc $table_name }
74- or die color(" red" ), " ❌ No schema found for table '$table_name '\n " ,
75- color(" reset" );
90+ $schema = $schemas -> { lc $table_name } or die color(" red" ), " $msg_emoji {error} No schema found for table '$table_name '\n " , color(" reset" );
91+ $schema_name = $table_name ;
7692}
7793else {
78- $schema = $validator -> get_schema_from_csv_filename( $csv_file , $schemas )
79- or die color( " red " ),
80- " ❌ No schema found for table derived from ' $csv_file ' \n " , color( " reset " ) ;
94+ $schema = $validator -> get_schema_from_csv_filename( $csv_file , $schemas ) or die color( " red " ), " $msg_emoji {error} No schema found for table derived from ' $csv_file ' \n " , color( " reset " );
95+ $schema_name = basename( $csv_file );
96+ $schema_name =~ s / \. csv$ // i ;
8197}
8298
8399# Validate the CSV file
84100my $errors = $validator -> validate_csv_file( $csv_file , $schema , $sep );
85101
86102if (@$errors ) {
87- say color(" bold white on_red" ), " ❌ Validation errors found:" ,
103+ say color(" bold white on_red" ),
104+ " $msg_emoji {error} Validation errors found:" ,
88105 color(" reset" );
89106 foreach my $err (@$errors ) {
90- say color(" red" ), " ⚠️ Row $err ->{row} validation failed:" ,
107+ say color(" red" ),
108+ " $msg_emoji {warning} Row $err ->{row} validation failed:" ,
91109 color(" reset" );
92110 foreach my $msg ( @{ $err -> {errors } } ) {
93- say " ✖ $msg " ;
111+ say " $msg_emoji {dot} $msg " ;
94112 }
95113 }
96114 exit 1;
97115}
98116else {
99117 say color(" bold white on_green" ),
100- " ✅ CSV file 'person.csv ' is valid against the 'person ' schema." ,
118+ " $msg_emoji {success} CSV file '$csv_file ' is valid against the '$schema_name ' schema." ,
101119 color(" reset" );
102120 exit 0;
103121}
@@ -137,6 +155,10 @@ to derive the table name from the CSV filename.
137155
138156(optional) Path to a file where the DDL-derived schemas should be saved (in JSON format).
139157
158+ =item B<--no-color > , B<-nc >
159+
160+ (Optional) Turn off STDOUT color
161+
140162=item B<--help > , B<-h >
141163
142164Display this help message.
0 commit comments