Class: Multilocale::CLI

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

Overview

multilocale-ruby — the thin command wrapper around this gem.

The name is not multilocale on purpose: that binary belongs to the npm CLI (npm i -g multilocale), and two tools fighting over one name on PATH is a support ticket nobody enjoys.

Credentials come from the environment only. A secret passed as --api-key=… is in the process table for every user on the machine and in ~/.zsh_history forever, so there is no such flag.

Constant Summary collapse

<<~USAGE
  multilocale-ruby #{VERSION}

  Usage:
    multilocale-ruby pull [options]        download phrases into locale files
    multilocale-ruby push [options]        upload locale files as phrases
    multilocale-ruby projects [options]    list the projects this credential can see
    multilocale-ruby phrases [options]     print one language as key = value
    multilocale-ruby version

  Options:
    -p, --project NAME     project id or name (default: multilocale.json)
    -c, --config PATH      path to multilocale.json (default: nearest one)
        --path TEMPLATE    output path containing %lang% (repeatable)
    -l, --language LANG    restrict to one language (repeatable)
        --flat             write flat "a.b" keys instead of nested hashes
        --json             machine-readable output
        --yes              required by push, which overwrites remote rows
    -h, --help

  Environment:
    MULTILOCALE_API_KEY        API key secret (app.multilocale.com -> API keys)
    MULTILOCALE_ACCESS_TOKEN   operator session token, as an alternative
    MULTILOCALE_API_URL        override the API host
    MULTILOCALE_PROJECT        default project id or name
USAGE

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr) ⇒ CLI

Returns a new instance of CLI.



46
47
48
49
# File 'lib/multilocale/cli.rb', line 46

def initialize(stdout: $stdout, stderr: $stderr)
  @stdout = stdout
  @stderr = stderr
end

Instance Method Details

#run(argv) ⇒ Object

Returns a process exit status: 0 ok, 1 failure, 2 usage.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/multilocale/cli.rb', line 52

def run(argv)
  command = argv.first

  return usage(0) if command.nil? || %w[-h --help help].include?(command)
  return version if %w[version --version -v].include?(command)

  options = parse(argv[1..] || [])

  case command
  when "pull" then pull(options)
  when "push" then push(options)
  when "projects" then projects(options)
  when "phrases" then phrases(options)
  else
    @stderr.puts("Unknown command: #{command}")
    usage(2)
  end
rescue ConfigurationError, ApiError, ConnectionError, Error, ArgumentError => error
  @stderr.puts(error.message)
  1
end