Class: GemDating::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_dating/cli.rb

Constant Summary collapse

SUCCESS =
0
GENERAL_ERROR =
1
HELP_TEXT =
<<~HELP
  gem_dating [--help | -h] [<GEMFILE_FILEPATH>]

  GEMFILE_FILEPATH defaults to ./Gemfile if not provided.

  Options:
    --help, -h, -?  Show this help message
    --older-than=<AGE>, --ot=<AGE>    Filter gems updated within the last X (e.g. 2y, 1m, 4w, 10d)
    --sort-by=<FIELD>                 Sort by field (name or date), defaults to name
    --order=<DIRECTION>               Sort direction (asc or desc), defaults to asc
    --json                            Output results as JSON
HELP

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Cli

Returns a new instance of Cli.



20
21
22
23
24
25
26
# File 'lib/gem_dating/cli.rb', line 20

def initialize(argv = [])
  args, file_path = argv.partition { |arg| (arg =~ /--\w+/) || (arg =~ /(-[a-z])/) }

  @args = args
  @file_path = file_path.first
  @options = parse_args
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gem_dating/cli.rb', line 28

def run
  if @options[:help]
    $stdout << HELP_TEXT
    return SUCCESS
  end

  unless @file_path
    current_directory = Dir.pwd
    file_path = File.join(current_directory, "Gemfile")

    if File.exist?(file_path)
      @file_path = file_path
    else
      $stdout << HELP_TEXT
      return GENERAL_ERROR
    end
  end

  result = GemDating.from_file(@file_path, @options)
  output = @options[:json] ? result.to_json : result.table_print
  $stdout << output << "\n"

  SUCCESS
end