Class: TLDR::CLI::Commands

Inherits:
Object
  • Object
show all
Includes:
TTY::Option
Defined in:
lib/tldr/cli/commands.rb

Constant Summary collapse

URL_BASE =
ENV.fetch('TLDR_URL_BASE', 'https://raw.githubusercontent.com/tldr-pages/tldr/main/pages')
URL_SUFFIX =
ENV.fetch('TLDR_URL_SUFFIX', '.md')
LOCAL_BASE =
ENV.fetch('TLDR_LOCAL_BASE', "#{Dir.home}/.config/tldr/pages")

Instance Method Summary collapse

Instance Method Details

#runObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tldr/cli/commands.rb', line 101

def run
  parse

  if params.errors.any?
    puts params.errors.summary
  elsif params[:help]
    print help
  elsif params[:version]
    version
  elsif params[:query]
    query, lang, platform, source =
      params.to_h.values_at(:query, :lang, :platform, :source)

    page_path = "/#{platform}/#{query}"

    if source == 'local' && local_page?(local_path(page_path, lang: lang))
      content = File.read(local_path(page_path, lang: lang))
      render_markdown(content)
      return
    end

    response = Faraday.get(remote_path(page_path, lang: lang))
    return not_found unless response.success?

    render_markdown(response.body)
  else
    print help
  end
end