Class: Mpx::Cli

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

Overview

Command line interface.

Constant Summary collapse

Usage =
"Usage: #{File.basename($0)} [operation] [args...]"
MpxRoot =
'MPX_ROOT'
RootFolder =
'mpx'

Class Method Summary collapse

Class Method Details

.data_homeObject



145
146
147
148
149
150
151
# File 'lib/mpx/cli.rb', line 145

def self.data_home
  data_home = ENV['XDG_DATA_HOME']
  return data_home if data_home

  home = ENV['HOME']
  return File.join(home, '.local', 'share') if home
end

.get_rootObject



130
131
132
133
134
135
# File 'lib/mpx/cli.rb', line 130

def self.get_root
  root = self.root
  raise 'Unable to determine root folder' unless root
  raise 'Root folder does not exist' unless File.directory?(root)
  return root
end

.helpObject



91
92
93
94
95
# File 'lib/mpx/cli.rb', line 91

def self.help
  puts Help
  puts
  puts Usage
end

.history(args) ⇒ Object



105
106
107
108
# File 'lib/mpx/cli.rb', line 105

def self.history(args)
  history = Loader.new(self.get_root).history
  puts history.get(*args)
end

.listObject



101
102
103
# File 'lib/mpx/cli.rb', line 101

def self.list
  puts Loader.new(self.get_root).list
end

.rootObject



137
138
139
140
141
142
143
# File 'lib/mpx/cli.rb', line 137

def self.root
  root = ENV[MpxRoot]
  return root if root

  data_home = self.data_home
  return File.join(data_home, RootFolder) if data_home
end

.run(args) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mpx/cli.rb', line 110

def self.run(args)
  loader = Loader.new(self.get_root)
  request = Request.new(args)
  commands = loader.load(request.name).sort
  history = loader.history

  mut = Mutex.new
  commands.map do |command|
    Thread.new do
      result = command.run(request.args)
      history.write(command.name, *request.args)
      mut.synchronize {
        puts result
        puts
        puts
      }
    end
  end.map(&:join)
end

.startObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mpx/cli.rb', line 68

def self.start
  op, *args = ARGV
  case op
  when 'help'
    self.help
  when 'version'
    self.version
  when 'list'
    self.list
  when 'history'
    self.history(args)
  when nil
    self.help
    exit 1
  else
    self.run(ARGV)
  end
rescue => e
  puts "Error: #{e}."
  puts Usage
  exit 1
end

.versionObject



97
98
99
# File 'lib/mpx/cli.rb', line 97

def self.version
  puts Mpx::VERSION
end