Class: Snaptoken::CLI

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

Constant Summary collapse

CONFIG_FILE =
"leg.yml"

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/snaptoken/cli.rb', line 4

def initialize
  initial_dir = FileUtils.pwd

  last_dir = nil
  while FileUtils.pwd != last_dir
    if File.exist?(CONFIG_FILE)
      @config = YAML.load(File.read(CONFIG_FILE))
      if @config == false
        puts "Error: Invalid config file."
        exit!
      end
      @config = {} unless @config.is_a?(Hash)
      @config[:path] = FileUtils.pwd
      break
    end

    last_dir = FileUtils.pwd
    FileUtils.cd('..')
  end

  FileUtils.cd(initial_dir)
end

Instance Method Details

#run(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/snaptoken/cli.rb', line 27

def run(args)
  args = ["help"] if args.empty?
  cmd_name = args.shift.downcase

  if cmd_name =~ /\A\d+\z/
    args.unshift(cmd_name)
    cmd_name = "ref"
  end

  if cmd = Snaptoken::Commands::LIST.find { |cmd| cmd.name == cmd_name }
    cmd.new(args, @config).run
  else
    puts "There is no '#{cmd_name}' command. Run `leg help` for help."
  end
end