Class: BooticCli::CLI

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

Constant Summary collapse

CUSTOM_COMMANDS_DIR =
ENV.fetch('BTC_CUSTOM_COMMANDS_PATH') { File.join(ENV['HOME'], 'bootic') }

Constants included from Connectivity

BooticCli::Connectivity::DEFAULT_ENV

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sub(klass, descr) ⇒ Object



196
197
198
199
# File 'lib/bootic_cli/cli.rb', line 196

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

Instance Method Details

#__print_versionObject



23
24
25
# File 'lib/bootic_cli/cli.rb', line 23

def __print_version
  puts "#{BooticCli::VERSION} (Ruby #{RUBY_VERSION})"
end

#checkObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bootic_cli/cli.rb', line 137

def check
  logged_in_action do
    say "Yup, API connection is working!\n\n", :green

    print_table([
      [bold('Email'), root.email],
      [bold('Shop'), "#{shop.url} (#{shop.subdomain})"],
      [bold('Scopes'), root.scopes]
    ])
  end
end

#consoleObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/bootic_cli/cli.rb', line 159

def console
  logged_in_action do
    require 'irb'
    require 'irb/completion'
    IRB.setup nil

    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::Irb.new(IRB::WorkSpace.new(context))
    IRB.conf[:MAIN_CONTEXT] = irb.context

    trap('SIGINT') do
      irb.signal_handle
    end

    begin
      catch(:IRB_EXIT) do
        irb.eval_input
      end
    ensure
      IRB.irb_at_exit
    end
  end
end

#eraseObject



127
128
129
130
131
132
133
134
# File 'lib/bootic_cli/cli.rb', line 127

def erase
  if session.setup?
    session.erase!
    say "Ok mister. All credentials have been erased.", :magenta
  else
    say "Couldn't find any stored credentials.", :red
  end
end

#helpObject

override Thor’s help method to print banner and check for keys



15
16
17
18
19
# File 'lib/bootic_cli/cli.rb', line 15

def help
  say "Bootic CLI v#{BooticCli::VERSION}\n\n", :bold
  super
  check_client_keys
end

#login(scope = 'admin') ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bootic_cli/cli.rb', line 79

def (scope = 'admin')
  if !session.setup?
    say "App not configured for #{options[:environment]} environment. Running setup first. You only need to do this once.", :red
    invoke :setup, []
  end

  if session.logged_in?
    input = ask "Looks like you're already logged in. Do you want to redo this step? [n]", :magenta
    if input != 'y'
      say "That's what I thought! Try running `bootic help`."
      exit(1)
    end
  end

  email = ask("Enter your Bootic email:", :bold)
  pass  = ask("Enter your Bootic password:", :bold, echo: false)

  if email.strip == '' or email['@'].nil? or pass.strip == ''
    say "\nPlease make sure to enter valid data.", :red
    exit 1
  end

  say "\n\nAlrighty! Getting access token for #{email}...\n"

  begin
    session.(email, pass, scope)
    say "Great success! You're now logged in as #{email} (#{scope})", :green
    say "For a list of available commands, run `bootic help`."
  rescue StandardError => e
    say e.message, :red
    if e.message['No application with client ID']
      sleep 2
      say "\nTry running `bootic setup` again. Or perhaps you missed the ENV variable?", :magenta
    end
  end
end

#logoutObject



117
118
119
120
121
122
123
124
# File 'lib/bootic_cli/cli.rb', line 117

def logout
  if session.logged_in?
    session.logout!
    say 'Done. You are now logged out.', :magenta
  else
    say "You're not logged in. Did you mean `bootic login` perhaps?", :red
  end
end

#runner(filename) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/bootic_cli/cli.rb', line 150

def runner(filename)
  require 'bootic_cli/file_runner'

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

#setupObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bootic_cli/cli.rb', line 28

def setup
  apps_host   = "auth.bootic.net"
  dev_app_url = "#{apps_host}/dev/cli"

  if session.setup?
    input = ask "Looks like you're already set up. Do you want to re-enter your app's credentials? [n]", :magenta
    if input != 'y'
      say 'Thought so. You can run `bootic help` for a list of supported commands.'
      exit(1)
    end
  else
    say "This CLI uses the #{bold('Bootic API')} in order to interact with your shop's data."
    say "This means you need to create a Bootic app at #{bold(dev_app_url)} to access the API and use the CLI.\n"
  end

  input = ask "Have you created a Bootic app yet? [n]"
  if input == 'y'
    say "Great. Remember you can get your app's credentials at #{bold(dev_app_url)}."
  else
    say "Please visit https://#{bold(dev_app_url)} and hit the 'Create' button."
    sleep 2
    # Launchy.open(apps_url)
    say ""
  end

  if current_env != DEFAULT_ENV
    auth_host = ask("Enter auth endpoint host (#{BooticClient.configuration.auth_host}):", :bold).chomp
    api_root  = ask("Enter API root (#{BooticClient.configuration.api_root}):", :bold).chomp
    auth_host = nil if auth_host == ""
    api_root  = nil if api_root == ""
  end

  client_id     = ask("Enter your application's client_id:", :bold)
  client_secret = ask("Enter your application's client_secret:", :bold)

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

  if current_env == DEFAULT_ENV
    say "Credentials stored!", :magenta
  else
    say "Credentials stored for #{current_env} env.", :magenta
  end

  return if ENV['nologin']

  say ""
  sleep 3
  
end