Module: Inquirex::TTY::OutputPath
- Defined in:
- lib/inquirex/tty/output_path.rb
Overview
Shared output path resolution for commands that accept an -o/--output option.
Rules:
- nil or empty output → nil (caller should write to stdout)
- output is an existing dir → <dir>/<flow-basename>.<ext>
- output has an extension → <output> with its extension replaced by .<ext>
- output has no extension → <output>.<ext>
For commands that must always write to a file (e.g. images that cannot go
to stdout), use OutputPath.resolve_with_default, which falls back to
<flow-basename>.<ext> in the current directory.
Class Method Summary collapse
-
.flow_basename(flow_file) ⇒ String
The flow file's basename without its extension.
-
.resolve(flow_file, output, extension) ⇒ String?
Resolves an output path, returning nil when the caller should use stdout.
-
.resolve_with_default(flow_file, output, extension) ⇒ String
Resolves an output path, falling back to "
. " in cwd when no --output was given. -
.with_default_extension(path, extension) ⇒ String
Ensures
pathends with the given extension.
Class Method Details
.flow_basename(flow_file) ⇒ String
The flow file's basename without its extension.
47 48 49 |
# File 'lib/inquirex/tty/output_path.rb', line 47 def flow_basename(flow_file) File.basename(flow_file, File.extname(flow_file)) end |
.resolve(flow_file, output, extension) ⇒ String?
Resolves an output path, returning nil when the caller should use stdout.
25 26 27 28 29 30 |
# File 'lib/inquirex/tty/output_path.rb', line 25 def resolve(flow_file, output, extension) return nil if output.nil? || output.to_s.empty? return File.join(output, "#{flow_basename(flow_file)}#{extension}") if File.directory?(output) with_default_extension(output, extension) end |
.resolve_with_default(flow_file, output, extension) ⇒ String
Resolves an output path, falling back to "
39 40 41 |
# File 'lib/inquirex/tty/output_path.rb', line 39 def resolve_with_default(flow_file, output, extension) resolve(flow_file, output, extension) || "#{flow_basename(flow_file)}#{extension}" end |
.with_default_extension(path, extension) ⇒ String
Ensures path ends with the given extension. If the path has no
extension, appends it. If it has one, replaces it.
57 58 59 60 61 62 63 |
# File 'lib/inquirex/tty/output_path.rb', line 57 def with_default_extension(path, extension) if File.extname(path).empty? "#{path}#{extension}" else path.sub(%r{\.[^/.]+\z}, extension) end end |