File: cli.rb

package info (click to toggle)
ruby-oauth 0.5.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 584 kB
  • sloc: ruby: 4,070; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'optparse'
require 'oauth/cli/base_command'
require 'oauth/cli/help_command'
require 'oauth/cli/query_command'
require 'oauth/cli/authorize_command'
require 'oauth/cli/sign_command'
require 'oauth/cli/version_command'
require 'active_support/core_ext/string/inflections'

module OAuth
  class CLI
    def self.puts_red(string)
      puts "\033[0;91m#{string}\033[0m"
    end

    ALIASES = {
      'h' => 'help',
      'v' => 'version',
      'q' => 'query',
      'a' => 'authorize',
      's' => 'sign',
    }

    def initialize(stdout, stdin, stderr, command, arguments)
      klass = get_command_class(parse_command(command))
      @command = klass.new(stdout, stdin, stderr, arguments)
      @help_command = HelpCommand.new(stdout, stdin, stderr, [])
    end

    def run
      @command.run
    end

    private

    def get_command_class(command)
      Object.const_get("OAuth::CLI::#{command.camelize}Command")
    end

    def parse_command(command)
      case command = command.to_s.downcase
      when '--version', '-v'
        'version'
      when '--help', '-h', nil, ''
        'help'
      when *ALIASES.keys
        ALIASES[command]
      when *ALIASES.values
        command
      else
        OAuth::CLI.puts_red "Command '#{command}' not found"
        'help'
      end
    end
  end
end