Class: BooticCli::CLI

Inherits:
Thor
  • Object
show all
Includes:
Connectivity, Thor::Actions
Defined in:
lib/bootic_cli/cli.rb

Constant Summary collapse

DEFAULT_ENV =
'production'.freeze
CUSTOM_COMMANDS_DIR =
ENV.fetch("BTC_CUSTOM_COMMANDS_PATH") { File.join(ENV["HOME"], "btc") }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sub(klass, descr) ⇒ Object



115
116
117
118
# File 'lib/bootic_cli/cli.rb', line 115

def self.sub(klass, descr)
  command_name = underscore(klass.name)
  register klass, command_name, "#{command_name} SUBCOMMAND ...ARGS", descr
end

Instance Method Details

#consoleObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bootic_cli/cli.rb', line 90

def console
  logged_in_action do
    require 'irb'
    require 'irb/completion'
    IRB.setup nil
    IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
    require 'irb/ext/multi-irb'
    require 'bootic_cli/console'
    context = Console.new(session)
    prompt = "/#{shop.subdomain} (#{root.user_name}|#{root.scopes}) $ "

    IRB.conf[:PROMPT][:CUSTOM] = {
      :PROMPT_I => prompt,
      :PROMPT_S => "%l>> ",
      :PROMPT_C => prompt,
      :PROMPT_N => prompt,
      :RETURN => "=> %s\n"
    }
    IRB.conf[:PROMPT_MODE] = :CUSTOM
    IRB.conf[:AUTO_INDENT] = false

    IRB.irb nil, context
  end
end

#eraseObject



60
61
62
63
# File 'lib/bootic_cli/cli.rb', line 60

def erase
  session.erase!
  say "all credentials erased from this computer"
end

#infoObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bootic_cli/cli.rb', line 66

def info
  logged_in_action do
    print_table([
      ['username', root.user_name],
      ['email', root.email],
      ['scopes', root.scopes],
      ['shop', "#{shop.url} (#{shop.subdomain})"],
      ['custom commands dir', CUSTOM_COMMANDS_DIR]
    ])

    say_status 'OK', 'API connection is working', :green
  end
end

#login(scope = 'admin') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bootic_cli/cli.rb', line 32

def (scope = 'admin')
  if !session.setup?
    say "App not configured for #{options[:environment]} environment. Running setup first. You only need to do this once."
    say "Please create an OAuth2 app and get its credentials at https://auth.bootic.net/dev/apps or the relevant auth app for your environment"
    invoke :setup, []
  end

  username  = ask("Enter your Bootic email")
  pwd       = ask("Enter your Bootic password:", echo: false)

  say "Loging in as #{username}. Getting access token..."

  begin
    session. username, pwd, scope
    say "Logged in as #{username} (#{scope})"
    say "try: btc help"
  rescue StandardError => e
    say e.message
  end
end

#logoutObject



54
55
56
57
# File 'lib/bootic_cli/cli.rb', line 54

def logout
  session.logout!
  say_status 'Logged out', 'You are now logged out', :red
end

#runner(filename) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/bootic_cli/cli.rb', line 81

def runner(filename)
  require 'bootic_cli/file_runner'

  logged_in_action do
    FileRunner.run(root, filename)
  end
end

#setupObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bootic_cli/cli.rb', line 16

def setup
  if options[:environment] != DEFAULT_ENV
    auth_host     = ask("Enter auth endpoint host (#{BooticClient::AUTH_HOST}):").chomp
    api_root      = ask("Enter API root (#{BooticClient::API_ROOT}):").chomp
    auth_host = nil if auth_host == ""
    api_root  = nil if api_root == ""
  end
  client_id     = ask("Enter your application's client_id:")
  client_secret = ask("Enter your application's client_secret:")

  session.setup(client_id, client_secret, auth_host: auth_host, api_root: api_root)

  say "Credentials stored for #{options[:environment]} environment. client_id: #{client_id}"
end