Class: Legato::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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

def initialize
  config_path = File.join(ENV['HOME'], '.legato.yml')

  if File.exists?(config_path)
    @config = YAML.load_file(config_path) rescue {}
  else
    puts "##########################################################"
    puts "We can auto-load OAuth2 config from ~/.legato.yml"
    puts "##########################################################"

    build_config
  end

  build_access_token

  print_yaml unless File.exists?(config_path)
end

Instance Method Details

#build_access_tokenObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/legato/cli.rb', line 82

def build_access_token
  if token
    @access_token = OAuth2::AccessToken.new(client, token)
  else
    puts
    puts "Opening the OAuth2 auth url: #{OAuth2Helpers.auth_url(client)} ..."
    `open "#{OAuth2Helpers.auth_url(client)}"`

    puts
    print 'OAuth2 Code (in the url): '
    code = $stdin.gets.strip

    @access_token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost')
    @config['token'] = @access_token.token

    puts
    print "Your oauth token is #{@config['token']}"
    puts
  end
end

#build_configObject



72
73
74
75
76
77
78
79
80
# File 'lib/legato/cli.rb', line 72

def build_config
  @config ||= {}

  puts
  print 'Your OAuth2 id: '
  @config['id'] = $stdin.gets.strip
  print 'Your OAuth2 secret: '
  @config['secret'] = $stdin.gets.strip
end

#build_userObject



103
104
105
# File 'lib/legato/cli.rb', line 103

def build_user
  Legato::User.new(@access_token)
end

#clientObject



64
65
66
# File 'lib/legato/cli.rb', line 64

def client
  @client ||= OAuth2Helpers.build_client(@config['id'], @config['secret'])
end


107
108
109
110
111
112
# File 'lib/legato/cli.rb', line 107

def print_yaml
  puts "##########################################################"
  puts "If you haven't already done so, you can make ~/.legato.yml"
  puts YAML.dump(@config)
  puts "##########################################################"
end

#runObject



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

def run
  setup_irb
end

#setup_irbObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/legato/cli.rb', line 50

def setup_irb
  IRB.setup(nil)
  irb = IRB::Irb.new
  IRB.conf[:MAIN_CONTEXT] = irb.context
  irb.context.evaluate("require 'irb/completion'", 0)

  trap("SIGINT") do
    irb.signal_handle
  end
  catch(:IRB_EXIT) do
    irb.eval_input
  end
end

#tokenObject



68
69
70
# File 'lib/legato/cli.rb', line 68

def token
  @config['token']
end