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

Class Method Details

.flow_basename(flow_file) ⇒ String

The flow file's basename without its extension.

Parameters:

  • flow_file (String)

Returns:

  • (String)


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.

Parameters:

  • flow_file (String)

    source .rb flow file (basename used for defaults)

  • output (String, nil)

    value of the --output option

  • extension (String)

    including leading dot, e.g. ".json"

Returns:

  • (String, nil)


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 "." in cwd when no --output was given. For commands where stdout is not an option.

Parameters:

  • flow_file (String)
  • output (String, nil)
  • extension (String)

Returns:

  • (String)


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.

Parameters:

  • path (String)
  • extension (String)

    including leading dot

Returns:

  • (String)


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