Subversion Repositories zfs_utils

Rev

Rev 25 | Rev 30 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 25 Rev 27
Line 11... Line 11...
11
 
11
 
12
 
12
 
13
our $VERSION = '0.1';
13
our $VERSION = '0.1';
14
our $logFileName = '/tmp/zfs_utils.log'; # this can be overridden by the caller, and turned off with empty string
14
our $logFileName = '/tmp/zfs_utils.log'; # this can be overridden by the caller, and turned off with empty string
15
our $displayLogsOnConsole = 1;
15
our $displayLogsOnConsole = 1;
-
 
16
our $merge_stderr = 0; # if set to 1, stderr is captured in runCmd
16
 
17
 
17
# Execute a command and return its output.
18
# Execute a command and return its output.
18
# If called in scalar context, returns the full output as a single string.
19
# If called in scalar context, returns the full output as a single string.
19
# If called in list context, returns the output split into lines.
20
# If called in list context, returns the output split into lines.
20
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
21
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
21
# returns empty string or empty list on failure and logs failure message.
22
# returns empty string or empty list on failure and logs failure message.
22
sub runCmd {
23
sub runCmd {
23
   my ($cmd, $merge_stderr) = @_;
24
   my $cmd = \@_;
24
   $merge_stderr = 1 unless defined $merge_stderr;
25
   $merge_stderr = 1 unless defined $merge_stderr;
25
 
-
 
26
   my $output = '';
26
   my $output = '';
27
 
27
 
28
   if (ref $cmd eq 'ARRAY') {
28
   if (ref $cmd eq 'ARRAY') {
29
      # Execute without a shell (safer). Note: stderr is not merged in this path.
29
      # Execute without a shell (safer). Note: stderr is not merged in this path.
-
 
30
      logMsg( 'Running command [' . join ' ', @$cmd . ']');
30
      open my $fh, '-|', @{$cmd} or do {
31
      open my $fh, '-|', @{$cmd} or do {
31
         logMsg("runCmd: failed to exec '@{$cmd}': $!");
32
         logMsg("runCmd: failed to exec '@{$cmd}': $!");
32
         return wantarray ? () : '';
33
         return wantarray ? () : '';
33
      };
34
      };
34
      local $/ = undef;
35
      local $/ = undef;
35
      $output = <$fh>;
36
      $output = <$fh>;
36
      close $fh;
37
      close $fh;
37
   } else {
38
   } else {
38
      # Scalar command runs via the shell; optionally merge stderr into stdout.
39
      # Scalar command runs via the shell; optionally merge stderr into stdout.
-
 
40
      logMsg( "Scalar running command [$cmd]" );
39
      my $c = $cmd;
41
      my $c = $cmd;
40
      $c .= ' 2>&1' if $merge_stderr;
42
      $c .= ' 2>&1' if $merge_stderr;
41
      $output = `$c`;
43
      $output = `$c`;
42
   }
44
   }
43
 
45
 
Line 182... Line 184...
182
        } or do {
184
        } or do {
183
            logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
185
            logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
184
            return ($default && ref $default eq 'HASH') ? $default : {};
186
            return ($default && ref $default eq 'HASH') ? $default : {};
185
        };
187
        };
186
    };
188
    };
187
 
-
 
188
    # Ensure we have a hashref
189
    # Ensure we have a hashref
189
    die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');
190
    die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');
190
 
191
 
191
    return $yaml;
192
    return $yaml;
192
}
193
}